Contact form/Jump Menu help needed urgently

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

nathanbrooks88
Forum Newbie
Posts: 5
Joined: Tue Aug 18, 2009 11:02 am

Contact form/Jump Menu help needed urgently

Post by nathanbrooks88 »

Hi there,

Im very new to php. Im doing a query form for a site but have problems getting the selected data from a jump menu to send through to an email correctly.

The information posts, but its incorrect.

Here is the code for the jump menu :

<?php
$province = array (1 => 'Western Cape',
'West Coast', 'Cape Overberg', 'Northern Cape', 'Eastern Cape',
'Free State', 'Limpopo', 'Gauteng');
echo '<select name="province">';
foreach ($province as $key => $value)
{
echo "<option value=\"$key\">$value</option>\n";
}
echo "</select>";
?>

Heres the code that is meant to get the info :
$province_select=' ';
for ($i=0; $i < count($province); $i++)
{
$province_select=$province[$i];
}

Lastly to print out the array I do the following :
$array = array ($name, $surname, $contact, $email_confirmed, $province_select, $date);
$fields = array ('First Name', 'Surname', 'Contact Number', 'Email Address', 'Province', 'Region/Town', 'Date of Function');

echo 'Thank you for your Enquiry. <br><br>You Entered the following details : <br>';
for ($i=0; $i < count($array); $i++)
{
$array [$i] = ucfirst($array [$i]);
echo '<table width="300px"><tr><td bgcolor="#669933" width="65%">' . $fields [$i] . ' : </td><td bgcolor="#669933">' . $array [$i] . '</td></tr></table>';

}
When I echo out $province_select [$i] it always prints out 'Limpopo', which is the 7th item in the array, instead of the selected option.

Please advise on where I have coded incorrectly?

Thanks in advance.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Contact form/Jump Menu help needed urgently

Post by jackpf »

That's because you're just looping through the array seven times. So it will be the 7th value.

You need to use the $_POST (or $_GET dependng on your form method) array instead.
nathanbrooks88
Forum Newbie
Posts: 5
Joined: Tue Aug 18, 2009 11:02 am

Re: Contact form/Jump Menu help needed urgently

Post by nathanbrooks88 »

Thanks for the early reply.

Could you please give me an example?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Contact form/Jump Menu help needed urgently

Post by jackpf »

nathanbrooks88
Forum Newbie
Posts: 5
Joined: Tue Aug 18, 2009 11:02 am

Re: Contact form/Jump Menu help needed urgently

Post by nathanbrooks88 »

Hmmm I understand that I must use a GET or POST method, but where and hows do I use it on a jump menu?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Contact form/Jump Menu help needed urgently

Post by jackpf »

Well your select box's name is "province", so $_POST['province'] or $_GET['province'].
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Contact form/Jump Menu help needed urgently

Post by McInfo »

PHP Manual: Variables From External Sources

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 6:50 pm, edited 1 time in total.
nathanbrooks88
Forum Newbie
Posts: 5
Joined: Tue Aug 18, 2009 11:02 am

Re: Contact form/Jump Menu help needed urgently

Post by nathanbrooks88 »

What is the syntax for posting an array?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Contact form/Jump Menu help needed urgently

Post by McInfo »

Do you mean like this?

Code: Select all

<form action="" method="post">
    <input type="text" name="things[]" /><br />
    <input type="text" name="things[]" /><br />
    <input type="text" name="things[]" /><br />
    <input type="text" name="things[]" /><br />
    <input type="submit" value="Submit These Things" />
</form>
<pre><?php
if (isset($_POST['things'])) {
    print_r($_POST['things']);
}
?></pre>
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 6:50 pm, edited 1 time in total.
nathanbrooks88
Forum Newbie
Posts: 5
Joined: Tue Aug 18, 2009 11:02 am

Re: Contact form/Jump Menu help needed urgently

Post by nathanbrooks88 »

Hmmm not quite. You see I have an array $province which is a jump menu that contains roughly 8 or 9 entries. But how do I echo out the selected value?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Contact form/Jump Menu help needed urgently

Post by jackpf »

I've told you how.

You're going about this completely the wrong way.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Contact form/Jump Menu help needed urgently

Post by McInfo »

Maybe this will help.

Code: Select all

<?php
$things = array
(   'House'
,   'Tree'
,   'Pipe'
,   'String'
,   'Computer'
);
?>
 
<pre>$things => <?php print_r($things); ?></pre>
 
<form method="post">
    <select name="thing">
<?php
    foreach ($things as $value => $label) :
        echo "\t\t<option value=\"{$value}\">{$label}</option>\n";
    endforeach;
?>
    </select>
    <select name="things[]" multiple="multiple">
<?php
    foreach ($things as $value => $label) :
        echo "\t\t<option value=\"{$value}\">{$label}</option>\n";
    endforeach;
?>
    </select>
    <input type="submit" value="Submit" />
</form>
 
<pre>$_POST => <?php
if (!empty($_POST)) {
    print_r($_POST);
} else {
    echo '<i>(Not submitted yet)</i>';
}
?></pre>
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 6:51 pm, edited 1 time in total.
nathanbrooks
Forum Newbie
Posts: 8
Joined: Tue Aug 18, 2009 10:59 am

Re: Contact form/Jump Menu help needed urgently

Post by nathanbrooks »

Okay so I tried the following;
$province_=$_POST ['province'];

and it sends back a numeric value. So if I pick the second option on the menu, the output will be '2' instead of 'West Coast'.

So sorry about all this, really a beginner to PHP and still learning.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Contact form/Jump Menu help needed urgently

Post by aceconcepts »

When you get values from a form it always gets the 'value'.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Contact form/Jump Menu help needed urgently

Post by McInfo »

A number is what you should expect to be submitted by the form. (It's actually a number-as-a-string.)

Suppose one of the elements of the source array ($things) has a key 2 and a value "Pipe".

Code: Select all

$things[2] = 'Pipe';
When the form is created, 2 becomes the value of the option and "Pipe" becomes the label.

Code: Select all

<select name="thing">
    <option value="0">House</option>
    <option value="1">Tree</option>
    <option value="2">Pipe</option>
    <option value="3">String</option>
    <option value="4">Computer</option>
</select>
When the form is submitted, a single value is submitted (the 2).

Code: Select all

$_POST['thing'] == '2'
To convert the POSTed value back to the associated string, you need to use the value as the key in the original array ($things).

Code: Select all

if (isset($_POST['thing']) && array_key_exists($_POST['thing'], $things)) {
    $chosen_thing = $things[$_POST['thing']];
}
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 6:52 pm, edited 1 time in total.
Post Reply