0

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>'; 
6
  • sure...you can use string concatenation to make that value whatever you want. Commented Feb 8, 2014 at 1:34
  • The code at the end looks fine to me. Why is it obviously wrong? Commented Feb 8, 2014 at 1:34
  • How is your option list created? If you send the player could you not derive the rating? If you're trying to rate them, why not send the player in another field? Commented Feb 8, 2014 at 1:36
  • @Barmar because I when I inspect the code when opening it in my browser I get the Rating and The Player name (which is what I want) but the && is included I suspect there is an error with my string concatenation Commented Feb 8, 2014 at 1:39
  • 1
    Yes you can do this, but your need to do this would suggest some flaw in how you are structuring or working with your data. Perhaps you should be using some sort of row ID here instead, assuming you are populating this from a database. Commented Feb 8, 2014 at 1:39

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

Something other than a comma, like && as in his question?
True... A comma was the first thing that came to mind.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.