Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用ajax提交表单以更新数据库_Php_Ajax_Forms_Mysqli - Fatal编程技术网

Php 使用ajax提交表单以更新数据库

Php 使用ajax提交表单以更新数据库,php,ajax,forms,mysqli,Php,Ajax,Forms,Mysqli,尝试传递单个隐藏数据以使用ajax更新我的sql。但它不工作,也没有错误消息 Html php 您没有阻止提交事件的默认行为,因此,在发送Ajax请求之前,表单正在提交,页面将重新加载 // Capture the event object argument $("#hidden_form").submit(function(evt){ $.ajax({…}); // Prevent the default behaviour evt.preventDefault(); 我建议您尝

尝试传递单个隐藏数据以使用ajax更新我的sql。但它不工作,也没有错误消息

Html

php


您没有阻止提交事件的默认行为,因此,在发送Ajax请求之前,表单正在提交,页面将重新加载

// Capture the event object argument
$("#hidden_form").submit(function(evt){
  $.ajax({…});
  // Prevent the default behaviour
  evt.preventDefault();

我建议您尝试一下:创建一个正确的表单标记(带有action和method),不要在JS中硬编码

Html

另外,确保在页面中出现表单后执行JavaScript代码。如果您不确定(就在您
xxx.submit(function(){xxx})
line之前),请尝试此操作:

如果你得到0,那就不好了。尝试用以下内容包装此代码:

$(document).ready(function onReady(){
    // your code goes here
});
另外,使用Firebug或Chrome DevTools并查看网络面板:您应该在其中看到您的请求,可能是404错误(找不到页面)或其他原因。

试试这个

HTML


危险:您正在使用并且应该使用。您还容易受到现代API的影响,因为它会使您更容易从中获得。使用开发人员工具来确定是否有任何数据是通过ajax请求发送的,并回显$id以查看是否收到任何DataSucces指示ajax请求已成功。它并不表示您希望在服务器上执行的操作是否成功。您必须从服务器返回更有意义的内容,然后检查数据以确定是显示成功还是显示错误。
$id = $_GET['id_hidden'];

$sql = "UPDATE projectmilestone SET accepted=1 WHERE ID='$id'";

if (mysqli_query($con,$sql)){
    echo "Project Accepted";
} else {
    echo mysqli_error();
}
?>
// Capture the event object argument
$("#hidden_form").submit(function(evt){
  $.ajax({…});
  // Prevent the default behaviour
  evt.preventDefault();
<form id="hidden_form" method="GET" action="milestoneAccept.php">
    <input type="hidden" id="id_hidden" name="id_hidden">
    <input type="submit" value="Accept">
</form>
$("#hidden_form").on('submit', function onSubmitForm(ev){

    var $form = $(this);

    $.ajax({
        // retrieve automatically values from the form itself, don't hardcode them in JavaScript
        type: $form.attr('method')
        url: $form.attr('action'),
        data: $form.serializeArray(),
        success: function(data){
            // use this to see what result you get in your console
            console.log(data);

            $("#add_success").html(data);
            $("#add_err").html("");
        }
    });

    // don't let the browser submit the form itself
    ev.preventDefault();

});
console.log( $("#hidden_form").length );
$(document).ready(function onReady(){
    // your code goes here
});
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<form id="hidden_form">
<input type="text" id="id_hidden" name="id_hidden">
<input type="button" value="Accept" onclick="saveform()">
</form>
<div id="add_success" >#</div>
 <script type="text/javascript">
function saveform() {
    $.ajax({
        type: "GET",
        url: "milestoneAccept.php", 
        data: $("#hidden_form").serialize(), // serializes the form's elements.
        success: function(data){

            $("#add_success").html(data);
            $("#add_err").html("");

        }
    });
} 
</script>
$id = $_GET['id_hidden'];

$sql = "UPDATE projectmilestone SET accepted=1 WHERE ID='$id'";

if (mysqli_query($con,$sql)){
    echo "Project Accepted";
} else {
    echo mysqli_error();
}