Formulario desplegable seleccionado baed en URL - variable de paso

Tengo este codigo:

<label for=subject accesskey=S>Subject</label>
<select name="subject" type="text" id="subject">
<option value="a dog">Dog</option>
<option value="a cat">Car</option>
<option value="a tree">Tree</option>
</select>

If I wanted to preselect a selection via the URL,

Would it not be:

http://xxx.com/contactus.php?subject=a cat

I have tried the above and it does not work.

I have had a look around and there are posts about adding Java Script. I wish to avoid that if possible.

preguntado el 02 de febrero de 12 a las 10:02

well the space must be encoded with %20 ? -

I tired that as http://xxx.com/contactus.php?subject=a%20cat. No go -

My mistake you cannot simply fill the values to from by passing in the URL. -

1 Respuestas

If you url is http://xxx.com/contactus.php?subject=a cat

Puede usar el $_GET['subject'] to get to the value of subject.

Entonces algo como:

<label for=subject accesskey=S>Subject</label>
<select name="subject" type="text" id="subject">
<option value="a dog" <?php echo ($_GET['subject'] == 'a dog' ? 'selected="selected"' : ''); ?>>Dog</option>
<option value="a cat" <?php echo ($_GET['subject'] == 'a cat' ? 'selected="selected"' : ''); ?>>Car</option>
<option value="a tree" <?php echo ($_GET['subject'] == 'a tree' ? 'selected="selected"' : ''); ?>>Tree</option>
</select>

Dont forget to use urlencode on your values and strip them before using in a query. Like so:

http://xxx.com/contactus.php?subject=a%20cat y la urlencode(stripslashes($_GET['subject']))

Respondido 02 Feb 12, 15:02

Sorry, I think I am misunderstanding here. I do not wish to get the info. I would like to select the drop down from a URL. If I were to click on the link it would auto select a cat from the drop down menu, if you know what I mean. @Tim - Arthor

Above answer solves ur question, if i am getting your question correct you want the drop down value to be based on the URL. - Naveen Kumar

@Naveen Kumar - not quite. if I click on xxx.com/contactus.php?subject=a cat which would be a link on another page. It will come to contact us page and the DROP DOWN menu will be preselected with a cat opposed to the default setting. - Arthor

Then in the contact us page get the value of subject like $_GET['subject'] and make the appropriate dropdown selected as suggested above. - Naveen Kumar

@Tim - Works perfectly! - Thank you - Arthor

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.