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 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致
  • ¥15 禅道二次开发编辑版本,上传不了发行包