...

View Full Version : How to Stop PHP from clearing valid fields



njccbeach9
03-13-2010, 10:05 PM
Hi,

I have managed to use PHP and stop PHP from clearing valid fields the user has entered except for two controls. Drop down menus, here is part of my page's code:


if (!filter_var($phone, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^0[1-9][0-9]{8,9}$/")))) {
echo "<p><font color='red'>Please enter a valid UK phone number.</font></p>";
}
if ($employee == "1-10" || $employee == "10-20" || $employee == "20-40" || $employee == "40-80" || $employee == "80-160" || $employee == "160-320" || $employee == "320-640" || $employee == "1000+"){

}else {

echo "<p><font color='red'>Please select the number of employees</font></p>";
}


if($consultingtype == "FinanceSupport" || $consultingtype == "MarketingSupport") {

}else {

echo "<p><font color='red'>Please select a consulting option</font></p>";
}

if (!empty($_POST['message'])) {

} else {
$message = FALSE;
echo "<p><font color='red'>Please enter a message.</font></p>";
}

if(isset($_POST['checkbox'])) {
} else {
echo "<p><font color='red'>You must agree to the Terms and Conditions.</font></p>";
}

}
$osrows = array(
array('idInvalid' => 0, 'label' => 'Please select'),
array('id' => 1, 'label' => '1-10'),
array('id' => 2, 'label' => '20-40'),
array('id' => 3, 'label' => '40-80'),
array('id' => 4, 'label' => '80-160'),
array('id' => 5, 'label' => '160-320'),
array('id' => 6, 'label' => '320-640'),
array('id' => 7, 'label' => '1000+'),
);


?>
</p>
</div>
<table width="508" border="0">
<tr>
<td><p>First name</p></td>
<td><label>
<input type="text" name="Firstname" id="Firstname" value="<?php if (!isset($_POST['submit'])) {} else {
$Firstname=$_POST['Firstname']; echo "$Firstname"; }?>" >
</label> </td>
</tr>
<tr>
<td width="164"><p>Surname</p></td>
<td width="334"><label>
<input type="text" name="Surname" id="Surname" value="<?php if (!isset($_POST['submit'])) {} else {
$Surname=$_POST['Surname']; echo "$Surname"; }?>" >
</label></td>
</tr>
<tr>
<td><p>Email Address</p></td>
<td><label>
<input type="text" name="Email" id="Email" value="<?php if (!isset($_POST['submit'])) {} else {
$Email=$_POST['Email']; echo "$Email"; }?>">
</label></td>
</tr>
<tr>
<td><p>Company name</p></td>
<td><label>
<input type="text" name="Company" id="Company" value="<?php if (!isset($_POST['submit'])) {} else {
$Company=$_POST['Company']; echo "$Company"; }?>">
</label></td>
</tr>
<tr>
<td><p>Telephone Number</p></td>
<td><input type="text" name="Telephone" id="Telephone" value="<?php if (!isset($_POST['submit'])) {} else {
$phone=$_POST['Telephone']; echo "$phone"; }?>"></td>
</tr>
<tr>
<td><p>Number of employees</p></td>

<td><select name="employee" id="employee">
<?php foreach ($osrows as $osrow): ?>
<option value="<?php echo $osrow['id'] ?>"

<?php if ($osrow['id'] == $osrow):?>selected="selected"<?php endif; ?>
><?php echo $osrow['label'] ?></option>
<?php endforeach; ?>
</select>

Feeback and help would be really appreciated...

Thanks

Fumigator
03-14-2010, 04:45 AM
This code:


<?php if ($osrow['id'] == $osrow):?>selected="selected"<?php endif; ?>


Seems to be comparing the wrong things. Don't you want to compare $osrow['id'] to $_POST['employee']?

FYI, just a little tip, you can use what is called ternary syntax for this kind of "if" statement, which is easier to read and code:



<?php
//ternary syntax: (condition ? true : false)
echo ($osrow['id'] == $_POST['employee'] ? "selected='selected'" : "");
?>

njccbeach9
03-14-2010, 11:46 PM
Ok that's fantastic. Your code has solved this problem entirely.

However relating to the entire form. For the text area field (message) it automatically puts a space inside the field and therefore the validation error does not appear. Once I keep submitting with invalid data it puts more and more spaces in the text area control. However when I delete the spaces and submit the validation error appears as I would like. Please can you help me solve this issue.

Relating to this, all my fields have this problem, the user could just hit the space bar a couple of times in each field and the form successfully validates.
How do I ensure that the user enters characters into the fields and not just spaces. Here is my code:


<form action="<?php $_SERVER['PHP_SELF']; ?>"method="post" name="form" id="form">
<div class="divRightContact"><p>
<?php


