Jump to content

Archived

This topic is now archived and is closed to further replies.

aeafisme23

Validation using PHP not JS

Recommended Posts

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?

[code]
........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>
[/code]

Share this post


Link to post
Share on other sites
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--]

Share this post


Link to post
Share on other sites

×

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.