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

时间:2015-07-20 20:39:30

标签: javascript php jquery html ajax

点击按钮,我正在使用ajax请求 POST SELECT 数据到我的PHP页面。出于某种原因,我无法将我的PHP IF语句评估为true。它违反了 business_unit_brand = 3 的ELSE条件。我尝试回显并打印$ 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 Ajax

$("#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";
}

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:0)

使用inspect元素查看您的表单发布或未发布的内容。单击网络选项卡,然后选择有问题的文件名,您应该看到那里的操作。

Ctrl + Shift + i