Jump to content

How can i achieve this?


stuart7398

Recommended Posts

Hi.

How would I do the following?

 

On my website will be a contact us page with a drop down menu with 'subjects'

For example

Request a brochure

Make a complaint

Make a suggestion

 

When the user lands on the 'contact us' page how can I get the subject box to automatically show the option from the page they have come from? 

 

 

So coming from request a brochure page the subject area will show

'Request a brochure' and so on.

 

 

Any help is appreciated.

 

Thanks Stu.

 

Link to comment
https://forums.phpfreaks.com/topic/98477-how-can-i-achieve-this/
Share on other sites

You want to place the SELECTED statement within the option. The quick and dirty way would be:

<!-- $_SERVER['HTTP_REFERER'] will show the page they came from -->
<select name='subjects' id='subjects'>
<option <?php if ($_SERVER['HTTP_REFERER'] == 'urlofapage') { echo 'SELECTED'; } ?> value='brochure'>Request a brochure</option>
<option <?php if ($_SERVER['HTTP_REFERER'] == 'urlofadifferentpage') { echo 'SELECTED'; } ?> value='complaint'>Make a complaint</option>
<option <?php if ($_SERVER['HTTP_REFERER'] == 'urlofanotherpage') { echo 'SELECTED'; } ?> value='suggestion'>Make a suggestion</option>
</select>

Assuming that your coding your page and have an understanding of the coding being used.. I am going to say.

Just use some simple variables.

You will need to change your links up a bit that link to the form to include said variables.

 

so if your links are something to the effect of www.yourdomain.com/form.php

you could change it to www.yourdomain.com/form.php?subj=reqst

 

Where reqst would be your variable. Then in the form.php

add a couple simple if-else-then style statements (pending how your server is set up and what PHP version you have this may vary). But just set them up to find the variable.

 

Example:

<?php
if ($_Get['subj'] == "") {
//leave field blank.. for user input
} elseif ($_Get['subj'] == "reqst") {
$subj = "1";
} else {
//leave field blank.. for user input
}
?>

It gets complicated with the essence of a drop down box, but..

<select size="1" name="D1">
<option value="none" <?php if (!isset($subj)) { echo "selected"; ?>>--select--</option>
<option value="1" <?php if ($subj == "reqst") { echo "selected"; ?>>Request Brocher</option>
<option value="2" <?php if ($subj == "var2") { echo "selected"; ?>>blah</option>
<option value="3" <?php if ($subj == "var3") { echo "selected"; ?>>something</option>
</select>

 

I might be making things more complicated then need be.. but that usually works for me.. when I'm working on forms.. If anyones got a better idea, by all means let it be known, I could use something better if it exists when im workin on forms.. :-)

 

Actually viewing my own post.. the first if statement really isn't necessary, well not per say.. at least.. I would use it along with for other means, but not for the form element.. Was kinda typing without thinking, sorry for any confusion..

 

selected="selected"

 

Not just

selected

Um, not, you are wrong. just SELECTED is necessary.

http://htmlhelp.com/reference/html40/forms/option.html

 

That is fine if you're using plain HTML4. However if you're using XHTML then you'll have to use select="select"

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.