Jump to content

Validation using PHP not JS


aeafisme23

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

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.