Jump to content


Photo

Validation using PHP not JS


  • Please log in to reply
1 reply to this topic

#1 aeafisme23

aeafisme23
  • Members
  • PipPipPip
  • Advanced Member
  • 121 posts
  • LocationUSA

Posted 09 May 2006 - 03:38 PM

This code works if i keep the action set to form.php which is the same page. But i want it to go to process.php (action="process.php") and echo the results. HOWEVER, if i change the action to another page it will not validate. Any ideas, suggestions?

........FORM.PHP.......<br>
<form method="POST" action="form.php">
<table border="0" cellpadding="0" cellspacing="0" bordercolor="#000000" width="600" >
  <tr>
    <td width="100" align="right">User Name:</td>
    <td width="25">&nbsp;</td>
    <td width="200">
    <input type="text" name="user_name_input" size="30" value="<? echo $user_name_input; ?>"></td>
      <td width="275">
      <? // only validate form when form is submitted
        if(isset($submit_button)){
            $error_msg='';
            if(trim($user_name_input)=='' || strlen(trim($user_name_input)) < 3 || strlen(trim($user_name_input)) > 12) {
                $error_msg.="Username must be 3 to 12 chars. long<br>";
            }
            // display error message if any, if not, proceed to other processing
            if($error_msg==''){
                // other process here
            } else {
                echo "<font color=red>$error_msg</font>";
            }
        }
        ?>
    </td>
  </tr>
  <tr>
    <td width="100" align="right">Email:</td>
    <td width="25">&nbsp;</td>
    <td width="200">
    <input type="text" name="email_input" size="20" value="<? echo $email_input; ?>"></td>
    <td width="275">
    <?
// only validate form when form is submitted
if(isset($submit_button)){
    $error_msg='';
    if(trim($email_input)=='') {
        $error_msg.="Please enter an email<br>";
    } else {
        // check if email is a valid address in this format username@domain.com
        if(!ereg("[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]", $email_input)) $error_msg.="Please enter a valid email address<br>";
    }
    // display error message if any, if not, proceed to other processing
    if($error_msg==''){
        // other process here
    } else {
        echo "<font color=red>$error_msg</font>";
    }
}
?></td>
  </tr>
  
    <tr>
    <td width="100">&nbsp; </td>
    <td width="25">&nbsp;</td>
    <td width="200">
    <input type="submit" value="   Submit   " name="submit_button"></td>
    <td width="275">&nbsp;</td>
  </tr>
</table>
</form>


#2 jeremywesselman

jeremywesselman
  • Members
  • PipPipPip
  • Advanced Member
  • 154 posts
  • LocationIndependence, KY

Posted 09 May 2006 - 06:41 PM

It does not validate when you change the form action because all of your validation process is located on this page. If you try to change the action, once the submit button is clicked, it automatically goes to the form action page.

To fix this, you need to move all of your form processing to the other page. OR just echo the data after it is processed with this page.

[!--coloro:#990000--][span style=\"color:#990000\"][!--/coloro--]Jeremy[!--colorc--][/span][!--/colorc--]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users