if (!isset($_POST['submit'])) {


} else {

$Firstname = $_POST['Firstname'];


$Surname = $_POST['Surname'];


$Email = $_POST['Email'];


$Company = $_POST['Company'];


$phone = ereg_replace('[^0-9]', '', $_POST['Telephone']);


$employee = ($_POST['employee'] or $osrows);


$consultingtype = $_POST['consultingtype'];


$message = $_POST['Message'];



if (!empty($_POST['Firstname'])) {

} else {
$Firstname = FALSE;
echo "<p><font color='red'>Please enter your first name.</font></p>";
}

if (!empty($_POST['Surname'])) {

} else {
$Surname = FALSE;
echo "<p><font color='red'>Please enter your surname.</font></p>";
}

if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $Email)){
} else { echo "<p><font color='red'>Please enter a valid email address.</font></p>";
}

if (!empty($_POST['Company'])) {

} else {
$Company = FALSE;
echo "<p><font color='red'>Please enter your company name.</font></p>";
}


if (!filter_var($phone, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^0[1-9][0-9]{8,9}$/")))) {
echo "<p><font color='red'>Please enter a valid UK phone number.</font></p>";
}
if ($employee == "1-10" || $employee == "10-20" || $employee == "20-40" || $employee == "40-80" || $employee == "80-160" || $employee == "160-320" || $employee == "320-640" || $employee == "1000+"){

}else {

echo "<p><font color='red'>Please select the number of employees</font></p>";
}


if($consultingtype == "FinanceSupport" || $consultingtype == "MarketingSupport") {

}else {

echo "<p><font color='red'>Please select a consulting option</font></p>";
}

if (!empty($_POST['Message'])) {

} else {
$message = FALSE;
echo "<p><font color='red'>Please enter a message.</font></p>";
}

if(isset($_POST['checkbox'])) {
} else {
echo "<p><font color='red'>You must agree to the Terms and Conditions.</font></p>";
}

}
$osrows = array(
array('idInvalid' => 0, 'label' => 'Please select'),
array('id' => 1, 'label' => '1-10'),
array('id' => 2, 'label' => '20-40'),
array('id' => 3, 'label' => '40-80'),
array('id' => 4, 'label' => '80-160'),
array('id' => 5, 'label' => '160-320'),
array('id' => 6, 'label' => '320-640'),
array('id' => 7, 'label' => '1000+'),
);

$consultingrows = array(
array('idInvalid' => 0, 'label' => 'Please select'),
array('id' => 1, 'label' => 'Finance Client'),
array('id' => 2, 'label' => 'Marketing Client'),

);
?>
</p>
</div>
<table width="508" border="0">
<tr>
<td><p>First name</p></td>
<td><label>
<input type="text" name="Firstname" id="Firstname" value="<?php if (!isset($_POST['submit'])) {} else {
$Firstname=$_POST['Firstname']; echo "$Firstname"; }?>" >
</label> </td>
</tr>
<tr>
<td width="164"><p>Surname</p></td>
<td width="334"><label>
<input type="text" name="Surname" id="Surname" value="<?php if (!isset($_POST['submit'])) {} else {
$Surname=$_POST['Surname']; echo "$Surname"; }?>" >
</label></td>
</tr>
<tr>
<td><p>Email Address</p></td>
<td><label>
<input type="text" name="Email" id="Email" value="<?php if (!isset($_POST['submit'])) {} else {
$Email=$_POST['Email']; echo "$Email"; }?>">
</label></td>
</tr>
<tr>
<td><p>Company name</p></td>
<td><label>
<input type="text" name="Company" id="Company" value="<?php if (!isset($_POST['submit'])) {} else {
$Company=$_POST['Company']; echo "$Company"; }?>">
</label></td>
</tr>
<tr>
<td><p>Telephone Number</p></td>
<td><input type="text" name="Telephone" id="Telephone" value="<?php if (!isset($_POST['submit'])) {} else {
$phone=$_POST['Telephone']; echo "$phone"; }?>"></td>
</tr>
<tr>
<td><p>Number of employees</p></td>

<td><select name="employee" id="employee">
<?php foreach ($osrows as $osrow): ?>
<option value="<?php echo $osrow['id'] ?>"

<?php echo ($osrow['id'] == $_POST['employee'] ? "selected='selected'" : ""); ?>
><?php echo $osrow['label'] ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
<tr>
<td><p>Consulting type</p></td>
<td>
<select name="consultingtype" id="consultingtype">
<?php foreach ($consultingrows as $consultingrow): ?>
<option value="<?php echo $osrow['id'] ?>"

<?php echo ($consultingrow['id'] == $_POST['consultingtype'] ? "selected='selected'" : ""); ?>
><?php echo $consultingrow['label'] ?></option>
<?php endforeach; ?>
</select></td>
</tr>
<tr>
<td height="133"><p>Message</p></td>
<td valign="top">
<textarea name="Message" cols="45" rows="10" id="Message"> <?php if (!isset($_POST['submit'])) {} else {
$message=$_POST['Message']; echo "$message"; }?></textarea></td>
</tr>
<tr>
<td valign="top"><p> I agree to LMGR <a href="../Policies/policies.html" target="_blank">Terms and Conditons </a></p> </td>
<td valign="top">
<input name="checkbox" type="checkbox" id="checkbox" value="checked"></td>
</tr>
</table>

<input name="submit" type="submit" id="submit" value="Submit">
</form>

Thanks for all your help, it really is appreciated,

Nicholas



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum