Jump to content

Webmail - using drop-down for subject


murfitUK

Recommended Posts

OK. I'll try my best to describe what I'm trying to do.I have a webpage through which a user can send an email. They type in their name, email address, a subject and the message then click Send. The page then calls itself, checks that none of the four fields are empty and that the email address is a valid address. If everything is OK, the email gets sent.If anything is missing the page is displayed again along with everything the user typed so that they don't have to type it all again. (You can see what it's like at www.wooden-heart.co.uk - in the Contact Us section.)Anyway, what I'd like to do is limit the subject field to a few items from a drop-down list. This works but if the page is recalled, it does not keep the subject chosen by the user - it always displays the top item. I would like it to keep what the user has already chosen, but don't know how.Here's some of the code.BEFORE (when user types subject):echo "<input type='text' name='subject' value=\"" . $_POST['subject'] . "\" />\n";The . $_POST['subject'] . bit makes sure that the user's typed field gets added to the value of subject field so the user doesn't need to type it again if the page is recalled.AFTER (when user has to choose from drop-down list):echo "<select name='subject'>\n<option value='Sales Enquiry'>Sales Enquiry</option>\n<option value='Comment About Website'>Comment About Website</option>\n<option value='Other Enquiry'>Other Enquiry</option>\n (& there will be a few more options)</select>\n";I know I can use selected='selected' to make one of the options be selected but I'm not sure what I have to do. I think I will have to put an if... in each of the option value lines. Something like:echo "<option value='Sales Enquiry' "; if ($_POST['subject']=="Sales Enquiry") echo "selected='selected'"; print ">Sales Enquiry</option>";for each line obviously changing Sales Enquiry to the relevant text.Am I on the right track? Is there an easier way to do it?Thanks.

Link to comment
Share on other sites

Well, I've tried it and it seems to work! There might be an easier way of doing it but this way works for me - and I thought of it all by myself.(Sorry for the two postings... when I first posted I got a sql database error message so I went back and hit submit again.)

Link to comment
Share on other sites

Few options:Do a search and replace:

$text = "<select name='subject'>\n<option value='Sales Enquiry'>Sales Enquiry</option>\n<option value='Comment About Website'>Comment About Website</option>\n<option value='Other Enquiry'>Other Enquiry</option>\n (& there will be a few more options)</select>\n";//via str_replace$newtext = str_replace($_POST['subject'], $_POST['subject']."\' selected", $text);//via regex$newtextb = preg_replace('/('.$_POST['subject'].'\')/', '$1 selected', $text);

The other thing, the method that I would recommend is to dynamically build the list using the method previously mentioned:

$array = ("Sales Enquiry", "Comment About Website", "Other enquiry", "Etc", "Etc");$text = "<select name=\"subject\">\n";foreach ($array as $choice){  $text.= "<option value=\"$choice\" ".($_POST["subject"]==$choice ? " selected=\"selected\"" : "").">$choice</option>\n";}$text.= "</select>\n";

This way should be easier. If you want to add new subject choices, you just add to the $array variable. Otherwise, you'd have to craft a new line of code for each choice.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...