【问题标题】:Getting error on submiting the form using AJAX使用 AJAX 提交表单时出错
【发布时间】:2017-01-16 12:08:16
【问题描述】:

我创建了一个具有表单的 php 文件。表单接受输入,它有 4 个按钮来添加、显示、更新和删除数据库中的记录。

然后另一个php文件读取用户数据并根据点击的提交按钮进行操作。

我希望作为成功/错误的输出在单击任一提交按钮时显示在第一个 php 文件中。 它需要 AJAX,但我是新手,不知道如何让它工作!!

另外,如果从服务器端(php文件)接收到的数据是json格式,如何在第一个php文件中读取呢?

<form id="form" class="col s12"><!--  action="submit.php">-->
  <div class="row">
    <div class="input-field col s12">
      <input id="id" name="id" type="text" class="validate">
      <label for="id">ID:</label>
    </div>
  </div>
<div class="row">
    <div class="input-field col s12 m6">
      <input id="firstname" name="firstname" type="text" class="validate">
      <label for="firstname">FIRST NAME:</label>
    </div>
    <div class="input-field col s12 m6">
      <input id="lastname" name="lastname" type="text" class="validate">
      <label for="lastname">LAST NAME:</label>
    </div>
  </div>
<div class="row">
    <div class="col s12 m3">
       <input type="submit" value="Add" name="add" id="add" class="btn waves-effect" >
    </div>
    <div class="col s12 m3">
       <input type="submit" value="Display" name="display" id="display" class="btn waves-effect">
    </div>
    <div class="col s12 m3">
       <input type="submit" value="Update" name="update" id="update" class="btn waves-effect">
    </div>
    <div class="col s12 m3">
       <input type="submit" value="Delete" name="delete" id="delete" class="btn waves-effect">
    </div>
  </div>
</form>

这是表格。

我试过了:

blog.teamtreehouse.com/create-ajax-contact-forhttp://stackoverflow.com/questions/16616250/form-submit-with-ajax-passing-form-data-to-php-without-page-refreshm

https://www.formget.com/submit-form-using-ajax-php-and-jquery/

https://www.formget.com/form-submission-using-ajax-php-and-javascript/

还有更多示例,但没有一个有效!

<script>

$(document).ready(function(){
            $("#form").submit(function(){
//                      event.preventDefault();

            $.ajax({
                    url: "submit.php",
                    type: "POST",
                    data: $("#form").serialize(),
                    dataType: "json",
                    success: function(response)
                    {
                            var data = $.parseJSON(response);
//                              var data = JSON.parse(response);
//                              console.log(data);
                            alert(data.msg);
                    },
                    error: function(xhr, status, thrown)
                    {
//                              alert(status);
                            console.log("xhr= " + xhr);
                            console.log("status= " + status);
                            console.log("error= " + thrown);
//                              console.log(response);
                    }
            });
            });
            return false;
       });
</script>

php 文件也只发送 json 格式的数据!

if(isset($_POST['add']) AND !empty($id))
{

try {

    $conn = $GLOBALS['pdo'];

    $stmt = $conn->prepare("INSERT INTO user (id, firstname, lastname, email, reg_date) VALUES (:id, :firstname, :lastname, :email, :reg_date)");

    $stmt->bindParam(':id', intval($id));
    $stmt->bindParam(':firstname', $firstname);
    $stmt->bindParam(':lastname', $lastname);
    $stmt->bindParam(':email', $email);
    $stmt->bindParam(':reg_date', $reg_date);

    $stmt->execute();
    if($stmt)
    {
//              echo "<script type='text/javascript'>alert('Successfully Added!');</script>";
            return json_encode(array('error' => 0, 'msg' => 'Added user'));
//              return true;
    }
    else
    {
            return json_encode(array('error' => 1, 'msg' => 'Failed to add user!'));
//              return false;
    }
}
catch(PDOException $e)
{
    echo "ERROR! " . $e->getMessage ;
}
$conn = null;

}

【问题讨论】:

  • 哟,代码在哪里?
  • 那么你为什么认为另一个例子(即使这里为你提供了一个盘子)会起作用?
  • @YourCommonSense 我添加了代码!我一直在阅读和尝试 4-5 小时。我将不胜感激!
  • @samayo 我添加了代码!

标签: php json ajax


【解决方案1】:

在js中先改变这一行 $( "表单" ).submit(函数(事件)

并在form method='post'中更改此行

【讨论】:

  • 在控制台中:xhr= [object Object] status= parsererror error= SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data NetworkError: 发生网络错误。
  • 现在:xhr= [object Object] status= error error= Internal Server Error
猜你喜欢
  • 2014-08-26
  • 2018-01-27
  • 1970-01-01
  • 2016-01-16
  • 1970-01-01
  • 2014-02-09
  • 2013-03-24
  • 2018-05-10
相关资源
最近更新 更多