2016-12-25 70 views
0

我用php和AJAX创建了一个简单的表单。当提交数据时,行被添加到数据库中,但是除非刷新,否则不会显示在页面上。用PHP和AJAX插入MYSQL不起作用

不显示错误。

更新:我在成功中添加的警报现在正在显示。但是,如果没有页面重新加载,数据仍然不会显示。

任何帮助是非常感谢。

AJAX:

$("form#message_form").submit(function() { 

    var form = $(this); 
    var url = "message.php"; // the script where you handle the form input. 

    $.ajax({ 
      type: "POST", 
      url: url, 
      data: $("input.input_styling").serialize(), // serializes the form's elements. 
      dataType: 'html', 
      success: function(data) 
      { 

       alert('Checking if working...'); 

      } 
     }); 

    return false; // avoid to execute the actual submit of the form. 

}); 

HTML:

<form action="" id="message_form" style="text-align:right;margin:0;"> 
    <input class="input_styling" type="text" name="infomation" /> 
    <button name="submit" class="submit_icon" type="submit"> > </button>  
</form> 

message.php(不是真的需要,但以防万一)

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line 

$sql = "INSERT INTO test (id, infomation) 
VALUES ('".$_POST["id"]."','".$_POST["infomation"]."')"; 


if ($dbh->query($sql)) { 

} 
else{ 
echo "Ops! something went wrong..."; 
} 

$dbh = null; 


?> 

回答

0

好吧,我只是跑这和它的工作...我改变的唯一的事情是URL,所以检查你是否正确链接文件。 还有一个反问题 - 你包括JQuery吗?

$("form#message_form").submit(function() { 

    var form = $(this); 
    var url = "http://httpbin.org/post"; // the script where you handle the form input. 

    $.ajax({ 
     type: "POST", 
     url: url, 
     data: $("input.input_styling").serialize(), // serializes the form's elements. 
     dataType: 'html', 
     success: function(data) 
     { 
      alert('Checking if working...'); 
     } 
    }); 

    return false; // avoid to execute the actual submit of the form. 

}); 
0

改变你的JS;

$("form#message_form").submit(function() { 
 

 
    var form = $(this); 
 
    var url = "message.php"; // the script where you handle the form input. 
 

 
    $.ajax({ 
 
      type: "POST", 
 
      url: url, 
 
      data: $("form#message_form").serialize(), // serializes the form's elements. 
 
      dataType: 'html', 
 
      success: function(data) 
 
      { 
 

 
       alert('Checking if working...'); 
 

 
      } 
 
     }); 
 

 
    return false; // avoid to execute the actual submit of the form. 
 

 
});

,如果你只需要 “信息来源” 值;

$("form#message_form").submit(function() { 
 

 
    var form = $(this); 
 
    var url = "message.php"; // the script where you handle the form input. 
 

 
    $.ajax({ 
 
      type: "POST", 
 
      url: url, 
 
      data: "information=" + $("input[name=information]").val(), // Get Information Value 
 
      dataType: 'html', 
 
      success: function(data) 
 
      { 
 

 
       alert('Checking if working...'); 
 

 
      } 
 
     }); 
 

 
    return false; // avoid to execute the actual submit of the form. 
 

 
});