Click to See Complete Forum and Search --> : [RESOLVED] Form Method Post Question


AHoward007
05-13-2009, 08:15 PM
I have a form setup up with three input boxes. If I enter data in one of the fields and accidentally hit 'enter' the data posts instead of waiting for all three fields to be entered and the Submit button is clicked. How do I correct that.

<form action="processforms.php" method="post">
<Input name="englishinput" type="text" Style="Color:#000000; Background-Color:#FFFFFF; Text-align:left; Font-Family:Arial; Font-Size:19px; Line-Height: normal; Left:36px; Width:177px; POSITION: absolute; Top:598px; Height:26px; border:2px solid;">
<Input name="spanishinput" type="text" Style="Color:#000000; Background-Color:#FFFFFF; Text-align:left; Font-Family:Arial; Font-Size:19px; Line-Height: normal; Left:242px; Width:177px; POSITION: absolute; Top:598px; Height:26px; border:2px solid;">
<textarea name="commentinput" style="Text-Align:Left; Font-Weight:Bold; Color:#000000; Background-Color:#FFFFFF; Left:36px; POSITION:absolute; Top:693px; Font-Family:Arial; Font-Size:15px; Width:384px; Height:89px;"></textarea>
<Button Style="Color:#0000CD; Background-Color:#C0C0C0; Font-Family:Arial; Font-Weight:Bold; Font-Size:16px; Left:36px; Width:135px; POSITION:absolute; Top:816px; Height:36px;">Submit</Button>
</form>

coldwerturkey
05-14-2009, 04:02 AM
if (empty($required_field)) {
//dont process form, handle error
}

then prior to that define the vars to avoid errors
$required_field = (isset($_POST['required_field'])) ? isset($_POST['required_field']) : '';

then in your form add some value='' attributes so that once the person forgets one field, and the page reloads with a can't-process error, their other entered values are pre-entered

AHoward007
05-14-2009, 07:10 PM
I was able to correct the problem with client side javascripting as follows:


//Revised this script line by adding onsubmit
<form action="processforms.php" method="post" onsubmit="return check_data(this)">




<script type="text/javascript" language="Javascript">
function check_data(my_form)
{
var problem = false;
//Validate englishinput
if (my_form.englishinput.value=="") {
alert ("Enter a Word for English.");
my_form.englishinput.value = "";
my_form.englishinput.focus();
problem = true;
}
//Validate spanishinput
if (my_form.spanishinput.value=="") {
alert ("Enter a Word for Spanish.");
my_form.spanishinput.value = "";
my_form.spanishinput.focus();
problem = true;
}

//The 3rd field commentinput is an optional entry.

if (problem) {
return false;
} else {
return true;
}
}
</script>

AHoward007
05-14-2009, 07:40 PM
I'm still working on the server side validation code.

Coldwerturkey thanks for the reply. I'm studying your code suggestion. Still not clear how it works yet since I'm new to php.