开发者

Having difficulty getting value to post from dynamic dropdown

开发者 https://www.devze.com 2023-04-10 17:09 出处:网络
G\'day, I\'m having a problem getting the right value to post on a form I\'ve made. It is passing the ID but not the name.

G'day, I'm having a problem getting the right value to post on a form I've made. It is passing the ID but not the name. Here's my code:

<td name="tech_userlogin" id="tech_userlogin" align="right" valign="top"><select name="tech_userlogin" id="tech_userlogin" class="db_field_name">

     开发者_JAVA百科   <?php
        $sql="SELECT techID, tech_userlogin FROM technicians";
    $result=mysql_query($sql);

    $options="";

    while ($row=mysql_fetch_array($result)) {

        $techID=$row["techID"];
        $tech_userlogin=$row["tech_userlogin"];
        $options.="<OPTION VALUE=\"$techID\">$tech_userlogin</option>";
    }
    ?>

    <OPTION VALUE="">---Select---
    <?php echo $options; ?>
    </SELECT> 

The form posts to this page:

<?php
include 'sql_connect_R.inc.php';

$id = mysql_real_escape_string($_POST['jobID']); 
$equip = mysql_real_escape_string($_POST['wo_equip']);
$techID = mysql_real_escape_string($_POST['techID']);
$tech_login = mysql_real_escape_string($_POST['tech_userlogin']);

mysql_query("UPDATE work_orders SET wo_equip = UCASE('$equip'), wo_techID = '$techID', tech_userlogin = '$tech_login'
WHERE jobID = '$id'");

mysql_close($con);

I'm very new to PHP/MySQL and it took me a couple of days to get something that would put the tech_userlogin into a dropdown box. That is working, but when it posts the techID comes through on both $techID and $tech_userlogin. Could someone please help me sort this out? Any help would be greatly appreciated. Cheers, Spud


What seems to be happening is that you've named the select box "tech_userlogin", but in the option tags the value is set to the techid. Since the value is what's sent back in the $_REQUEST array, try something like this instead:

while ($row=mysql_fetch_array($result)) {
    $tech_userlogin=$row["tech_userlogin"];
    $options.="<OPTION VALUE=\"$tech_userlogin\">$tech_userlogin</option>";
}

I believe that will give you what you want.


On the form the $row['techID'] is being set to the drop down menu tech_userlogin (Ultimately $_POST['tech_userlogin']). There is a few ways to fix this.

1) POST both values in the value field of the option and seperate on the processing side by using:

 $techArray = explode(',',$string); 
 # $techArray[0] will be either the id or login
 # $techArray[1] will be the other

This is not recommended due to possible security issues.

2) Just past the techID through the dropdown box and have another query to get the user login information on the post page and use it to fill in the tech_userlogin field. Recommended


The reason why it is passing the tech id, is because when you submit a drop down list, the value is the ... well.. value.

If you want to pass the value and the name, you can do it through various means.

The first is to use Javascript (jQuery would be great) and you can bind a change event to the tech_user_login drop down. So when this changes, it can update a hidden input to the contents of the selected item in the list.

The second method would be to store the tech id and the tech logon name in the value of the option - so something like:

$techID=$row["techID"];
$tech_userlogin=$row["tech_userlogin"];
$options.="<OPTION VALUE=\"$techID~~$tech_userlogin\">$tech_userlogin</option>";

Then when the value is posted, you can simply do something like $array_tech = explode("~~",$_POST["tech_userlogin"]); and use $array_tech[0] for the tech id and $array_tech[1] for the tech logon name.

Finally, you can just post the value and look up the tech logon using a second query on the next page.


first check in the value of selected in include 'sql_connect_R.inc.php'; like if(isset( $_REQUEST['tech_userlogin'] )) this will will give you the value of the select box that is <OPTION VALUE=\"$techID\">$tech_userlogin</option>. Note that just provide the value $techID, not the $tech_userlogin. and if you want the result of $tech_userlogin then again run a query with its value that you will get through $techID.

hope this will help you

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号