Hello there,

I/ve been working on a script that dynamically fills a form with data from a DB with SQL. This all worked fine until I wanted to build in some validation so I could be sure no-one delete the formfield and then send an empty field to the database.

I tried using the following code...

<html>
<body>
<font face="arial" >
<?php
$conn=odbc_connect('test','','');
if (!$conn)
{exit("Connection Failed: " . $conn);}
$sql="SELECT NAAM FROM test1 WHERE Id1 IN (355)";
$rs=odbc_exec($conn,$sql);
if (!$rs)
{exit("Error in SQL");}

while (odbc_fetch_row($rs))
{

$naam=odbc_result($rs,"NAAM");

$newnaam = $naam;

if ($naam == "") echo "<td>&nbsp;</td></tr>";
else

echo "<td>$naam</td></tr>";

}

?>

<?php
// only validate form when form is submitted

if(isset($submit_button)){
$error_msg='';
if(trim($newnaam)=="" ) {
$error_msg.="U mag geen leeg veld invoeren<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>"; 
} 

}

?>
<form action="updatehandler.php" method="post">

NAAM PAKKET: <input type="text" name="naampakket" size="50" value=" <?php echo $newnaam; ?> "><br/>

<input type="Submit" name="submit_button" value="Wijzigen">
</form>

<?php
odbc_close($conn);
echo "</table>";
?>

</font>
</body>
</html>


I hope someone can help me...

    To be extra clear on this one

    I want to make sure that the form is checked before it is send to the updatehandler.php...

    (Ahum...I forgot to mention that...)

      Well that won't happen because PHP is server side and can not check any $_POST variables until submit has been pressed and your action will happen when it has had the submit button pressed, you would have to write to code to submit the page to itself and then do any validation or what not and if written properly then use a header() function to redirect it to the update page.

      When posting code HTMP PHP whatever please use the BB Code provided in these forums, it makes it easier on everyone. See how by clicking the link in my signature.

        Thank you for the information about the BB code. I'm trying to do something with

        if(isset($_POST[submit_button])) 

        I hope that will help me...

        If anyone has a better idea please let me know!

        Thanks in advance.

          Here is some sample script that will do what you are wanting to do. This will when submitted validate all fields (or those you select) then you can either use a link to the next page or use a javascript to send the user to the page you want then on, it is in the comments of the script.

          <html>
          <body>
          <?php 
          /*Only verify/validate form when it is submitted program name: form.php */
          if(isset($_POST[submit])){
            $error='';//initialize $error to blank
            if(trim($_POST[first_name_input])==''){
                $error .="You left out your first name.<br />"; 	
            }
            if(trim($_POST[last_name_input])==''){
                $error .="You left out your last name.<br />"; 	
            }
            if(trim($_POST[username])=='' || strlen(trim($_POST[username])) < 6 ||strlen(trim($_POST[username])) >12){
                $error.="Please enter a username between 6 and 12 characters!<br />"; //concatenate the $error Message with a line break
            }
            if(trim($_POST[password])=='' || strlen(trim($_POST[password]))< 6){
                $error.="Your password must be at least 6 characters in length!<br />";//concatenate more to $error  
          } if(trim($_POST[email])==''){ $error.="An email address is required!<br />"; } else { if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST[email])) { $error="The e-mail you entered was not in the proper format!"; } } if($error==''){//Hmmmm no text is in $error so do something else, the page has verified and the email was valid // so uncomment the line below to send the user to your own success page or wherever (swap yourpage.php with your files location). Will not work with javascript turned off so you could just use a link instead! //echo "script type=\"text/javascript\">window.location=\"yourpage.php\"<script>";
          } else{ echo "<span style=color:red>$error</span>"; }
          } ?> <form method="post" action="<?php echo $PHP_SELF ?>"> <table border="1" cellpadding="7" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1"> <tr> <td width="16%" align="right">First Name</td> <td width="84%"> <input type="text" name="first_name_input" size="20" value="<?php echo $_POST['first_name_input']; ?>"></td> </tr> <tr> <td width="16%" align="right">Last Name</td> <td width="84%"> <input type="text" name="last_name_input" size="20" value="<?php echo $_POST['last_name_input']; ?>"></td> </tr> <tr> <td width="16%" align="right">Username</td> <td width="84%"> <input type="text" name="username" size="20" value="<?php echo $_POST[username]; ?>"> (between 6 to 15 characters)</td> </tr> <tr> <td width="16%" align="right">Password</td> <td width="84%"> <input type="password" name="password" size="20" value="<?php echo $_POST[password]; ?>"> (must be at least 4 characters)</td> </tr> <tr> <td width="16%" align="right">Email</td> <td width="84%"> <input type="text" name="email" size="20" value="<?php echo $_POST[email]; ?>"></td> </tr> <tr> <td width="16%" align="right"> </td> <td width="84%"> <input type="submit" value="Register" name="submit"></td> </tr> </table> </form> </body> </html>

            Hey, Houdini.

            Thanks for your "magic"! 🙂

              Write a Reply...