I'm not sure I understand how ajax works even though I read a lot about it. I want to run the following php if a button is clicked, without loading the page:
我不确定我是否理解ajax是如何工作的,尽管我读了很多关于它的文章。如果单击按钮,我想运行以下php,而不加载页面:
unset($checkout_fields['billing']['billing_postcode']);
So I put the following:
所以我写了如下内容:
jQuery(document).ready(function($) {
$('.keep-buying-wrapper').click(function(){
$.ajax({
url: "url-to-the-script.php",
method: "POST",
data: {'checked': checked},
success: alert('success!'),
});
});
});
And in my php script:
在我的php脚本中:
if( $_POST['checked'] == 'checked' ){
unset($checkout_fields['billing']['billing_postcode']);
}
However nothing happen. even though the success alert is popping, POST['checked']
is null.
但是没有发生。即使成功警报是弹出的,POST['checked']也是空的。
Is the ajax supposes to trigger the php script?
What if I want to send some variable to functions.php
?
ajax假设会触发php脚本吗?如果我想将某个变量发送到函数。php呢?
1 个解决方案
#1
3
The problem is that you need to serialize post data first.
问题是您需要首先序列化post数据。
HTML code (The id and name of checkbox is "billing_postcode"
):
HTML代码(复选框的id和名称为“billing_postcode”):
<input type = "checkbox" id = "billing_postcode" name = "billing_postcode">
JS Code
JS代码
$(document).on('click', '#billing_postcode', function (event) {
var data = $("#billing_postcode").serializeArray();
$.ajax({
url: "url-to-the-script.php",
method: "POST",
data: data,
success: alert('success!'),
})
});
You will get value in post array on server side and enter code here
in php script:
您将在服务器端获取post数组的值,并在这里输入php脚本中的代码:
if($_POST['billing_postcode'])
unset($checkout_fields['billing']['billing_postcode']);
#1
3
The problem is that you need to serialize post data first.
问题是您需要首先序列化post数据。
HTML code (The id and name of checkbox is "billing_postcode"
):
HTML代码(复选框的id和名称为“billing_postcode”):
<input type = "checkbox" id = "billing_postcode" name = "billing_postcode">
JS Code
JS代码
$(document).on('click', '#billing_postcode', function (event) {
var data = $("#billing_postcode").serializeArray();
$.ajax({
url: "url-to-the-script.php",
method: "POST",
data: data,
success: alert('success!'),
})
});
You will get value in post array on server side and enter code here
in php script:
您将在服务器端获取post数组的值,并在这里输入php脚本中的代码:
if($_POST['billing_postcode'])
unset($checkout_fields['billing']['billing_postcode']);