gpt4 book ai didi

php - 在 php 中存储 jquery.ajax POST 数据

转载 作者:行者123 更新时间:2023-11-30 18:21:32 25 4
gpt4 key购买 nike

这是我正在尝试做的事情。

我有以下脚本,它将在同一页面(index.php)上从 index.php 发布数据:

<script type="text/javascript">

function loadabc(){
var optionValue = jQuery("select[name='citycountrystores']").val();
jQuery.ajax({
type: "POST",
url: "index.php",
data: ({result: optionValue}),
success: function(response){
jQuery("#cityAjax").html(response);
jQuery("#cityAjax").show();
}
});

我想在 php 中访问 POST 数据。php 语句是用相同的 javascript 编写的。我需要在 php if 语句中访问 $_POST['result'],例如:

<?php
if($check==$_POST['result'])
{
code....
.........
}
?>
}//Function loadabc end
</script>

这是我的选择框

<select id="citycountrystores" name="citycountrystores" onchange="loadabc();">
<option value="76">City1</option>
<option value="77">City2</option>
<option value="78">City3</option>
</select>

我可以在控制台中看到 POST 值(76 或 77 或 78)。但我想知道如何将它存储在 php 变量中以便立即访问。

谁能告诉我如何做到这一点,或者任何其他实现这一目标的方法。

最佳答案

如果你想从 AJAX 访问相同的页面,你需要在它打印页面的其余部分之前结束你的脚本,代码的第一行应该是:

<?php
if(isset($_POST['result']) && ($_POST['result'] != '')){
if($_POST['result'] == 77){
echo "My selected code: ".$_POST['result'];
} else {
echo "something else";
}
die();
}
?>

关于php - 在 php 中存储 jquery.ajax POST 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11949912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com