Jump to content

[SOLVED] Select Box and PHP - Can this be done?


Bricktop

Recommended Posts

Hello ppl!,

 

OK, I have a simple form which is structured as follows:

 

$content .= '<tr><td>Full Name</td><td><input name="Name" type="text" value="'.$_POST['Name'].'" size="32" maxlength="32" class="text" /></td></tr>';

$content .= '<tr><td>Telephone</td><td><input name="Telephone" type="text" value="'.$_POST['Telephone'].'" size="32" maxlength="32" class="text" /></td></tr>';

$content .= '<tr><td>Email Address</td><td><input name="Email" type="text" value="'.$_POST['Email'].'" size="32" maxlength="50" class="text" /></td></tr>';

echo $content;

 

This works great and when the user presses submit and there is an error on the form, the page refeshes, flashes the error reason (extra PHP code I haven't included handles this), and the fields remember what has been typed into them (because of the value="'.$_POST['xxxx'].'" syntax) - this is great because on a form error the user doesn't have to retype the data.

 

However, now I have added a drop-down box to the form and I cannot work out how I can get the drop-down box to remember the selection on a form error.  If people fill in the form, press submit and the form errors, the select box resets back to the default selection.  Essentially, the new code is:

 

$content .= '<tr><td>Full Name</td><td><input name="Name" type="text" value="'.$_POST['Name'].'" size="32" maxlength="32" class="text" /></td></tr>';

$content .= '<tr><td>Telephone</td><td><input name="Telephone" type="text" value="'.$_POST['Telephone'].'" size="32" maxlength="32" class="text" /></td></tr>';

$content .= '<tr><td>Email Address</td><td><input name="Email" type="text" value="'.$_POST['Email'].'" size="32" maxlength="50" class="text" /></td></tr>';

$content .= '<tr><td>County</td><td<SELECT NAME="txtCounty" SIZE="1">

				<option VALUE="Please select your county">Please select your county</option>

				<OPTION VALUE="Aberdeenshire">Aberdeenshire</OPTION>

				<OPTION VALUE="Angus">Angus</OPTION>

				<OPTION VALUE="Argyll">Argyll</OPTION>

				<OPTION VALUE="Avon">Avon</OPTION></SELECT></td></tr>';

echo $content;

 

Hopefully my request makes sense, does anyone know how I can achieve this with PHP please?  Or do I need to use JS?

 

 

something like this should work:

<?php

$options = generateOptions($_POST['txtCountry']);
$content .= '<tr><td>Full Name</td><td><input name="Name" type="text" value="'.$_POST['Name'].'" size="32" maxlength="32" class="text" /></td></tr>';

$content .= '<tr><td>Telephone</td><td><input name="Telephone" type="text" value="'.$_POST['Telephone'].'" size="32" maxlength="32" class="text" /></td></tr>';

$content .= '<tr><td>Email Address</td><td><input name="Email" type="text" value="'.$_POST['Email'].'" size="32" maxlength="50" class="text" /></td></tr>';
$content .= '<tr><td>County</td><td<SELECT NAME="txtCounty" SIZE="1">';
$content .= $options;
$content .= '</SELECT></td></tr>';

function generateOptions($curSelect)
{
$opts=array(
	'Please select your country',
	'Aberdeenshire',
	'Angus',
	'Argyll',
	'Avon'
);
$options="";
foreach($opts as $name)
{
	$sel = ($name == $curSelect) ? ' selected="selected"' : "";
	$options .= "<OPTION VALUE=\"$name\"$sel>$name</OPTION>\n";
}
        return $options;
}

although this:

<?php
$options = generateOptions($_POST['txtCountry']);

should be changed to this:

<?php
$selOption = isset($_POST['txtCountry']) ? $_POST['txtCountry'] : "";
$options = generateOptions($selOption);

in case it hasn't been submitted yet

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.