dongpi2503 2017-04-08 05:27
浏览 34
已采纳

在php页面中获取选择框的文本

I have select box in PHP and want text selected item of that:

<form action="" method="get" target="" dir="rtl" class="w3-container">
  <?php  
 echo " <select id='p1' name='p1'  >";
 echo "<option  value='*'>Choose person</option>";
sql code

 $result2 = mysql_query($query2,$con);
 while($row2 = mysql_fetch_array($result2))
 {
$row2['name'];
echo "<option  value='".$row2['id']."'>".$row2['name']."</option>"; 
}
echo " </select>";
 mysql_close($con);
 ?>

<input  class="w3-btn w3-hover-yellow" name="" type="submit" value="جستجو">
</form>

<?php
if(isset($_GET['p1']) and $_GET['p1'] != "*")
{


    if ($name_insert=trim($_GET["p1"]))
{    
        if ($strWhere == ""){
        $strWhere .= "name_insert='".trim($name_insert)."' ";
        $getPage .= "&name_insert=".($name_insert);}
        else{
        $strWhere .= " and name_insert='".trim($name_insert)."' ";
        $getPage .= "&name_insert=".($name_insert);}
    }
}
?>

I want code that get text of select box in $name_insert. This code gets value of that.

  • 写回答

1条回答 默认 最新

  • duanguanzai6181 2017-04-08 05:30
    关注

    You can use javascript and save selected option text in a hidden field.

    Example

    <form  action=""  method="POST">  
        <select id="test" onchange="document.getElementById('text_content').value=this.options[this.selectedIndex].text">
         <option value="1">Test One</option>
         <option value="2">Test Two</option>
        </select>
    
    <input type="hidden" name="test_text" id="text_content" value="" />
    </form>
    

    PHP

    $getOptionText = $_POST['test_text'];
    

    Replace your code with this code

    <form action="" method="get" target="" dir="rtl" class="w3-container">
    
     <select id='p1' name='p1'  onchange="document.getElementById('text_content').value=this.options[this.selectedIndex].text">
     <option  value='*'>Choose person</option>
    <?php
     $result2 = mysql_query($query2,$con);
     while($row2 = mysql_fetch_array($result2))
     { 
    echo "<option  value='".$row2['id']."'>".$row2['name']."</option>"; 
    }
    mysql_close($con);
     ?>
    </select>
     <input type="hidden" name="test_text" id="text_content" value="" />
    
    <input  class="w3-btn w3-hover-yellow" name="" type="submit" value="جستجو">
    </form>
    
    <?php
    if(isset($_GET['p1']) and $_GET['p1'] != "*")
    {
       $getValueOfText =  $_GET['test_text'];
       echo $getValueOfText;
    
        if ($name_insert=trim($_GET["p1"]))
    {    
            if ($strWhere == ""){
            $strWhere .= "name_insert='".trim($name_insert)."' ";
            $getPage .= "&name_insert=".($name_insert);}
            else{
            $strWhere .= " and name_insert='".trim($name_insert)."' ";
            $getPage .= "&name_insert=".($name_insert);}
        }
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试