2017-04-08 96 views
0

選擇框的文本我有選擇框在PHP和希望的文本選擇的項目:獲取PHP頁面

<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);} 
    } 
} 
?> 

我想要得到的選擇框的文本$name_insert代碼。這段代碼得到了這個價值。

+0

你只需要'$ 2行[ '名稱' ]'?或者在表單提交後同時使用'$ row2 ['id']'和'$ row2 ['name']'? –

+0

我需要他們兩個 – aynaz

回答

1

您可以使用javascript並將選定的選項文本保存在隱藏字段中。

<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']; 

使用此代碼替換代碼

<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);} 
    } 
} 

?> 
+0

不工作此代碼 – aynaz

+0

請根據示例更新您的代碼,然後我將檢查 – shubham715

+0

並且還添加html代碼 – shubham715