Jump to content

Recommended Posts

here is my form

 

       
        <?php foreach($_POST as $field => $value) {
		if (!$value) {
		   $err .= "$field is a required field <br>";
		   $warnings[$field] ="required";
		   }
		   }
		   
		   if ($_POST["firstname"] &&
		   !ereg("^A-Za-z", $_POST["firstname"]))
		   $warnings["firstname"] = "Must be letters only";
		   
		   if ($err){
		   echo $err;			   
		  if (count($warnings) > 0) {


	?>
        <form id="FormName" action='<?php "$_SERVER[php_SELF]" ?>' method="post" name="basic">
        Name  : 
          <label>First: 
          <input type="text" name="firstname" id="firstname" value="<?php echo
$getuserprofile['first_name'] ?>" /> <?php echo $warnings["firstname"]; ?>
          </label>
          <label>Last: 
          <input type="text" name="lastname" id="lastname" value="<?php echo $getuserprofile['last_name'] ?>" />
          </label>
          <br />
          Birthday :
          <?php
// include the class
require "datepicker/class.datepicker.php";
// instantiate the object
$dp = new datepicker();
?>
(dd-mm-yyyy) 
<input type="text" id="date" value="<?php echo $getuserprofile['dob'] ?>">
  <input type="button" value="..." onclick="<?= $dp->show("date") ?>">
  <br />
          Gender : <select  id="gender" name="gender">
  <option value="Male" >Male</option>
  <option value="Female"  >Female</option>
</select><br />
  club :<?php $lines = file('runningclubs.txt');
echo '<select class="inputedit" id="club" name="club">';
foreach ($lines as $line)
{
			if ($pclub == trim($line))
			{
							echo '<option value="' . $line . '" selected="selected">' . $line . '</option>';
			}
			else
			{
							echo '<option value="' . $line . '">' . $line . '</option>';
			}
}
echo "</select>";
}}
?>
<input type="hidden" name="id" value="<?php echo $id ?>">
<input  type="submit" class="submit-btn" value="">  
</form>
       

 

basicly im trying to do to things

 

1) check fild is filled in!

2) check a field is filled in correctly!

 

thing is the filled in works (can it be improved or is the simple method fine?). But it awayls displays $warnings["firstname"] = "Must be letters only"; even if letter are just entred ??

Link to comment
https://forums.phpfreaks.com/topic/142787-solved-form-validation/
Share on other sites

try your ereg like this...

 

<?php
foreach($_POST as $field => $value) {
if (!$value) {
	$err .= "$field is a required field <br>";
	$warnings[$field] ="required";
}
}

if ($_POST["firstname"] && !ereg("/^[a-zA-Z]+$/", $_POST["firstname"]))
$warnings["firstname"] = "Must be letters only";
?>

<link rel="stylesheet" type="text/css" href="http://www.runningprofiles.com/css/login.css">
<link rel="stylesheet" type="text/css" href="include/profiletab/ajaxtabs/ajaxtabs.css">
<script type="text/javascript" src="include/profiletab/ajaxtabs/ajaxtabs.js"></script>
<?php
require_once '../settings.php';
checkLogin('1 2');
include "../info.php"; // sets username/id ect
include "../getuser.php"; // records user view on page
$getuser = mysql_fetch_assoc(mysql_query("SELECT * from users where Username='$username'"));
$getuserprofile = mysql_fetch_assoc(mysql_query("SELECT * from profile where ID='" .
			$getuser['ID'] . "'")); ?>
<div align="center"><?php echo $getuserprofile['club'] ?>Please edit you prfile here admin</div>
<table width="100%" cellspacing="0" cellpadding="0">
  <tr>
    <td> </td>
  </tr>
</table>
<table width="100%" cellspacing="0" cellpadding="0">
  <tr>
    <td width="10%"> </td>
    <td width="80%"><ul id="countrytabs" class="shadetabs">
      <li><a href="#" rel="#default" class="selected">About me</a></li>
      <li><a href="include/profiletab/edit1.htm" rel="countrycontainer">Hobbies</a></li>
      <li><a href="include/profiletab/edit2.htm" rel="countrycontainer">Goals</a></li>
      <li><a href="include/profiletab/edit3.htm" rel="#iframe">Films and Books</a> </li>
    </ul>
      <div id="countrydivcontainer" style="border:1px solid gray; width:800px; margin-bottom: 1em; padding: 10px"> 
        <p>
        <? 		   
		foreach($_POST as $field => $value) {
   if (!$value) {
      $err .= "$field is a required field <br>";
      $warnings[$field] ="required";
   }
}

