【问题标题】:how to post two select box values to php如何将两个选择框值发布到 php
【发布时间】:2015-03-20 06:33:21
【问题描述】:

我在一个表单中有两个选择框。过滤掉从数据库输出的选定值。这是我的代码

html代码

              <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" style="width:100px; height:30px;">
              <input type="date" name="date" id="date_id" style="width:150px; height:30px; margin-top:-29px; margin-bottom:0px; margin-left:0px;">

              <select name="value1" style="width:51px; height:30px;margin-left:161px; margin-top:-29px; margin-bottom:0px;">
              <option value="asc">ASC</option>
              <option value="dsc">DSC</option>
              </select>
              <select name="value2" style="width:149px; height:30px;margin-left:226px; margin-top:-26px;">
              <option value="readytoship">Ready To Ship</option>
              <option value="readytodispatch">Ready To Dispatch</option>
              </select>
              <input type="submit" value="Filter" name="submit" style="margin-left:163px; margin-top:20px; margin-bottom:0px;"/>
              </form>

php代码:

              <?php
              include "db.php";
              if(isset($_POST['submit']))
                        {
                        if(($_POST['value1'] == 'asc') && ($_POST['value2'] == 'readytoship'))
                        {
                            // query to get all Fitzgerald records
                            $query = "SELECT * FROM purna_orders WHERE shipping_status='readytoship' order by po_created_date asc";
                        }
                        elseif(($_POST['value1'] == 'dsc') && ($_POST['value2'] == 'readytoship'))
                        {
                            // query to get all Herring records
                            $query = "SELECT * FROM purna_orders WHERE shipping_status='readytoship' order by po_created_date dsc";
                        } 
                        elseif(($_POST['value1'] == 'asc') && ($_POST['value2'] == 'readytodispatch'))
                        {
                            // query to get all Herring records
                            $query = "SELECT * FROM purna_orders WHERE shipping_status='readytodispatch' order by po_created_date asc";
                        }
                        elseif(($_POST['value1'] == 'dsc') && ($_POST['value2'] == 'readytodispatch'))
                        {
                            // query to get all Herring records
                            $query = "SELECT * FROM purna_orders WHERE shipping_status='readytodispatch' order by po_created_date dsc";
                        }
                        else {
                            // query to get all records
                            $query = "select * from purna_orders where shipping_status='readytoship' order by po_created_date asc ";
                        }
                        $query = "select * from purna_orders where shipping_status='readytoship' order by po_created_date asc ";
                        $result = mysql_query($query);

                        $num_rows = mysql_num_rows($result);
                        if($num_rows >= 1)
                            {

                            echo "<div id='showmenu' class='scroll'>";  

                        echo "<table id='table_struct' cellspacing='0' cellpadding='1' border='1' width='400' height='30'>
                             <tr class='tr_class' bgcolor='white'>
                             <td align='center' style='font-weight:bold'> Select </td>
                             <td align='center' style='font-weight:bold'> Order Id </td>
                             <td align='center' style='font-weight:bold'> Customer Name </td>

                             <td align='center' style='font-weight:bold'> Price </td>

                             <td align='center' style='font-weight:bold'> Pincode </td>
                             <td align='center' style='font-weight:bold'> COD </td>
                             <td align='center' style='font-weight:bold'> Status </td>
                        </tr>";
                            while($row = mysql_fetch_array($result))
                            {
                                    $po_id = $row['po_id'];
                                    $_SESSION['po_id'] = $po_id;
                                    $is_cod =($row['is_cod'])? 'Yes' : ' ';
                                    echo "<tr height='20' data-order_id='".$row['po_id']."'>
                                    <td align='center'><input type='radio' class='case' name='case' value='1'></td>
                                    <td align='center'>".$row['po_id']."</td>
                                    <td align='center'>".$row['customer_name']."</td>
                                    <td align='center'>".$row['order_value']."</td>
                                    <td align='center'>".$row['bill_to_pincode']."</td>
                                    <td align='center'>".$is_cod."</td>
                                    <td align='center'>".$row['shipping_status']."</td>";

                            echo "</tr>";
                                    }

                                    echo "</table>";

                                    echo "</div>";
                                    }
                        }

                        ?>

问题是它直接执行 else 部分而不检查条件。

而且我需要默认显示数据库中的所有记录而不点击过滤器。

【问题讨论】:

  • 使用 echo var_dump($_POST),检查发布数据,然后继续。
  • 您的代码对我有用。它进入正确的状态?
  • 为什么要在所有 if else 条件中编写一个额外的查询。那有什么意思吗?我不认为它有任何意义?它会覆盖您在 if else 条件中写入的查询
  • sry 我写错了

标签: php html sql


【解决方案1】:

您的代码没有检查它是否具有空值。所以检查这种方式并执行操作。

if(!empty($_POST[`value1`]) && !empty($_POST[`value2`])) {
    //write code here
}

【讨论】:

    【解决方案2】:

    将您的 PHP 代码编辑到此

           $shipping_status = 'readytoship';
           $order = 'ASC';
           if (!empty($_POST[`value1`]) {
              $shipping_status = $_POST[`value1`];
           }
           if (!empty($_POST[`value2`]) {
              $order = $_POST[`value2`];
           }
          $query = 'select * from purna_orders where shipping_status="'.$shipping_status.'" order by po_created_date "'.$order.'"';
          //other stuff
    

    和html到这个

    <select name="value1" style="width:51px; height:30px;margin-left:161px; margin-top:-29px; margin-bottom:0px;">
                  <option value="asc">ASC</option>
                  <option value="desc">DSC</option>
                  </select>
    

    【讨论】:

    • 如果我给出类似 dis,它如何检查下拉列表中的每个值??
    • 我已经编辑了我的答案,请检查是否理解我在做什么。
    【解决方案3】:

    你需要检查一下

    if($_POST[`value1`] != "") && ($_POST[`value2`] != "")) {
        //Something your query should be here
    }
    

    【讨论】:

      【解决方案4】:

      您的查询超出了 if else 条件

      $query = "select * from purna_orders where shipping_status='readytoship' order by po_created_date asc ";
      

      删除它应该可以工作

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-26
        • 2017-07-02
        • 1970-01-01
        • 1970-01-01
        • 2013-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多