60740

Assign value to textbox onchange of php while select

Question:

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 <a href="http://jsfiddle.net/TPE9r/5/" rel="nofollow">http://jsfiddle.net/TPE9r/5/</a> 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'

Answer1:

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'].'" />';

Answer2:

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

Answer3:

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!'

Answer4:

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

Recommend

  • Vue.js edit variables in dynamic components
  • Join and filter condition between CRM entities using Odata query
  • PHP associate arrays, multidimension. How do I access them?
  • Possible to use ClientRowTemplate() Kendo UI Grid without building a String?
  • Large Photo Version from contacts in android
  • how to parse soap xml in php? [duplicate]
  • Best way to represent list of XPaths in java
  • Mapping JSON to Java Object return null value
  • Ansible syntax best practice, YAML dictionary (key: value) or equal sign (key=value)?
  • How to read xlsx file in an android application?
  • Formatting with mako
  • Is a .txt file created in VB different than one I'd randomly create?
  • Client side validation mvc dropdown
  • Slicing an SPA into several components and use AngularJS
  • force json_encode to create strings
  • distinct values from multiple fields within one table ORACLE SQL
  • Select inner HTML item in CSS
  • Double-click autofill - dynamic based on adjacent cell
  • how to get data attributes of dynamically generated element
  • Responsive design method for collapsing a div
  • Iron Router: How do I send data to the layout?
  • Building Qt project for C++11 standard
  • how to avoid repetitive constructor in children
  • Keep this build forever option - Jenkins
  • How to define custom class, title, and target in Link Browser for content elements and the new rte_c
  • How do I access an unhandled exception in an MVC Error view?
  • Google Custom Search with transparent background
  • Spark fat jar to run multiple versions on YARN
  • vba code to select only visible cells in specific column except heading
  • ActionScript 2 vs ActionScript 3 performance
  • SVN: Merging two branches together
  • json Serialization in asp
  • How can I remove ASP.NET Designer.cs files?
  • Append folder name and increment by 1 using batch script
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • To Get the radio button value in ruby on rails
  • java string with new operator and a literal