【问题标题】:isset and !empty for ajax posted variable retrieval phpisset 和 !empty 用于 ajax 发布的变量检索 php
【发布时间】:2017-12-28 08:47:08
【问题描述】:

我有一个由两个按钮组成的按钮组,每个按钮都分配了一个 id 和一个值:

echo "<div style='margin-top:20px;' class='btn-group'>
    <button type='button' class='btn btn-primary' id='btnconventional' value='conventional' style=' border-radius: 3px;'>Conventional Units</button>
    <button type='button' class='btn btn-primary' id='btnsi' value='si' style=' border-radius: 3px;'>SI Units</button>
</div>";

然后我想根据将通过此 ajax 脚本发送的按钮的值提交查询和表格结果:

<script>    
    $("#btnconventional").click(function(e) {
    e.preventDefault();
    $.ajax({
        type: "POST",            
        data: { 
            Metric: $('#btnconventional').val(), // < note use of 'this' here                
        },
        success: function(result) {
            alert('Viewing conventional units');
        },
        error: function(result) {
            alert('error');
        }
    });
});
</script>

现在,当我尝试获取变量并运行我的代码时,除了 else 语句之外什么都没有出现:

if(isset($_POST['Metric']) && !empty($_POST['Metric'])){
    $sql2 = "Select DISTINCT Record_id,Test_Group,Test_Name,Result,subtests.Units from tests,medicalrecords,subtests WHERE medicalrecords.CommonID=".$comid." and medicalrecords.Subtest_id IS NOT NULL AND medicalrecords.Subtest_id=subtests.Subtest_id AND subtests.Test_id=tests.Test_id and tests.Test_Group='CBC'"; 
    $result2 = $conn->query($sql2);
    echo $_POST['Metric'];
    echo "<table style='margin-top:10px;' class='table table-hover table-striped table-bordered table-condensed' width='100%' cellspacing='0'>
        <thead>
            <tr bgcolor='#d3d3d3'>
                <th style='text-align:center;'>CBC</th>
                <th style='text-align:center;'>Result</th>
            </tr>
        </thead>";
        while ($row2 = mysqli_fetch_array($result2)) {
            echo "<tr>
                <td style='text-align:center;'>".$row2['Test_Name']."</td> 
                <td style='text-align:center;'>".$row2['Result']." ".$row2['Units']."</td>                                     
            </tr>";
        }
        echo "<tfoot>
            <td></td>
            <td></td>
        </tfoot>
    </table>";
    echo "<button onclick='hideLabResult();'>Back</button>";
}
else echo "Nope";

单击按钮后显示成功警报,但单击后仍显示“否”,它没有进入 if 语句,我似乎找不到方法,我是 ajax 的初学者,我非常感谢任何提示或帮助。

【问题讨论】:

  • 尝试将 Metric 放在引号内,例如 "Metric": $('#btnconventional').val(),
  • 不幸的是,这不起作用,但我确信它不带引号也有效,因为我已经在不同的页面上尝试过类似的请求,但我已经在页面顶部提取了结果请求方法发布并在其中运行查询并运行,但现在我不想只在该范围内完成它
  • $comid 设置在哪里?你也对 SQL 注入持开放态度。
  • @Lalati 你把你的脚本标签放在哪里了?
  • @LawrenceCherone 在这个阶段我并不关心 SQL 注入,我知道我必须使用准备好的语句,并且 $comid 在我的页面开头通过 url 检索,因为它是发送的从另一个页面获取,我认为它与问题无关,我尝试明确指定它但仍然是同样的问题

标签: php ajax variables post


【解决方案1】:

因此,在成功时,您只注册以显示有关成功请求的信息,而不是其内容。要显示其内容,请在 jquery 示例中使用 $('.btn-group').html(result);。

【讨论】:

  • 你好,我试过了,它返回了从第一个选项卡开始的所有页面,所以现在我将解决指定我的 if 中的所有内容,而不是“结果”,但在不同的页面上我能够使用请求方法 post {} 检索页面顶部的变量,但我想超出该范围
【解决方案2】:

好吧,当我在本地运行代码时,我发现 ajax 没有将请求发送到正确的页面。当我输入 url 时,所有 html 代码(我的意思是整个页面)都会显示成功消息。然后我在页面顶部写 isset 条件,并在 } 的结尾放置 exit()。 现在它的工作。

 <?php
if(isset($_POST['Metric']) && !empty($_POST['Metric'])) 
 {
  $sql2 = "Select DISTINCT Record_id,Test_Group,Test_Name,Result,subtests.Units from tests,medicalrecords,subtests WHERE medicalrecords.CommonID=".$comid." and medicalrecords.Subtest_id IS NOT NULL AND medicalrecords.Subtest_id=subtests.Subtest_id AND subtests.Test_id=tests.Test_id and tests.Test_Group='CBC'  "; 
  $result2 = $conn->query($sql2);
  $html = $_POST['Metric'];

  $html.= "<table style='margin-top:10px;'  class='table table-hover table-striped table-bordered table-condensed' width='100%' cellspacing='0'>
<thead>
   <tr bgcolor='#d3d3d3'>

    <th style='text-align:center;'>CBC</th>

    <th style='text-align:center;'>Result</th>
</tr>
</thead>";

 while ($row2 = mysqli_fetch_array($result2)) {

   $html.= "<tr>

        <td style='text-align:center;'>".$row2['Test_Name']."</td> 

        <td style='text-align:center;'>".$row2['Result']." ".$row2['Units']."</td>                                     
    </tr>"; 


  }
 $html .="<tfoot>

 <td></td>
 <td></td>
  </tfoot>
 </table>"; 

  $html .= "<button onclick='hideLabResult();'>Back</button>";
   echo $html;
  } else echo "Nope"; ?>


<script>
$("#btnconventional").click(function(e) {
e.preventDefault();
$.ajax({
    type: "POST",

    data: { 
        Metric: $('#btnconventional').val(), // < note use of 'this' here

    },
    url : '', // put your page url
    success: function(result) {
        alert('Viewing conventional units'+result);
    },
    error: function(result) {
        alert('error');
    }
 });
}); 
</script>

【讨论】:

  • 伙计,如果你想编辑代码,请,但这里有些,请尽量不要这样写,这会给以后的读者带来很多问题
  • Priyanka,如果我们不指定 url,它将把它作为当前页面,我需要,我已经尝试按照你的建议指定它,但它仍然是一样的
  • 是的,但仍然在我的本地,它没有发送到那里。所以当我输入网址时,它对我有用:)
  • Lalit,你检查了检查元素内的请求网址吗?
  • 您指定的结果是 if 语句中正在执行的操作?你能编辑代码并发表你的声明吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-20
  • 1970-01-01
  • 2015-10-14
  • 1970-01-01
  • 2011-11-03
相关资源
最近更新 更多