Marilee
Marilee

Reputation: 1598

PHP send multiple values with option tag

Sorry for the NOOB question, but hey im learning and have found stackoverflow to be of tremendous help to me, so first of all thank you to every contributor on this site

The Question

As the questions title suggest I want to know if it is possible to send multiple variable values from PHP in the Select option tag

Example

Say I have something like this

 echo '<option value="'.$row['Rating'].'">'.$row['Player'].'</option>'; 

Is it possible to include $row['Player'] in the value field TOGETHER with $row['Rating'] which is allready in the value field?

I.E.

Can I do something like this?
Obviously the code below is wrong, but just serves as an examply of what I am trying to do

 echo '<option value="'.$row['Rating'].'&&'.$row['Player'].'">'.$row['Player'].'</option>'; 

Upvotes: 0

Views: 1515

Answers (2)

Anonymous
Anonymous

Reputation: 12017

You can set the value to:

$row['Rating'].'&&'.$row['Player']

Then, when you get the data, use:

$both = explode('&&',$_POST['select_name']);

And $both will be an array with both values. Obviously, you would need to replace select_name with the actual name of the select though.

Then, if you wanted a third value, you could go ahead and add that too:

$row['Rating'].'&&'.$row['Player'].'&&'.$row['other']

Keep in mind that this assumes that there are no && in your variables. If there will be &&, you will need to use another symbol or symbols to separate the values.

Upvotes: 0

tomirons
tomirons

Reputation: 554

You could send values like that, but it would be one big value. You would have to explode() the value if you want to use it in other code.

For example:

echo '<option value="'.$row['Rating'].'&&'.$row['Player'].'">'.$row['Player'].'</option>';

Comes out to be:

<option name="test" value="24,Hulu">Hulu</option>

If you wanted to use a form and POST this it would look something like:

<?php
 $result = $_POST['test']";
 $value = explode("&&", $result);
 print_r($value);
?>

The output of that is:

Array ( [0] => 24 [1] => Hulu ) 

Then you can use that array for other parts.

Upvotes: 1

Related Questions