YaoRaoLov 2020-04-21 10:01 采纳率: 50%
浏览 118

Ajax无法通过POST

I have a form and I need to POST data to a compute.php file. I have to pass the content of two select fields, named select_1 and select_2. My fiire button has a onclick='go()' call.

My script code is as follows:

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 + "
Status: " + status);
      },
    error: function(data,status)
      {
        alert("passed data are: " + data + "
Status: " + status);
      }
    });
}

On the php side my compute.php file has code as follows:

$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);
}

Now the alert box shows that js has correctly got the values of the two select fields, and I also get back a success status, but my debug echo from compute.php shows empty values for selection_1 and selection_2, and the table row is inserted but in the inserted row the table columns are empty. Apache log shows two notices: PHP Notice: Undefined index: select_1 in compute.php and PHP Notice: Undefined index: select_2 in compute.php. So PHP doesn't receives the two POST values. What am I doing wrong? I have the following rules and conditions in .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]

Can they block POST? If yes, how can I get the same result without blocking POST? I need externally redirect /dir/foo.php to /dir/foo and to internally redirect /dir/foo to /dir/foo.php.

  • 写回答

5条回答 默认 最新

  • weixin_33701251 2020-04-21 10:06
    关注

    Try modifying your go function to be like this:

    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 + "
    Status: " + status);
          },
        error: function(data,status)
          {
            alert("passed data are: " + data + "
    Status: " + status);
          }
        });
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器