isset和!空为ajax发布变量检索php

时间:2017-12-28 08:47:08

标签: php ajax variables post

我有一个按钮组,由两个按钮组成,每个按钮分配一个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";

单击按钮后显示成功提示,但是&#34; Nope&#34;仍然显示点击后,它没有进入if语句,我似乎无法找到方法,我是ajax的首发,我很感激任何提示或帮助请

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:-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>