gpt4 book ai didi

javascript - 表单选择中的 $_POST 值不起作用

转载 作者:行者123 更新时间:2023-11-30 12:19:24 24 4
gpt4 key购买 nike

单击按钮时,我使用 ajax 请求将 POST 数据发送到我的 PHP 页面。出于某种原因,我无法让我的 PHP IF 语句评估为真。它默认为 business_unit_brand = 3 的 ELSE 条件。我尝试回显和 print_r $brand_bu 变量以查看它包含的内容,但没有成功。

表格:

    <select id="brand_bu" name="selected" class="form-control">

<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>

<span class="input-group-btn">
<button class="btn btn-success" id="submitbu" type="button" tabindex="-1" action=""><span class="glyphicon glyphicon-retweet" aria-hidden="true"></span></button>
</span>

jQuery 技术

$("#submitbu").click(function(event) {
console.log("Chose BU: " + $("#brand_bu").val());

$.ajax({

url: "table.php",
type: "POST",
dataType:'json',
data: JSON.stringify({'bu': $('#brand_bu').val()}),
success: function(data){ console.dir(data); refreshTable(); },
error: function(){alert("Something went wrong, please close the page and re-open.")}
}); });

PHP:

    $brand_bu = $_POST['bu'];

if ($brand_bu == "1"){
$business_unit_brand = "1";
} else if ($brand_bu == "2"){
$business_unit_brand = "2";
} else if ($brand_bu == "3"){
$business_unit_brand = "3";
} else if ($brand_bu == "4"){
$business_unit_brand = "4";
} else if ($brand_bu == "5"){
$business_unit_brand = "5";
} else {
$business_unit_brand = "3";
}

最佳答案

PHP 期望 POST 数据为 application/x-www-form-urlencoded 格式,而不是 JSON。 $.ajax 将自动正确编码一个对象。所以改为:

data: { bu: $("#brand_by").val() },

关于javascript - 表单选择中的 $_POST 值不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31525919/

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