如何在页面加载时使用ajax提交隐藏表单?

时间:2022-11-24 11:57:11

How can i submit a hidden form to php using ajax when the page loads?

如何在页面加载时使用ajax向php提交隐藏表单?

I have a form with one hidden value which i want to submit without refreshing the page or any response message from the server. How can implement this in ajax? This is my form. I also have another form in the same page.

我有一个带有一个隐藏值的表单我想要提交而不刷新页面或来自服务器的任何响应消息。如何在ajax中实现这个?这是我的表格。我在同一页面上还有另一种表格。

<form id = "ID_form" action = "validate.php" method = "post">
 <input type = "hidden" name = "task_id" id = "task_id" value = <?php echo $_GET['task_id'];?>>
</form>

5 个解决方案

#1


3  

similar to Zafar's answer using jQuery

类似于Zafar使用jQuery的答案

actually one of the examples on the jquery site https://api.jquery.com/jquery.post/

实际上是jquery网站上的一个例子https://api.jquery.com/jquery.post/

$(document).ready(function() {
    $.post("validate.php", $("#ID_form").serialize());
});

you can .done(), .fail(), and .always() if you want to do anything with the response which you said you did not want.

如果你想对你说你不想要的响应做任何事情,你可以.done(),. fail()和.always()。

in pure javascript

在纯JavaScript中

body.onload = function() {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("POST","validate.php",true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send("task_id=" + document.getElementById("task_id").value);
};

#2


1  

I think you have doubts invoking ajax submit at page load. Try doing this -

<script type="text/javascript">
$(document).ready(function(){
    $.ajax({
        "url": "validate.php",
        "type": "post"
        "data": {"task_id": $("#task_id").val();},
        "success": function(){
        // do some action here
        }
    })
})
</script>

#3


1  

If you're using jQuery you should be able to get the form and then call submit() on it.

如果您正在使用jQuery,您应该能够获取表单,然后在其上调用submit()。

E.g.:

var $idForm = $('#ID_form');
$idForm.submit();

#4


1  

Simple solution - jQuery AJAX post the value as others have suggested, but embed the PHP value directly. If you have multiple forms, you can add more key:value pairs as needed. Add a success/error handler if needed.

简单的解决方案 - jQuery AJAX发布其他人建议的值,但直接嵌入PHP值。如果您有多个表单,则可以根据需要添加更多键:值对。如果需要,添加成功/错误处理程序。

<script type="text/javascript">
$(document).ready(function(){
    $.post( "validate.php", { task_id: "<?=$_GET['task_id']?>" } );
})
</script>

#5


1  

As others have said, no need for a form if you want to send the data in the background.

正如其他人所说,如果你想在后台发送数据,则不需要表单。

validate.php

<?php

$task_id = $_POST['task_id'];

//perform tasks//

$send = ['received:' => $task_id]; //json format//
echo json_encode($send);

JQuery/AJAX:

$(function() { //execute code when DOM is ready (page load)//
    var $task = $("#task_id").val(); //store hidden value//
    $.ajax({
        url: "validate.php", //location to send data//
        type: "post",
        data: {task_id: $task},
        dataType: "json", //specify json format//
        success: function(data){
            console.log(data.received); //use data received from PHP//
        }
    });
});

HTML:

<input type="hidden" name="task_id" id="task_id" value=<?= $_GET['task_id'] ?>>

#1


3  

similar to Zafar's answer using jQuery

类似于Zafar使用jQuery的答案

actually one of the examples on the jquery site https://api.jquery.com/jquery.post/

实际上是jquery网站上的一个例子https://api.jquery.com/jquery.post/

$(document).ready(function() {
    $.post("validate.php", $("#ID_form").serialize());
});

you can .done(), .fail(), and .always() if you want to do anything with the response which you said you did not want.

如果你想对你说你不想要的响应做任何事情,你可以.done(),. fail()和.always()。

in pure javascript

在纯JavaScript中

body.onload = function() {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("POST","validate.php",true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send("task_id=" + document.getElementById("task_id").value);
};

#2


1  

I think you have doubts invoking ajax submit at page load. Try doing this -

<script type="text/javascript">
$(document).ready(function(){
    $.ajax({
        "url": "validate.php",
        "type": "post"
        "data": {"task_id": $("#task_id").val();},
        "success": function(){
        // do some action here
        }
    })
})
</script>

#3


1  

If you're using jQuery you should be able to get the form and then call submit() on it.

如果您正在使用jQuery,您应该能够获取表单,然后在其上调用submit()。

E.g.:

var $idForm = $('#ID_form');
$idForm.submit();

#4


1  

Simple solution - jQuery AJAX post the value as others have suggested, but embed the PHP value directly. If you have multiple forms, you can add more key:value pairs as needed. Add a success/error handler if needed.

简单的解决方案 - jQuery AJAX发布其他人建议的值,但直接嵌入PHP值。如果您有多个表单,则可以根据需要添加更多键:值对。如果需要,添加成功/错误处理程序。

<script type="text/javascript">
$(document).ready(function(){
    $.post( "validate.php", { task_id: "<?=$_GET['task_id']?>" } );
})
</script>

#5


1  

As others have said, no need for a form if you want to send the data in the background.

正如其他人所说,如果你想在后台发送数据,则不需要表单。

validate.php

<?php

$task_id = $_POST['task_id'];

//perform tasks//

$send = ['received:' => $task_id]; //json format//
echo json_encode($send);

JQuery/AJAX:

$(function() { //execute code when DOM is ready (page load)//
    var $task = $("#task_id").val(); //store hidden value//
    $.ajax({
        url: "validate.php", //location to send data//
        type: "post",
        data: {task_id: $task},
        dataType: "json", //specify json format//
        success: function(data){
            console.log(data.received); //use data received from PHP//
        }
    });
});

HTML:

<input type="hidden" name="task_id" id="task_id" value=<?= $_GET['task_id'] ?>>