Welcome to WebmasterWorld Guest from 34.228.143.13

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP Form: how to implement drop down menus?

classification: i am a total newbie at php

     
4:20 pm on Jul 11, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 13, 2003
posts:52
votes: 0


Hello peoples,

Okay I'm total new at php. I wanted a form, so I got one made at thesitewizard.com which I then modified.

There are some places on my form where I don't want input fields, instead I want a drop down menu with options to choose from there.

<stupid_question>Would it help to post code, or do people not usually do that for php.</stupid_question>

thanx a mill,
animgirl

4:25 pm on July 11, 2003 (gmt 0)

Full Member

10+ Year Member

joined:Nov 17, 2002
posts:313
votes: 0


Hi animgirl,

Is it some form of dynamic drop-down you want? Ie; information pulled from a database or such?
Or is it a standard drop-down with options you specify in the page?

And from the TOS:

Excessive code dumps will be edited. We appreciate that it's often neccessary to post code, just keep it very short and simple please.

wruk999

4:30 pm on July 11, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 13, 2003
posts:52
votes: 0


No, nothing about pulling info from a database, just several otpions that will be submitted to a server along with the rest of the form info I have set up.
4:41 pm on July 11, 2003 (gmt 0)

Full Member

10+ Year Member

joined:Nov 17, 2002
posts:313
votes: 0


You should not need to construct it in php then.
If you could post the basics of your form ie: no references to domain and without the html formatting, then we can see how is the best way to do it! ;)

wruk999

4:51 pm on July 11, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 13, 2003
posts:52
votes: 0


$name = $_POST['name'] ;
$email = $_POST['email'] ;
$purpose =$_POST['purpose'] ;
$content =$_POST['content'] ;
$target_audience =$_POST['target_audience'] ;
$existing_graphics =$_POST['existing_graphics'] ;
$existing_url =$_POST['existing_url'] ;
$budget = $_POST['budget'] ;
$see_in_a_site = $_POST['see_in_a_site'] ;
$additional_comments = $_POST['additional_comments'];

$http_referrer = getenv( "HTTP_REFERER" );

if (!isset($_POST['email'])) {
header( "Location: $formurl" );
exit ;
}
if (empty($name) empty($email) empty($purpose) empty($content) empty($budget)) {
header( "Location: $errorurl" );
exit ;
}

Howzat?

4:59 pm on July 11, 2003 (gmt 0)

Full Member

10+ Year Member

joined:Nov 17, 2002
posts:313
votes: 0


Thats fine ;)

Okay, is this form emailed to you then?
I take it that that part of it is processed after the code you posted? No-probs.

On your form page, you want something like:


<select name="title">
<option value="Mr">Mr</option>
<option value="Mrs">Mrs</option>
<option value="Ms">Ms</option>
<option value="Miss">Miss</option>
<option value="Dr">Dr</option>
<option value="Choose" selected>Please select..</option>

Okay, this form select is an example of titles.

Then in you php you will need:
$title = $_POST['title'];

Then you can use the $title variable in whatever processing your form does ;)

HTH,
wruk999

5:24 pm on July 11, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 13, 2003
posts:52
votes: 0


Yes it is e-mailed to me, i didn't include that section because it would have been a reference to my site. :)

Okay, that works great :)
o thank you thank you.

Now i'm trying to get the html right.
I'm using xhtml, which makes it a bit different and no tables, just divs.

Here's how I've set up my html:

<form action="online_consultation_form.php" method="post">
<div>
check it baby:<br />
<select name="title">
<option value="Mr">Mr</option>
<option value="Mrs">Mrs</option>
<option value="Ms">Ms</option>
<option value="Miss">Miss</option>
<option value="Dr">Dr</option>
<option value="Choose" selected>Please select..</option>

<br />
<br />

Name:<br />
<input type="text" name="name" size="25" />

<br />
<br />

Email address:<br />
<input type="text" name="email" size="25" />

</div>
</form>

{shortened for convenience}

This is what it looks like:

The name section is overtaken by the new box.

Any ideas? :)
animgirl

[edited by: jatar_k at 8:28 pm (utc) on July 11, 2003]
[edit reason] no personal urls thanks [/edit]

5:33 pm on July 11, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 13, 2003
posts:52
votes: 0


O, the top sections should look like this:

<hehe>mybad</hehe>

[edited by: jatar_k at 8:29 pm (utc) on July 11, 2003]
[edit reason] no personal urls thanks [/edit]

6:02 pm on July 11, 2003 (gmt 0)

Full Member

10+ Year Member

joined:Nov 17, 2002
posts:313
votes: 0


Hi,

The bit with it emailing to you - you can post it, just change domains and email addy's etc to: example@example.com or similar. This means we can still see the function being processed.

