![](/img/trans.png)
[英]Unset session in PHP and submit form using onClick with only 1 button
[英]Unset php variables before form submit (post) using jQuery
我有两个变量$get_book_id
和$_GET['book_id']
和一个用于发布其他数据的表单。 单击提交按钮后,我想清空以上两个变量,并使用表单post发布剩余的值。
jQuery代码如下所示。
的index.php
$('#submit_button').click(function(){
$.ajax({
type: 'POST',
url: 'empty_variable.php',
success: function(data) {
},
error: function(ts) { alert(ts.responseText) }
});
});
<form action="process.php" method="post">
.................
</form>
empty_variable.php
<?php
$get_book_id = '';
$_GET['book_id'] = '';
?>
process.php
<?php
echo $_GET['book_id'];
echo $get_book_id;
print_r($_POST);
?>
我想做的是,在表单提交上使用jQuery清空两个变量。 我怎样才能做到这一点 ?
在过程页面中,我仍然看到$_GET
值和$get_book_id
。
请不要让我清空index.php页面中的值。
使用jQuery在表单提交(发布)之前取消设置PHP变量
谢谢
Kimz
更新了答案,请使用发布请求,如果该字段为空,则将该变量设置为空:
将提交输入更改为如下所示的按钮:index.php
<script type="text/javascript">
$(document).ready(function() {
$('#submit_button').on('click', function(e) {
$('#book_id').val('');
$(this).submit();
});
});
</script>
<form id="form_id" action="process.php" method="POST">
<input id="book_id" name="book_id" type="text" />
<button id="submit_button">Submit</button>
</form>
process.php
if($_POST['book_id'] = '') $get_book_id = $_POST['book_id'];
echo $_POST['book_id'];
echo $get_book_id;
print_r($_POST);
尝试这种方式
$('#submit_button').click(function(){
$.ajax({
type: 'POST',
data:{"clearBit":"YES"}, //use the data to refresh the specified variables on process.php page
url: 'empty_variable.php',
success: function(data) {
},
error: function(ts) { alert(ts.responseText) }
});
});
Process.php:
<?php
if(isset($_POST['clearBit']) && $_POST['clearBit']=='YES')
{
$get_book_id = '';
$_GET['book_id'] = '';
}
?>
快乐编码:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.