if ($_POST["firstname"] && preg_match("/^[a-zA-Z]+$/", $_POST["firstname"]))
   $warnings["firstname"] = "Must be letters only";
		   
		   if ($err){
		   echo $err;			   
		  if (count($warnings) > 0) {


	?>
        <form id="FormName" action='<?php "$_SERVER[php_SELF]" ?>' method="post" name="basic">
        Name  : 
          <label>First: 
          <input type="text" name="firstname" id="firstname" value="" /> <?php echo $warnings["firstname"]; ?>
          </label>
          <label>Last: 
          <input type="text" name="lastname" id="lastname" value="<?php echo $getuserprofile['last_name'] ?>" />
          </label>
          <br />
          Birthday :
          <?php
// include the class
require "datepicker/class.datepicker.php";
// instantiate the object
$dp = new datepicker();
?>
(dd-mm-yyyy) 
<input type="text" id="date" value="<?php echo $getuserprofile['dob'] ?>">
  <input type="button" value="..." onclick="<?= $dp->show("date") ?>">
  <br />
          Gender : <select  id="gender" name="gender">
  <option value="Male" >Male</option>
  <option value="Female"  >Female</option>
</select><br />
  club :<?php $lines = file('runningclubs.txt');
echo '<select class="inputedit" id="club" name="club">';
foreach ($lines as $line)
{
			if ($pclub == trim($line))
			{
							echo '<option value="' . $line . '" selected="selected">' . $line . '</option>';
			}
			else
			{
							echo '<option value="' . $line . '">' . $line . '</option>';
			}
}
echo "</select>";
}}
?>
<input type="hidden" name="id" value="<?php echo $id ?>">
<input  type="submit" class="submit-btn" value="">  
</form>

        </td>
    <td width="10%"> </td>
  </tr>
</table>
<script type="text/javascript">
var countries=new ddajaxtabs("countrytabs", "countrydivcontainer")
countries.setpersist(true)
countries.setselectedClassTarget("link") //"link" or "linkparent"
countries.init()
  </script>

here we go

 

<link rel="stylesheet" type="text/css" href="http://www.runningprofiles.com/css/login.css">
<link rel="stylesheet" type="text/css" href="include/profiletab/ajaxtabs/ajaxtabs.css">
<script type="text/javascript" src="include/profiletab/ajaxtabs/ajaxtabs.js"></script>
<?php
require_once '../settings.php';
checkLogin('1 2');
include "../info.php"; // sets username/id ect
include "../getuser.php"; // records user view on page
$getuser = mysql_fetch_assoc(mysql_query("SELECT * from users where Username='$username'"));
$getuserprofile = mysql_fetch_assoc(mysql_query("SELECT * from profile where ID='" .
            $getuser['ID'] . "'")); ?>
<div align="center"><?php echo $getuserprofile['club'] ?>Please edit you prfile here admin</div>
<table width="100%" cellspacing="0" cellpadding="0">
  <tr>
    <td> </td>
  </tr>
</table>
<table width="100%" cellspacing="0" cellpadding="0">
  <tr>
    <td width="10%"> </td>
    <td width="80%"><ul id="countrytabs" class="shadetabs">
      <li><a href="#" rel="#default" class="selected">About me</a></li>
      <li><a href="include/profiletab/edit1.htm" rel="countrycontainer">Hobbies</a></li>
      <li><a href="include/profiletab/edit2.htm" rel="countrycontainer">Goals</a></li>
      <li><a href="include/profiletab/edit3.htm" rel="#iframe">Films and Books</a> </li>
    </ul>
      <div id="countrydivcontainer" style="border:1px solid gray; width:800px; margin-bottom: 1em; padding: 10px">
        <p>
        <?          
         foreach($_POST as $field => $value) {
   if (!$value) {
      $err .= "$field is a required field <br>";
      $warnings[$field] ="required";
   }
}

if (!$_POST["firstname"] || !preg_match("/^[a-zA-Z]+$/", $_POST["firstname"])) {
   $warnings["firstname"] = "Must be letters only";
}
            
            if ($err){
            echo $err;            
           if (count($warnings) > 0) {
      
      
      ?>
        <form id="FormName" action='<?php "$_SERVER[php_SELF]" ?>' method="post" name="basic">
        Name  :
          <label>First:
          <input type="text" name="firstname" id="firstname" value="" /> <?php echo $warnings["firstname"]; ?>
          </label>
          <label>Last:
          <input type="text" name="lastname" id="lastname" value="<?php echo $getuserprofile['last_name'] ?>" />
          </label>
          <br />
          Birthday :
          <?php
// include the class
require "datepicker/class.datepicker.php";
// instantiate the object
$dp = new datepicker();
?>
(dd-mm-yyyy)
<input type="text" id="date" value="<?php echo $getuserprofile['dob'] ?>">
  <input type="button" value="..." onclick="<?= $dp->show("date") ?>">
  <br />
          Gender : <select  id="gender" name="gender">
  <option value="Male" >Male</option>
  <option value="Female"  >Female</option>
</select><br />
  club :<?php $lines = file('runningclubs.txt');
echo '<select class="inputedit" id="club" name="club">';
foreach ($lines as $line)
{
            if ($pclub == trim($line))
            {
                        echo '<option value="' . $line . '" selected="selected">' . $line . '</option>';
            }
            else
            {
                        echo '<option value="' . $line . '">' . $line . '</option>';
            }
}
echo "</select>";
}}
?>
<input type="hidden" name="id" value="<?php echo $id ?>">
<input  type="submit" class="submit-btn" value=""> 
</form>

        </td>
    <td width="10%"> </td>
  </tr>
</table>
<script type="text/javascript">
var countries=new ddajaxtabs("countrytabs", "countrydivcontainer")
countries.setpersist(true)
countries.setselectedClassTarget("link") //"link" or "linkparent"
countries.init()
  </script>

I just realise you're looking for a FALSE return, you have the wrong boolean value, get rid of the exclamation mark.

 

Basically instead of checking if it did have letters you were checking if it did.

 

So....

 

Correct name -> return error

Wrong name -> run script

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.