Could you posdt the sending to ou bit, as it may need to be configured to show your select list.

The layout on the page, I cannot really help with. That is something worth asking either in a different forum cat, or using trial and error to fix.

wruk999

8:04 pm on July 11, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 13, 2003
posts:52
votes: 0


// $mailto - set to the email address you want the form
// sent to, eg
//$mailto= "example@example.com" ;

$mailto = 'example@example.com' ;

// $subject - set to the Subject line of the email, eg
//$subject= "Spiffy Form Thingie" ;

$subject = "Spiffy Form Thingie" ;

// the pages to be displayed, eg
//$formurl= "http://www.example.com/spiffy_form_thingie.htm" ;
//$errorurl= "http://www.example.com/error.htm" ;
//$thankyouurl= "http://www.example.com/thankyou.htm" ;

$formurl = "http://www.example.com/spiffy_form_thingie.htm" ;
$errorurl = "http://www.example.com/error.htm" ;
$thankyouurl = "http://www.example.com/thankyou.htm" ;

4:55 am on July 12, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 13, 2003
posts:52
votes: 0


As far as the above question went, about why it visually wasn't working, it was because I was missing a closing </select> tag. That was it :)

But now everything works great.
Thank you soo much,
I totally appreciate it ;)
animgirl

7:46 am on July 12, 2003 (gmt 0)

Full Member

10+ Year Member

joined:Nov 17, 2002
posts:313
votes: 0


Hi animgirl,

Sorry, I didn't include the closing </select> tag in my code [blush]

But glad that you got it sorted ;)

wruk999

2:54 pm on July 12, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 13, 2003
posts:52
votes: 0


NP, I do querky stuff all the time. LOL
BUT it works now :D
12:11 am on July 19, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 13, 2003
posts:52
votes: 0


O O but now I want to have some radio buttons for one of my questions also. I've looked online but can't really grasp how to do it. Could you give me some pointers?

Thanks so much,
amimgirl

6:55 pm on July 19, 2003 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15756
votes: 0


You can add as many form alements as you like to your form and then just add the value that is posted to the script to the message.

I assume you build the message from your $_POST values and then use it in the mail function something like this

mail [ca3.php.net]($mailto,$subject,$message,$headers);

All you need to do is add the values and formatting for your form elements to $message.

You have this right now

$name = $_POST['name'] ;
$email = $_POST['email'] ;
$purpose =$_POST['purpose'] ;
$content =$_POST['content'] ;
$target_audience =$_POST['target_audience'] ;
$existing_graphics =$_POST['existing_graphics'] ;
$existing_url =$_POST['existing_url'] ;
$budget = $_POST['budget'] ;
$see_in_a_site = $_POST['see_in_a_site'] ;
$additional_comments = $_POST['additional_comments'];

Just add a new line that has the name of the radio button or you could change this to a loop

$message = "";
foreach ($_POST as $key => $value) {
$message .= $key . ": " . $value . "\n";
}

You could then tweak that to add the formatting to each element or skip ones that you don't want included.

[edited by: jatar_k at 7:00 pm (utc) on July 22, 2003]

6:59 pm on July 22, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 13, 2003
posts:52
votes: 0


What's a loop?

And what would the html part of the radio buttons be like?

7:23 pm on July 22, 2003 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15756
votes: 0


ok loop basics

There are 3 types of loops that I use on a regular basis
foreach [ca.php.net]
while [ca.php.net]
for [ca.php.net]

These are Control Structures [ca.php.net].

A loop will do something over and over again, why and when it stops is different for each type of loop. The documentation linked to above will explain each in detail.

As for your specific situation I threw this at you

$message = "";
foreach ($_POST as $key => $value) {
$message .= $key . ": " . $value . "\n";
}

You have all those lines putting $_POST vars into named vars and then I imagine somewhere you take all of those and put them into a message variable and then send them off to the mail function.

Instead you can use a foreach loop. What that does is make it very simple to iterate through an array and take action (or not) on each individual value. So the little loop I threw at you says

1. For each value in the $_POST array take the name of the element from your form and put it in the variable $key. Then take the value submitted with that form element and put it into the variable $value.

2. Add the name and the value into the message variable, with some formatting.

3. Keep doing this until there are no more values left.

This just makes for shorter, easier to maintain code. Another option would be to use extract($_POST) [ca.php.net] to get the variables out of the $_POST array.

As far as how the radio buttons will look, they look like html radio buttons how about

Radio Buttons, Checkboxes, and More [hotwired.lycos.com]

10:55 pm on July 24, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 13, 2003
posts:52
votes: 0


Yeah, um that's going to take some mental digestion time here. LOL. I'll keep reading it over. Thanks for the great link too ;)
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members