[英]Pass jQuery variable to PHP using POST method
我有一个名为selectedText
的变量,希望传递给PHP。 我根据哪个变量作为一个案例:
$(function(){
$('#status_users').change(function () {
var selectedText = $(this).find("option:selected").text();
//alert(selectedText);
switch ($('#status_users :selected').text()) {
case 'Active':
$.post("ajax/readRecords_Status.php", {}, function (data, selectedText) {
$(".records_content").html(data);
});
break;
case 'Inactive':
$.post("ajax/readRecords_Status.php", {}, function (data, selectedText) {
$(".records_content").html(data);
});
break;
case 'Try':
$.post("ajax/readRecords_Status.php", {}, function (data, selectedText) {
$(".records_content").html(data);
});
break;
case 'All':
$.post("ajax/readRecords.php", {}, function (data, selectedText) {
$(".records_content").html(data);
});
break;
}
});
});
运行最后一种情况,因为未传递变量并调用了另一个代码
在php上有:
$status_users = $_POST['selectedText'];
$sql="SELECT * FROM `users` where register_status ='".$status_users"'" ;
只需添加以下内容
{selectedText:selectedText}
作为所有请求中的第二个参数。 并从功能中删除第二个参数。
完整代码:
$(function(){
$('#status_users').change(function () {
var selectedText = $(this).find("option:selected").text();
//alert(selectedText);
switch ($('#status_users :selected').text()) {
case 'Active':
$.post("ajax/readRecords_Status.php", {selectedText:selectedText}, function (data) {
$(".records_content").html(data);
});
break;
case 'Inactive':
$.post("ajax/readRecords_Status.php", {selectedText:selectedText}, function (data) {
$(".records_content").html(data);
});
break;
case 'Try':
$.post("ajax/readRecords_Status.php", {selectedText:selectedText}, function (data) {
$(".records_content").html(data);
});
break;
case 'All':
$.post("ajax/readRecords.php", {selectedText:selectedText}, function (data) {
$(".records_content").html(data);
});
break;
}
});
});
检查jQuery $ .post()以获得更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.