【问题标题】:How to insert data in database through jQuery Ajax in php如何在 php 中通过 jQuery Ajax 在数据库中插入数据
【发布时间】:2018-06-27 00:38:42
【问题描述】:

Hello 堆栈现在我面临一个问题我无法通过 jQuery Ajax 在数据库中插入数据我已附加所有相关文件任何人都可以通过它我是 php 开发的新手。

**task.php**

<html>
<head>
<style type="text/css">

    html, body {
        margin: 5px;
        padding: 5px;
        background: #fff;
    }

    form {
        border: 1px solid #999;
        padding: 0.25em;
        background: #EEE;

    }

    div {
        padding-bottom: 0.25em;
    }

    /* essential */
    label {
        float: left;
        width: 6em;
        text-align: right;
        padding-right: 0.5em;
    }

    input, textarea {
        text-align: left;
        width: 200px;
        height:30px;
    }

    input.submit {
        margin-left: 6.5em;
        width: auto;
    }


    label, input.submit {
        font: normal 0.8em Verdana, Geneva, Arial, Helvetica, sans-serif;
    }
</style>
<script src="jquery-3.3.1.js"></script>
<script src="insert.js"></script>

<script>
</script>
</head>
<body>
<h1>FORM</h1>
<form>
    <div>
        <label>Name:</label>
        <input type="text" name="name" id="Name" />
    </div>
    <div>
        <label>Age:</label>
        <input type="text" name="age" id="age"/>
    </div>
    <div>
        <label>Email Id:</label>
        <input type="text" name="emailId" id="emailId"/>
    </div>
    <div>
        <label>Password:</label>
        <input type="text" name="password" id="pass"/>
    </div>

    <div>
        <input type="submit" id="submit" name="submit" value="Submit" onclick="insertData()" />
    </div>
</form>
</body>
</html>

这是我的 insert.php 文件,我在其中编写了与插入数据相关的所有查询。

**insert.php**

<?php
include("connection.php");

if(isset($_POST['submit']))
{

    $Fname=$_POST["name"];
    $Age=$_POST["age"];
    $EmailId=$_POST["emailId"];
    $Password=$_POST["password"];

    $Insert = "INSERT INTO simpleform (Name, Age, Email_Id, Password) 
               VALUES ('.$Fname.', '.$Age.', '.$EmailId.', '.$Password.')";
    $Query=mysqli_query($con, $Insert);

    print_r($Insert);


    if(!$Query) 
    {
        echo mysqli_error();
    }
    else
    {
        echo "Successfully Inserted <br />";

    }
}

?>

这是我的 insert.js 文件,我在其中完成了所有与 ajax 相关的工作。

**insert.js**

  function insertData() {
      debugger;
      var name=$("#Name").val();
      var age=$("#age").val();
      var emailId=$("#emailId").val();
      var pass=$("#pass").val();
      debugger;

      // AJAX code to send data to php file.
      $.ajax({
          type: "POST",
          url: "kamalesh/insert.php",
          data: {name:name,age:age,emailId:emailId,pass:pass},
          dataType: "JSON",
          success: function(data) {

              alert("Data Inserted");

          }
      });
  }

【问题讨论】:

  • 您能清楚地解释您的问题吗?你预计会发生什么,正在发生什么,是否有任何错误?这不清楚你在问什么
  • 在控制台中我得到正确的数据但无法访问我的 insert.php 文件
  • 您的问题需要有一个完整和适当的解释,恐怕您提供的有限口头信息我们无能为力
  • 您是否尝试过使用类似数据在 phpMyAdmin 中运行查询?
  • 调试代码并分享结果。

标签: php jquery html ajax


【解决方案1】:

看起来在您的insert.php 中,当您的insert.js 函数将参数传递给insert.php 时,if(isset($_POST['submit'])) 正在阻止脚本运行。当 AJAX 将参数传递给insert.php 时,isset($_POST['submit']) 将返回 false。

您需要从nsert.php 中删除if(isset($_POST['submit']))。我还建议您阻止表单提交和刷新您的页面。查看e.preventDefault

对于您的 $insert 查询,您还应该删除“.”。在“VALUES”中的每个变量之前和之后。它们不是必需的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    • 2021-11-08
    • 1970-01-01
    • 2013-12-01
    • 2014-01-13
    • 1970-01-01
    • 2018-07-26
    相关资源
    最近更新 更多