Page 1 of 2

Contact form/Jump Menu help needed urgently

Posted: Tue Aug 18, 2009 11:15 am
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.

Re: Contact form/Jump Menu help needed urgently

Posted: Tue Aug 18, 2009 11:43 am
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.

Re: Contact form/Jump Menu help needed urgently

Posted: Tue Aug 18, 2009 12:36 pm
by nathanbrooks88
Thanks for the early reply.

Could you please give me an example?

Re: Contact form/Jump Menu help needed urgently

Posted: Tue Aug 18, 2009 12:45 pm
by jackpf

Re: Contact form/Jump Menu help needed urgently

Posted: Tue Aug 18, 2009 1:19 pm
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?

Re: Contact form/Jump Menu help needed urgently

Posted: Tue Aug 18, 2009 1:29 pm
by jackpf
Well your select box's name is "province", so $_POST['province'] or $_GET['province'].

Re: Contact form/Jump Menu help needed urgently

Posted: Tue Aug 18, 2009 1:55 pm
by McInfo
PHP Manual: Variables From External Sources

Edit: This post was recovered from search engine cache.

Re: Contact form/Jump Menu help needed urgently

Posted: Tue Aug 18, 2009 6:11 pm
by nathanbrooks88
What is the syntax for posting an array?

Re: Contact form/Jump Menu help needed urgently

Posted: Tue Aug 18, 2009 6:48 pm
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.

Re: Contact form/Jump Menu help needed urgently

Posted: Wed Aug 19, 2009 8:09 am
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?

Re: Contact form/Jump Menu help needed urgently

Posted: Wed Aug 19, 2009 9:02 am
by jackpf
I've told you how.

You're going about this completely the wrong way.

Re: Contact form/Jump Menu help needed urgently

Posted: Wed Aug 19, 2009 12:16 pm
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.

Re: Contact form/Jump Menu help needed urgently

Posted: Thu Aug 20, 2009 11:33 pm
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.

Re: Contact form/Jump Menu help needed urgently

Posted: Thu Aug 20, 2009 11:53 pm
by aceconcepts
When you get values from a form it always gets the 'value'.

Re: Contact form/Jump Menu help needed urgently

Posted: Thu Aug 20, 2009 11:59 pm
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.