2017-04-06 129 views
2

我想通過ajax函數調用一個addPost.php文件,並返回結果作爲後提交或不。如何通過ajax函數發送數據到php文件?

對於我打電話按鈕userAction功能的onClick但現在我不通過後獲取數據,當我試圖訪問addPost.php後陣列中的數據文件,它發送錯誤未定義指數類別。

任何人都可以請幫助出了什麼問題嗎?

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Post</title> 

</head> 
<body> 
<form class="postForm" id="postForm" method="post" action="addPost.html"> 


    <fieldset> 
     <legend>Please add the details below </legend> 
     <p> 
      <label for="title">Title (required, at least 2 characters)</label> 
      <input id="title" name="title" minlength="2" type="text" required> 
     </p> 

     <p> 
      <label for="url">URL (required)</label> 
      <input id="url" type="url" name="url"> 
     </p> 

     <p> 
      <label for="desc">Description (required, at least 2 characters)</label> 
      <input id="desc" name="desc" minlength="2" type="text" required> 
     </p> 

     <p> 
      <label for="keywords">Keywords (eg:#facebook)(required, at least 2 characters)</label> 
      <input id="keywords" name="keywords" minlength="2" type="text" required> 
     </p> 

     <p> 
      Select Url Type : 
      <select name="urlType" id="urlType"> 
       <option value="">Select Url Type...</option> 
       <option value="0">Server Image</option> 
       <option value="1">Server Video</option> 
       <option value="2">YouTube Video</option> 
       <option value="3">Vimeo Video</option> 
       <option value="4">Facebook Image</option> 
       <option value="5">Facebook Video</option> 
       <option value="6">Instagram Image</option> 
       <option value="7">Instagram Video</option> 
       <option value="-1">Other</option> 
      </select> 
     </p> 
     <p> 
      Select Category : 
      <select name="category" id="category"> 



      </select> 
     </p> 

     <p> 
      <input type="button" name="Submit" id="Submit" value="Submit" onclick="userAction('add')"> 
     </p> 
    </fieldset> 

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
    <script> 

     getCategories(); 

     function getCategories() { 

      $.ajax({ 
       type: "POST", 
       url: 'getCategories.php', 
       dataType: 'text', 
       async: false, 
       cache: false, 
       success: function (result) { 

        $('#category').html(result); 
       } 
      }); 
     } 

     function userAction(type,id){ 
      id = (typeof id == "undefined")?'':id; 
      var statusArr = {add:"added",edit:"updated",delete:"deleted"}; 
      var userData = ''; 
      if (type == 'add') { 
       userData = $("#postForm").find('.form').serialize() + '&action_type=' + type + '&id=' + id; 
      } 
      $.ajax({ 
       type: 'POST', 
       url: 'addPost.php', 
       data: userData, 
       success:function(report){ 

        alert(report) 
       } 
      }); 
     } 
    </script> 
</form> 
</body> 
</html> 

addPost.php

include 'Database.php'; 
ini_set('display_errors', 1); 
error_reporting(1); 
ini_set('error_reporting', E_ALL); 

if(isset($_POST['action_type']) && !empty($_POST['action_type'])) { 

    if($_POST['action_type'] == 'add') { 

     /* $database = new Database(Constants::DBHOST, Constants::DBUSER, Constants::DBPASS, Constants::DBNAME); 
     $dbConnection = $database->getDB(); 
     $dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

     $stmt = $dbConnection->prepare("insert into keywords(keyword) 
            values(?)"); 
     $stmt->execute(array($_POST['keywords'])); 


     //insert data into posts table 
     $stmt = $dbConnection->prepare("insert into posts(category_id,title,url,url_type,description,keywords) 
            values(?,?,?,?,?,?)"); 
     $stmt->execute(array($_POST['category'], $_POST['title'], $_POST['url'], $_POST['urlType'], $_POST['desc'], $_POST['keywords'])); 

     $count = $stmt->rowCount(); 

     if ($count > 0) { 

      //if inserted 
      $response = array("status" => -1, "message" => "Post Submitted"); 
      return $response; 
     } else { 
      //if not inserted 
      $response = array("status" => -1, "message" => "Could not submit post."); 
      return $response; 
     }*/ 

     echo $_POST['category'], $_POST['title'], $_POST['url'], $_POST['urlType'], $_POST['desc'], $_POST['keywords']; 


    } 

} 

編輯:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Post</title> 

</head> 
<body> 
<form class="postForm" id="postForm" method="post" action="addPost.php"> 


    <fieldset> 
     <legend>Please add the details below </legend> 
     <p> 
      <label for="title">Title (required, at least 2 characters)</label> 
      <input id="title" name="title" minlength="2" type="text" required> 
     </p> 

     <p> 
      <label for="url">URL (required)</label> 
      <input id="url" type="url" name="url" required> 
     </p> 

     <p> 
      <label for="desc">Description (required, at least 2 characters)</label> 
      <input id="desc" name="desc" minlength="2" type="text" required> 
     </p> 

     <p> 
      <label for="keywords">Keywords (eg:#facebook)(required, at least 2 characters)</label> 
      <input id="keywords" name="keywords" minlength="2" type="text" required> 
     </p> 

     <p> 
      Select Url Type : 
      <select name="urlType" id="urlType"> 
       <option value="">Select Url Type...</option> 
       <option value="0">Server Image</option> 
       <option value="1">Server Video</option> 
       <option value="2">YouTube Video</option> 
       <option value="3">Vimeo Video</option> 
       <option value="4">Facebook Image</option> 
       <option value="5">Facebook Video</option> 
       <option value="6">Instagram Image</option> 
       <option value="7">Instagram Video</option> 
       <option value="-1">Other</option> 
      </select> 
     </p> 
     <p> 
      Select Category : 
      <select name="category" id="category"> 

      </select> 
     </p> 


     <p> 
      <input type="hidden" name="action_type" id="action_type_id"/> 
      <input type="hidden" name="id" id="p_id"/> 

      <input type="button" name="Submit" id="Submit" value="Submit" onclick="userAction('add')"> 
     </p> 


     <p id="report"></p> 

    </fieldset> 

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
    <script> 

     getCategories(); 

     $("#postForm").validate(); 

     function getCategories() { 

      $.ajax({ 
       type: "POST", 
       url: 'getCategories.php', 
       dataType: 'text', 
       async: false, 
       cache: false, 
       success: function (result) { 

        $('#category').html(result); 
       } 
      }); 
     } 
     function userAction(type,id){ 

      var statusArr = {add:"added",edit:"updated",delete:"deleted"}; 

      if (type == 'add') { 
       $('#action_type_id').val(type); 
       $('#p_id').val(id); 
      } 
      $.ajax({ 
       type: 'POST', 
       url: 'addPost.php', 
       data: $('#postForm').serialize(), 
       success:function(report){ 
        $('#report').html(result); 
       } 
      }); 
     } 

    </script> 
</form> 
</body> 
</html> 

現在這個編輯代碼在數據庫中的數據越來越插入,但我想顯示的結果中添加對.html所以我已經在成功的方法給出了帕的id,但這是行不通的,也形式驗證不起作用。

請幫忙。謝謝。

+0

您的ajax方法類型是post,所以可能'&action_type'值和其他參數不能發送給控制器。 –

+0

那麼應該是什麼類型?以及如何訪問這些數據? @AtaurRahmanMunna – Sid

+0

我的** addPost.php **文件中也沒有看到任何''標籤。 –

回答

1

您可以使用隱藏值在服務器中發送數據。在您的表單中放置這兩個字段。

<input type="hidden" name="action_type" id="action_type_id"/> 
<input type="hidden" name="id" id="p_id"/> 

而且你ajax方法是這樣的:

function userAction(type,id){ 

     var statusArr = {add:"added",edit:"updated",delete:"deleted"}; 

     if (type == 'add') { 
      $('#action_type_id').val(type); 
      $('#p_id').val(id); 
     } 
     $.ajax({ 
      type: 'POST', 
      url: 'addPost.php', 
      data: $('#postForm').serialize(), 
      success:function(report){ 
       alert(report); 
      } 
     }); 
} 

說明:之前提交表單,您可以設置typeid值隱藏字段和發送整個形式server.now你可以通過$_POST['action_type']$_POST['id']獲得所有發佈數據。

+0

你能檢查編輯的問題嗎? – Sid

+0

@Sid我會爲你製作一個pastebin代碼。 –

+0

如果在窗體關閉標籤後添加了腳本,那麼表單提交就會提交。但我通過ajax函數獲取類別,如果在關閉表單後添加腳本,則選擇標記將顯示空白。 – Sid

0
echo $_POST['category'], $_POST['title'], $_POST['url'], $_POST['urlType'], $_POST['desc'], $_POST['keywords'] 
在此聲明

,您嘗試訪問「類別」,「冠軍」等,但你有沒有過在阿賈克斯字段(用戶數據),所以你得到了「未定義指數類別」 - 這個錯誤