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?

 

 

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.