簡體   English   中英

表單中的$ _POST值選擇不起作用

[英]$_POST value from form select not working

單擊按鈕,我使用ajax請求將SELECT數據POST到我的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";
}

PHP期望POST數據采用application/x-www-form-urlencoded格式,而不是JSON。 $.ajax將自動正確編碼對象。 所以改為:

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

使用inspect元素查看您的表單發布或未發布的內容。 單擊網絡選項卡,然后選擇有問題的文件名,您應該看到那里的操作。

Ctrl + Shift + i 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM