简体   繁体   English

使用Ajax插入Mysql表单php中而无需重新加载页面

[英]Inserting into Mysql form php with Ajax without reload page

I have this jQuery AJAX code that into Mysql form php. 我有将此jQuery AJAX代码转换为Mysql形式的php。 It works without reloading the page. 它无需重新加载页面即可工作。 The problem is that it When the user enters something into the form, then clicks submit, I would like to use php and ajax (with jquery). 问题是,当用户在表单中输入内容,然后单击提交时,我想使用php和ajax(带有jquery)。 But it do not print the string in alert() . 但是它不会在alert()中打印字符串。 Can someone please show me how this can be achieved? 有人可以告诉我如何实现吗?

HTML : HTML:

<form id="students" method="post">
   <div class="row">
      <input name="a[]" value="" type="text" >
      <input name="b[]" value="" type="text"  >
   </div>
   <div class="row">
      <input name="a[]" value="" type="text" >
      <input name="b[]" value="" type="text"  >
   </div>
   <input type="submit" value="submit" id="submitbutton" class="insert"/>
</form>


<script type="text/javascript">
 $('#students').submit(function(){
    event.preventDefault();
    $.ajax({
      type: 'POST',
      url: 'ajax_insert.php',
      data: $('#students').serialize(),
      dataType: 'JSON',
      success: function(data) {
          alert('form has been posted successfully');
      }
    });
 });
</script>

and ajax_insert.php : 和ajax_insert.php:

$a1=$_POST['a'];
$b1=$_POST['b'];

//$query_values = array();
$index=0;

foreach($a1 as $s){
   $sql = "INSERT INTO test_data(a,b) VALUES('$s','".$b1[$index]."')";

   $result = mysql_query($sql);
   if($result)
   {
     echo "1";
   }
   $index++;
}
$('#students').submit(function(event){
    event.preventDefault();
    $.ajax({
    type: 'POST',
    url: 'ajax_insert.php',
    data: $('#students').serialize(),
    dataType: 'JSON',
    success: function(data) {
    alert('form has been posted successfully');
  }
 });

check official document here and learn how to use event.preventDefault(); 在此处查看官方文档,了解如何使用event.preventDefault();

You probably have to event.preventDefault(); 您可能需要event.preventDefault(); in the submit event callback : 在Submit事件回调中:

$('#students').submit(function(){
  event.preventDefault();
  $.ajax({
    type: 'POST',
    url: 'ajax_insert.php',
    data: $('#students').serialize(),
    dataType: 'JSON',
    success: function(data) {
      alert('form has been posted successfully');
    }
  });
});

You need return valid json when use dataType: "json" in $.ajax call 在$ .ajax调用中使用dataType:“ json”时,您需要返回有效的json

Or you can use dataType: "html" without rewriting php code 或者,您可以使用dataType:“ html”而不用重写php代码

Update (examples of code, that should work): 更新(代码示例,应该可以):

in HTML: 在HTML中:

<script type="text/javascript">
  $('#students').submit(function(e){
    e.preventDefault();
    $.ajax({
      type: 'POST',
      url: 'ajax_insert.php',
      data: $('#students').serialize(),
      dataType: 'JSON',
      success: function(data) {
          if(data.result == 1) {
              alert('form has been posted successfully');
          } else {
              alert(data.error);
          }
      }
    });
 });
</script>

ajax_insert.php ajax_insert.php

$a1=$_POST['a'];
$b1=$_POST['b'];

//$query_values = array();
$index=0;
$errors = array();

foreach($a1 as $s){
   $sql = "INSERT INTO test_data(a,b) VALUES('$s','".$b1[$index]."')";

   $result = mysql_query($sql);
   if(!$result)
   {
     $errors[] = "\"$sql\"";
   }
   $index++;
}

if(!empty($errors)) {
   echo json_encode(array('result'=>0,'error'=>"Error executing following queries: \n".implode("\n", $errors)));
} else {
   echo json_encode(array('result'=>1));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM