如何通过ajax函数发送数据到php文件?

问题描述:

我想通过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 **文件中也没有看到任何''标签。 –

您可以使用隐藏值在服务器中发送数据。在您的表单中放置这两个字段。

<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

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

,您尝试访问“类别”,“冠军”等,但你有没有过在阿贾克斯字段(用户数据),所以你得到了“未定义指数类别” - 这个错误