2017-09-26 157 views
0
[enter image description here][1]  **add.php** 
I am not sure that my data is inserted through ajax or not, can anyone solve my issue. 

这是我add.php文件我正在插入数据库中的数据,首先我已经包含在该文件中的数据库连接文件,使用POST方法使用mysqli_real_escape_string提交数据从字符串去除特殊字符。检查空白字段,链接到上一页,显示成功消息。只在php中通过AJAX在数据库中插入数据。

 <html> 
<head> 
    <title>Add Data</title> 


</head> 

<body> 
<?php 
//including the database connection file 
include_once("config.php"); 

    $name = $_POST['name']; 
    $age = $_POST['age']; 
    $email = $_POST['email']; 

    // checking empty fields 
    if(empty($name) || empty($age) || empty($email)) { 

     if(empty($name)) { 
      echo "<font color='red'>Name field is empty.</font><br/>"; 
     } 

     if(empty($age)) { 
      echo "<font color='red'>Age field is empty.</font><br/>"; 
     } 

     if(empty($email)) { 
      echo "<font color='red'>Email field is empty.</font><br/>"; 
     } 

     //link to the previous page 
     echo "<br/><a href='javascript:self.history.back();'>Go Back</a>"; 
    } else { 
     // if all the fields are filled (not empty) 

     //insert data to database 
     $result = mysqli_query($mysqli, "INSERT INTO users(name,age,email) VALUES('$name','$age','$email')"); 



     //display success message 
     //echo "<font color='green'>Data added successfully."; 
     //echo "<br/><a href='index.php'>View Result</a>"; 

} 
?> 
</body> 
</html> 




     **add.html** 
     In this file i have included all my js file creating a small table for name, age, email. my method is post. i am unable to do this task first time i am using ajax without ajax i insert data many times. 
    I am not sure that my data is inserted through ajax or not, can anyone solve my issue. 


     <html> 
<head> 
    <title>Add Data</title> 
    <script src="style/jquery-3.2.1.js"></script> 
    <script src="style/insert.js"></script> 

</head> 

<body> 
    <a href="index.php">Home</a> 



    <br/><br/> 
    <p id="alert"><span id="show"></span></p> 

     <table align="center" width="25%" border="0"> 
      <tr> 
       <td>Name</td> 
       <td><input type="text" name="name" id="name"></td> 
      </tr> 
      <tr> 
       <td>Age</td> 
       <td><input type="text" name="age" id="age"></td> 
      </tr> 
      <tr> 
       <td>Email</td> 
       <td><input type="text" name="email" id="email"></td> 
      </tr> 
      <tr> 
       <td></td> 
       <td><input type="submit" name="Submit" id="submit" value="Add"></td> 
      </tr> 


    </table> 
</body> 
</html> 






     **insert.js** 
     In this file save input value in variable, my type is POST url is add.php, success function is result, my data is inserted but i am not sure that is this through AJAX or not. 
    I am not sure that my data is inserted in database through ajax or not, can anyone solve my issue. 



     $(document).ready(function(e) { 
    $('#submit').click(function() 
    { 

     var name = $('#name').val(); //save input value in variable 
     var age = $('#age').val(); 
     var email = $('#email').val(); 


     $.ajax({ 
      data :{name:name,age:age,email:email}, 
      url :"add.php", //php page URL where we post this data to save in database 
      type :'POST', 
      success: function(data){ 
       alert("Form submitted"); 
       $('#name').val(""); 
       $('#age').val(""); 
       $('#email').val(""); 

      } 
     }) 
    }); 
}); 

回答

1

您的数据是使用简单的PHP插入的而不是通过ajax。 因为用(“#name”)在ajax中获得输入值意味着输入id,但是您没有在输入字段中定义id属性。

+0

我在我的代码中做了很多更改,您的建议和我的承诺可以再次看到我的代码,我在做错误我已附加调试器截图。 –

1

这应该工作:

$(document).ready(function() { 
    $('#submit').click(function(event){ 
     event.preventDefault(); 

     $.ajax({ 
      type:'POST', 
      data: $(this).serialize(), 
      url:"add.php", //php page URL where we post this data to save in database 
      success: function(result){ 
       $('#alert').show(); 
       $('#show').html(result); 
      } 
     }) 
    }); 
}); 
  • 那对preventDefault额外的通话将 停止正常的回发发生(这将是你的提交按钮的正常行为,无论JS事件处理程序)。
  • 此外,您正在尝试使用您的标记中不存在的标识 来检索您的字段 - 您需要在文本框中使用id="name"(对于 实例)才能正常工作。但是更简单的方法来获取所有表单数据只需使用jQuery的serialize() 方法,就像我上面演示的那样。
  • 最后你有一个语法错误(应该是$.ajax而不是$ajax)。

同时,一个简单的方法来判断它是一个Ajax请求与否是是否显示或不显示您的警报。如果不是,页面是否做了完整的回传?或者是否存在某种Ajax错误(在浏览器的开发人员工具 - 控制台和网络选项卡中)?也许在网上关于调试JavaScript和Ajax的教程,它可能会帮助你。

P.S.使用mysqli_real_escape_string并不完全安全,以防止SQL注入攻击等。最好的解决方案是使用预准备语句和参数化查询。 http://bobby-tables.com解释了风险,并告诉你如何使用PHP和mysqli安全地编写查询。

+0

先生,我曾经提过你先生,但无法看到警戒框。如何在提交按钮上看到警告框 –

+0

如果您无法看到它,请按照我的建议进行操作,并在浏览器的开发人员工具中查看控制台或网络选项卡中的任何错误消息。你从add.php得到什么回应? BTW Add.php不应该返回'',''标签等 - 您不能在另一个HTML文档中插入HTML文档。删除这些。它应该只返回HTML片段,它可以进入你的“显示”范围内。 – ADyson

+0

我更新了我的代码,现在我正在处理上面提到的代码,你能再次看到我的代码,我在做错误,我附上了调试器截图。 –

相关问题