Option value to display field 2

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
Helikaon
New php-forum User
New php-forum User
Posts: 9
Joined: Thu Feb 23, 2012 9:18 am
Location: Wolverhampton

Fri May 04, 2012 9:32 am

Hello all,

I have a option value drop down box populated from mysql DB table returning one field which works great however, I have added another field to my query and would like to display the value of that field in <span> tags for example when a selection is made from the drop down box.

I can display both values in the option box with a separator as shown below but struggling to print the second field as a label.

Example code:

while($row = mysql_fetch_array($result)) {
$col1 = $row['surname'];
$col2 = $row['name'];

$display .= "<option value =".$col1.">".$col1."-".$col2."</option>";
}

echo $display;

Any help will be much appreciated. Thanks
User avatar
Nullsig
php-forum GURU
php-forum GURU
Posts: 979
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Fri May 04, 2012 12:42 pm

So you have a drop down like this:

Code: Select all

<select name='fullname' onchange='loadSpan('nameSpan', this.value);'>
<option value='John|Smith'>John Smith</option>
<option value='Jane|Doe'>Jane Doe</option>
<option value='Dexter|Morgan'>Dexter Morgan</option>
</select>
The onchange event will fire and change the content of a desired span.

Code: Select all

<script type='text/javascript'>
function loadSpan(id, value){
      var valArray = value.split("|");//splits string into an array on a delimiter

      ///Now grab the element and populate it with the choice
      document.getElementById(id).innerHTML = valArray[0] + " " + valArray[1]; 
}
</script>
Helikaon
New php-forum User
New php-forum User
Posts: 9
Joined: Thu Feb 23, 2012 9:18 am
Location: Wolverhampton

Fri May 04, 2012 1:29 pm

Thanks Nullsig I appreciate the help however as this is a PHP forum I was hoping for a PHP solution.

Anyone have any PHP implementations I can try?
User avatar
minimihi
php-forum GURU
php-forum GURU
Posts: 236
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

Sat May 05, 2012 5:36 am

You seem to miss the point about what PHP is.
From PHP code a HTML is generated and is passed to browser.
When HTML is sent to browser - PHPs work is done.
If you want to handle events on browser, you have to use different language.

DOM events
Helikaon
New php-forum User
New php-forum User
Posts: 9
Joined: Thu Feb 23, 2012 9:18 am
Location: Wolverhampton

Sat May 05, 2012 11:51 pm

Yes I know that and condescending comments don't really help anyone.
User avatar
Nullsig
php-forum GURU
php-forum GURU
Posts: 979
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Mon May 07, 2012 4:58 am

The problem with a PHP based solution in this specific situation is that is would require a full page refresh to complete the process.

PHP requires that you talk to the server and have it process the request and then the server just returns a new page to you.

You could feasibly do an AJAX based solution here where you are calling out to a PHP script and altering the current page with a partial refresh but that will require more JavaScript and more PHP coding than the solution I have given you.


The one major thing I would say to anyone who is messing around with web design is that PHP is not a catch all solution. A lot of coders that learn on VB or C++ or any other executable language get "tunnel vision". This is because the other programming languages don't associate with others.

PHP is just a tool in web design. You have to know when to best use it. Sometimes JavaScript is the better solution. Sometimes you absolutely have to use a Database and MySQL comes into play. There is no point in over complicating a spec if you don't have to though.

Could you explain why this solution needs to be strictly in PHP?
User avatar
minimihi
php-forum GURU
php-forum GURU
Posts: 236
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

Mon May 07, 2012 2:51 pm

:offtopic:
Helikaon wrote:Yes I know that
No you don't.

"you have to use different language."
So that wasn't helpful? Even after Nullsig gave you JavaScript exampled and you, in response, requested for PHP solution.

Thank goddess Nullsig made short story long.
Helikaon
New php-forum User
New php-forum User
Posts: 9
Joined: Thu Feb 23, 2012 9:18 am
Location: Wolverhampton

Tue May 08, 2012 1:14 am

Thanks for the help Nullsig I have implemented a javascript solution as you suggested.

Minimihi, I appreciate your assistance and yes your comments were useful although a little condescending.
No hard feeelings.

Thanks again guys.
User avatar
Nullsig
php-forum GURU
php-forum GURU
Posts: 979
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Tue May 08, 2012 4:16 am

Thank goddess Nullsig made short story long.
It would be Thank God... I am a man.
Post Reply