猿问

Ajax 不通过 POST

我有一个表单,我需要将数据发布到一个 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。


一只名叫tom的猫
浏览 120回答 4
4回答

慕森卡

在 go 函数数据的 ajax 中修改您的数据对象:{"select_1": sel_1, "select_2": sel_2}

慕桂英3389331

function go() {var sel_1 = $('#select_1').val();var sel_2 = $('#select_2').val();$.post("compute.php", {&nbsp; &nbsp; select_1 : sel_1,&nbsp; &nbsp; select_2 : sel_2&nbsp; &nbsp; }, function (answ) {&nbsp; &nbsp; console.log(answ);&nbsp; });}

慕村9548890

尝试将您的 go 函数修改为如下所示:function go() {var sel_1 = document.getElementById('select_1').value;var sel_2 = document.getElementById('select_2').value;$.ajax({&nbsp; &nbsp; method: "post",&nbsp; &nbsp; url: "compute.php",&nbsp;&nbsp; &nbsp; data: {"select_1":sel_1 , "select_2":sel_2},&nbsp; &nbsp; success: function(data,status)&nbsp; &nbsp; &nbsp; /* ------------ for debug I have an alert with a response ---- */&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; alert("passed data are: " + data + "\nStatus: " + status);&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; error: function(data,status)&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; alert("passed data are: " + data + "\nStatus: " + status);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}

慕沐林林

POST 被上面的 .htaccess 指令阻止。已更改,现在 POST 正在从 html 工作,但在使 js/ajax/jquery 工作以将我的数据发布到 compute.php 时仍然存在问题
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答