【问题标题】:Impossible to retrieve posted variable via AJAX无法通过 AJAX 检索发布的变量
【发布时间】:2019-08-31 12:08:51
【问题描述】:

我正在尝试使用 Ajax 将变量“id”发布到另一个 php 页面。

在名为“export.php”的第二个 php 页面中,我需要使用此变量来查询 MySql 表并将结果显示在 html 表中。

但是它不起作用似乎在 export.php 上没有设置变量。 你能帮帮我吗?

<script>
$('#btn_report').click(function(){
    if(confirm("Are you sure?"))  {
    var id = [];
        $(':checkbox:checked').each(function(i){
    id[i] = $(this).val();
    alert (id[i]); //this is correctly printed
       });
        if(id.length === 0)    {
       alert("No record selected");
            }
    else   {                        
       $.ajax({                      
        url:'export.php',
        type:'POST',
        data:{id:id},                                               
        });

          }
    }
    else  {
    return false;
    }
});
</script>

export.php

所以问题是“if(isset...”返回 false 并在此代码底部显示 else 警报。

<html lang="en">
  <head>
    <title>Export</title>
  </head>
  <body>
<?php

$connect = mysqli_connect('localhost', 'user', 'pass', 'db' );

    if(isset($_POST["id"])){
        echo 'break1';
        foreach($_POST["id"] as $id) {

            $sel = "SELECT * FROM `user_details` WHERE `Codice prodotto` = $id";
            echo $sel . "<br>";
             $res = mysqli_query($connect, $sel);
                        echo "<table border='1'>
                            <tr>
                        <th></th>
                        <th>Codice prodotto</th>
                        <th>Relazione tecnica</th>
                        <th>Modello offerto</th>
                        <th>Descrizione</th>
                        <th>Immagine</th>
                        <th>Tipologia di Ancoraggio</th>
                        <th>Design</th>
                        <th>Illuminazione</th>
                        <th>FSP</th>
                        <th>DATI TECNICI</th>
                        <th>Dissolvenza delle ombre</th>
                        </tr>";

                        while($row = mysqli_fetch_array($res))
                        {
                        echo '<tr id="' . $row['Codice prodotto'] . '">';
                        echo '<td><input type="checkbox" name="codice[]" class="delete_prodotto" value="' . $row['Codice prodotto'] . '"></td>';
                        echo "<td>" . $row['Codice prodotto'] . "</td>";
                        echo "<td>" . $row['Relazione tecnica'] . "</td>";
                        echo "<td>" . $row['Modello offerto'] . "</td>";
                        echo "<td>" . $row['Descrizione'] . "</td>";
                        echo "<td>" . $row['Immagine'] . "</td>";
                        echo "<td>" . $row['Tipologia di Ancoraggio'] . "</td>";
                        echo "<td>" . $row['Design'] . "</td>";
                        echo "<td>" . $row['Illuminazione'] . "</td>";
                        echo "<td>" . $row['FSP'] . "</td>";
                        echo "<td>" . $row['DATI TECNICI'] . "</td>";
                        echo "<td>" . $row['Dissolvenza delle ombre'] . "</td>";
                        echo "</tr>";
                        }
                        echo "</table>"; 
            }

     }
    else {
        echo "no value";
    }

?>
</body>
</html>

【问题讨论】:

  • 首先,你的 php 应该在任何 html 之前......然后再次尝试代码,让我知道你得到了什么
  • 您很容易受到 sql 注入的影响。请使用准备好的语句。
  • @alithedeveloper 这样吗? 导出' ......
  • @LLJ97 我知道但是有假信息
  • 尝试在你的 ajax 中使用 Type:'POST',method:'POST'

标签: php ajax post


【解决方案1】:

请使用 type 属性代替 ajax 方法中的方法。这是错误的。

$('#btn_report').click(function () {
    if (confirm("Are you sure?")) {
        var id = [];
        $(':checkbox:checked').each(function (i) {
            id[i] = $(this).val();
            alert(id[i]); //this is correctly printed
        });
        if (id.length === 0) {
            alert("No record selected");
        } else {
            $.ajax({
                url: 'export.php',
                type: 'POST',
                data: {id: id},
            });

        }
    } else {
        return false;
    }
});

【讨论】:

  • 我做到了。但什么都没有。如果我在单击按钮后打开 export.php,我只会读取我的“无价值”回声
  • open the export.php.. 是什么意思?
  • 真正打开浏览器标签的意思。我的目标是在页面中显示查询结果。因此,当我单击提交按钮时,我想在新选项卡中打开 export.php 并查看带有我选择的值的 html
  • 那么不要使用ajax,而是使用&lt;form&gt;&lt;/form&gt;,因为您不想发回数据而是在新页面中显示。
  • @Swati 我有一个问题,而不是这个,你如何在评论中突出显示特定文本,例如
    。
【解决方案2】:

尝试在您的 export.php 文件中删除所有 html 代码,因为您只回显以发送回结果,如果您需要添加 html,您必须将其与您的结果连接,然后回显结果。

<?php 
if(isset($_POST["id"])){
    echo 'it works';
}else{
    echo 'not worked';
}
?>

看看你是否在控制台中得到响应,改变你的 ajax 代码进行测试

$.ajax({
    url: 'export.php',
    type: 'POST',
    data: {id: id},
    success: function(response) {
        console.log(response);
    }
});

更多解释 当您回应 ajax 请求时,它会将响应发送回浏览器。 所以你应该做的是准备一个结果字符串,并在该字符串中执行任何类型的过程之后,在响应中存储你需要的所有数据,当你准备好发送响应时,你只需回显该字符串然后退出; 下面我尝试修复您的代码,希望它可以帮助您理解。

<?php

$connect = mysqli_connect('localhost', 'user', 'pass', 'db' );

if(isset($_POST["id"])){

    $result = '';
//      echo 'break1';
    foreach($_POST["id"] as $id) {

        $sel = "SELECT * FROM `user_details` WHERE `Codice prodotto` = $id";
//          echo $sel . "<br>";
        $res = mysqli_query($connect, $sel);
        $result .="<table border='1'>
                                                        <tr>
                                                <th></th>
                                                <th>Codice prodotto</th>
                                                <th>Relazione tecnica</th>
                                                <th>Modello offerto</th>
                                                <th>Descrizione</th>
                                                <th>Immagine</th>
                                                <th>Tipologia di Ancoraggio</th>
                                                <th>Design</th>
                                                <th>Illuminazione</th>
                                                <th>FSP</th>
                                                <th>DATI TECNICI</th>
                                                <th>Dissolvenza delle ombre</th>
                                                </tr>";

        while($row = mysqli_fetch_array($res))
        {
            $result .= '<tr id="' . $row['Codice prodotto'] . '">';
            $result .= '<td><input type="checkbox" name="codice[]" class="delete_prodotto" value="' . $row['Codice prodotto'] . '"></td>';
            $result .= "<td>" . $row['Codice prodotto'] . "</td>";
            $result .= "<td>" . $row['Relazione tecnica'] . "</td>";
            $result .= "<td>" . $row['Modello offerto'] . "</td>";
            $result .= "<td>" . $row['Descrizione'] . "</td>";
            $result .= "<td>" . $row['Immagine'] . "</td>";
            $result .= "<td>" . $row['Tipologia di Ancoraggio'] . "</td>";
            $result .= "<td>" . $row['Design'] . "</td>";
            $result .= "<td>" . $row['Illuminazione'] . "</td>";
            $result .= "<td>" . $row['FSP'] . "</td>";
            $result .= "<td>" . $row['DATI TECNICI'] . "</td>";
            $result .= "<td>" . $row['Dissolvenza delle ombre'] . "</td>";
            $result .= "</tr>";
        }
        $result. = "</table>"; 
    }

}
else {
    $result = "no value";
}

echo $result;
exit;

?>

【讨论】:

  • 我像你写的那样编辑然后编码。当我单击按钮时,我看到我的警报值并且是正确的,我在控制台中没有发生任何事情,然后我打开导出 php 并且显示的回显是“无效”
  • 你在 $.ajax 中成功了吗?
  • 我完全复制了你的 ajax 字符串
  • 我刚刚创建了一个非常基础的 ajax 教程供你理解和学习 ajax。 github.com/UmerAbbas8/Basic-Understanding,如果我们想在浏览器中停留在同一页面上,但又想从服务器获取或发布一些数据,我们只使用 ajax。祝你有美好的一天。
  • 好的,谢谢。我试图更好地解释。我有一个包含数字 id 的表,每行一个。我想通过复选框选择一行,单击一个按钮并打开一个显示所选行 id 的新页面
猜你喜欢
  • 1970-01-01
  • 2019-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-13
  • 1970-01-01
相关资源
最近更新 更多