亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Ajax 不通過 POST

Ajax 不通過 POST

一只名叫tom的貓 2022-07-08 18:29:10
我有一個表單,我需要將數據發布到一個 compute.php 文件。我必須傳遞名為 select_1 和 select_2 的兩個選擇字段的內容。我的 fiire 按鈕有一個 onclick='go()' 調用。我的腳本代碼如下:function go() {var sel_1 = document.getElementById('select_1').value;var sel_2 = document.getElementById('select_2').value;$.ajax({    type: "POST",    url: "compute.php",    data: "select_1=" + sel_1 + "&select_2=" + sel_2,    dataType: "text",    success: function(data,status)      /* ------------ for debug I have an alert with a response ---- */      {        alert("passed data are: " + data + "\nStatus: " + status);      },    error: function(data,status)      {        alert("passed data are: " + data + "\nStatus: " + status);      }    });}在 php 端,我的 compute.php 文件的代碼如下:$selection_1 = $_POST["select_1"];$selection_2 = $_POST["select_2"];$sqladd = "INSERT INTO table SET column_1 = '$selection_1', column_2 = '$selection_2';";if (mysqli_query($conn, $sqladd)) {   $resultadd= mysqli_query($conn, $sqladd);   // ------- then for debug purpose   echo "inserted row with col_1 --->" . $selection_1 . "<--- and col_2 --->" . $selection_2;} else {   // ------- either, still for debug purpose   echo "not inserted. ";   echo "Error: " . mysqli_error($conn);}現在警報框顯示 js 已經正確獲取了兩個選擇字段的值,并且我也返回了成功狀態,但是來自 compute.php 的調試回顯顯示 selection_1 和 selection_2 的空值,并且插入了表格行但是在插入的行中,表列是空的。Apache 日志顯示兩個通知:PHP Notice: Undefined index: select_1 in compute.php和PHP Notice: Undefined index: select_2 in compute.php。所以 PHP 不接收這兩個 POST 值。我究竟做錯了什么?我在 .htaccess 中有以下規則和條件:RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]RewriteRule ^ %1 [R,L,NC]RewriteCond %{REQUEST_FILENAME}.php [NC]RewriteRule ^ %{REQUEST_URI}.php [L]他們可以阻止 POST 嗎?如果是,如何在不阻止 POST 的情況下獲得相同的結果?我需要在外部將 /dir/foo.php 重定向到 /dir/foo 并在內部將 /dir/foo 重定向到 /dir/foo.php。
查看完整描述

4 回答

?
慕森卡

TA貢獻1806條經驗 獲得超8個贊

在 go 函數數據的 ajax 中修改您的數據對象:{"select_1": sel_1, "select_2": sel_2}



查看完整回答
反對 回復 2022-07-08
?
慕桂英3389331

TA貢獻2036條經驗 獲得超8個贊

function go() {

var sel_1 = $('#select_1').val();

var sel_2 = $('#select_2').val();

$.post("compute.php", {

    select_1 : sel_1,

    select_2 : sel_2

    }, function (answ) {

    console.log(answ);

  });

}


查看完整回答
反對 回復 2022-07-08
?
慕村9548890

TA貢獻1884條經驗 獲得超4個贊

嘗試將您的 go 函數修改為如下所示:


function go() {

var sel_1 = document.getElementById('select_1').value;

var sel_2 = document.getElementById('select_2').value;

$.ajax({

    method: "post",

    url: "compute.php", 

    data: {"select_1":sel_1 , "select_2":sel_2},

    success: function(data,status)

      /* ------------ for debug I have an alert with a response ---- */

      {

        alert("passed data are: " + data + "\nStatus: " + status);

      },

    error: function(data,status)

      {

        alert("passed data are: " + data + "\nStatus: " + status);

      }

    });

}


查看完整回答
反對 回復 2022-07-08
?
慕沐林林

TA貢獻2016條經驗 獲得超9個贊

POST 被上面的 .htaccess 指令阻止。已更改,現在 POST 正在從 html 工作,但在使 js/ajax/jquery 工作以將我的數據發布到 compute.php 時仍然存在問題



查看完整回答
反對 回復 2022-07-08
  • 4 回答
  • 0 關注
  • 142 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號