Contact form/Jump Menu help needed urgently
Moderator: General Moderators
-
nathanbrooks88
- Forum Newbie
- Posts: 5
- Joined: Tue Aug 18, 2009 11:02 am
Contact form/Jump Menu help needed urgently
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.
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
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.
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
Thanks for the early reply.
Could you please give me an example?
Could you please give me an example?
-
nathanbrooks88
- Forum Newbie
- Posts: 5
- Joined: Tue Aug 18, 2009 11:02 am
Re: Contact form/Jump Menu help needed urgently
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
Well your select box's name is "province", so $_POST['province'] or $_GET['province'].
Re: Contact form/Jump Menu help needed urgently
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
What is the syntax for posting an array?
Re: Contact form/Jump Menu help needed urgently
Do you mean like this?
Edit: This post was recovered from search engine cache.
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>
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
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
I've told you how.
You're going about this completely the wrong way.
You're going about this completely the wrong way.
Re: Contact form/Jump Menu help needed urgently
Maybe this will help.
Edit: This post was recovered from search engine cache.
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>
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
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.
$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.
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
Re: Contact form/Jump Menu help needed urgently
When you get values from a form it always gets the 'value'.
Re: Contact form/Jump Menu help needed urgently
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".
When the form is created, 2 becomes the value of the option and "Pipe" becomes the label.
When the form is submitted, a single value is submitted (the 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).
Edit: This post was recovered from search engine cache.
Suppose one of the elements of the source array ($things) has a key 2 and a value "Pipe".
Code: Select all
$things[2] = 'Pipe';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>Code: Select all
$_POST['thing'] == '2'Code: Select all
if (isset($_POST['thing']) && array_key_exists($_POST['thing'], $things)) {
$chosen_thing = $things[$_POST['thing']];
}
Last edited by McInfo on Wed Jun 16, 2010 6:52 pm, edited 1 time in total.