简体   繁体   中英

Assign value to textbox onchange of php while select

I'm slowly understanding PHP but this one has me stumped. I am populating a 'select' HTML element with data from PHP using..... while($row = mysql_fetch_array($result)) All is working fine and I understand why. I just wanted to fill in other textboxes on the page when a selection is made. There is a very helpful fiddle at http://jsfiddle.net/TPE9r/5/ but it does not use PHP. This is part of my code...

while($row = mysql_fetch_array($result)){
    echo '<option value=" ',$row['ContactID'],' ">', $row['ContactName'],' -- ',    $row['BusinessName'],
'</option>';}

Sorry about the formatting - new on here also... I have used similar JavaScript to the fiddle but I can't display $row['ContactName'] or $row['BusinessName'] in a textbox. I can do this with a normal HTML select but not when the rows are in a 'while loop'

because you try to do something, what should be done on other way.

In the while loop, you are putting out the options html tags.

To do this, i think, the best practice, if you build an array from your results, so you can use that later, if you want to do it in PHP.

The code is not tested.

echo '<select name="whatever">';
$results = array();
while($row = mysql_fetch_array($result)){
    //Storing all the rows in an array
    $results[] = $row;
}
for($i=0;$i < count($results); $i++) {
    echo '<option value=" ',$results[$i]['ContactID'],' ">', $results[$i]['ContactName'],' -- ',    $results[$i]['BusinessName']."</option>";
}
echo '</select>';
//And now you can youse your $resuls array more then one time.
$key = 0;
//The key is based on, wich data you need.
//Maybe you want to set it in the loop with an if condition
echo '<input type="text" value="'.$results[$key]['ContactName'],' -- ',    $results[$key]['BusinessName'].'" />';

Finally, solved this one - maybe not the best way but it may help others. I used : $(this).find(':selected').data('id') .... in the onchange and used data-attributes in the loop

for($i=0;$i < count($results); $i++) {
echo '<option data-id="'.$results[$i]['ContactID'],' " data-bn="'.$results[$i]['BusinessName'],' " >'. $results[$i]['ContactName']. 

"</option>";

and 3 textboxes whose id is bb, cc, dd ....

price2= this.value;
price1 = $(this).find(':selected').data('id')
price3 = $(this).find(':selected').data('bn')

$('#bb').val(price1);
$('#cc').val(price2);
$('#dd').val(price3);

Just a bit tricky with single and double quotes in the loop but 'Hey that's what we do!'

Trying to post my own answer - having trouble with formatting on here.

The loop should be

for($i=0;$i < count($results); $i++) { echo ''. $results[$i]['ContactName'].

"";

OOPS!!!!! Done it again - the loop code is truncated - don't know why

Use this instead. You do not want the value of the option but the text.

$('#job_title').live('change', function(){    
    var b = $(this).text();           
    $('#catch_value').val(b);
    return false;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM