doubi2228 2012-05-08 03:00
浏览 50
已采纳

用户点击时PHP提交按钮

I will like to get multiple request after clicking submit. Meaning that after i clicked submit and get the result, i will like to inquire results again and click submit again. I don't want to lead myself back to nothingness.

 <?php
 error_reporting(0);
 if (!$_POST['submit'])
 {
 <form action="http://localhost/test.php" method="post">
 Type of Leave: 
 <select name="leave">
 <option value=""selected="selected"></option>
 <option VALUE="Medical Leave"> Medical Leave</option>
 <option VALUE="Unpaid Leave"> Unpaid Leave</option> 
 </select>
 <input type="submit" name="submit" value="Check!" />
 </form>
 <?php
 }
 else
 {
 $conn=odbc_connect("employee","","") or die (odbc_errormsg());
 if (!$conn)  
 {
 exit
 ("Connection Failed: " . $conn);
 }
 else
 {
 $choice = $_POST['leave'];
 $sql="SELECT * FROM balance WHERE ID=$username";
 $rs=odbc_exec($conn,$sql);
 ?>
 <?php
 while (odbc_fetch_row($rs))
 {
 $choice=odbc_result($rs,"$choice");
 echo "<tr><td>$choice</td>";
 }
 odbc_close($conn);
 echo "</table>";
 }
 }

Currently the code itself queries out the expected output that I want, but the only downside is it stays there as an output. What my aim is to have the submit and the select choice option stays there so that I can continue to output the result I want, thanks.

  • 写回答

1条回答 默认 最新

  • duanpu6319 2012-05-08 03:14
    关注

    The best user experience would be to use AJAX to dynamically change the results table depending on what is chosen in the dropdown. For a simpler solution, you could try moving the form up and out of the if/else loop. Put the form first, then after the form prints, check for if ($_POST['submit']).

    In this example, I also added a little function to add selected="selected" to whichever option was chosen for this submit. It's very rough, but you get the idea.

    <?php
    function matchPost($name,$val) {
    if($val == $name)
        echo 'selected="selected"';
    }
    ?>
    
    <form action="http://localhost/test.php" method="post">
     Type of Leave: 
     <select name="leave">
     <option value=""></option>
     <option <?php matchPost('Medical_Leave',$_POST['leave']); ?> VALUE="Medical_Leave"> Medical Leave</option>
     <option <?php matchPost('Unpaid_Leave',$_POST['leave']); ?> VALUE="Unpaid_Leave"> Unpaid Leave</option> 
     </select>
     <input type="submit" name="submit" value="Check!" />
     </form>
    
    <?php
     if ($_POST['submit'])
    
     {
     $conn=odbc_connect("employee","","") or die (odbc_errormsg());
     if (!$conn)  
     {
     exit
     ("Connection Failed: " . $conn);
     }
     else
     {
     $choice = $_POST['leave'];
     $sql="SELECT * FROM balance WHERE ID=$username";
     $rs=odbc_exec($conn,$sql);
     ?>
     <?php
     while (odbc_fetch_row($rs))
     {
     $choice=odbc_result($rs,"$choice");
     echo "<tr><td>$choice</td>";
     }
     odbc_close($conn);
     echo "</table>";
     }
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应