Jump to content

form validation


proctk

Recommended Posts

Hi

 

The below code validates my form and makes the text red if field is blank.  My problem is i'm going to add code that will update info in a mysql table.  how do I stop the mysql code (not written yet) if validation failed.

 

thank you.

 

I'll write the mysql statement and post soon

 

function error_bool($error, $field) {
         if($error[$field]) {
	 echo '<td style="width:25%; color:red;" class="tableRowBottomBorder">';
          
         }
        else {
           echo '<td style="width:25%;" class="tableRowBottomBorder">';
        }
    }

function show_form() {
global $HTTP_POST_VARS, $print_again, $error;

}
if(isset($_POST["updateGift"])) {
    check_form();

} else {
    show_form();
}


function check_form()
{
global $HTTP_POST_VARS, $error, $print_again;

$error['givenBy'] = false;
    if($_POST["givenBy"]=="") {
        $error['givenBy'] = true;
         $print_again = true;
   
    }
$error['givenTo'] = false;
    if($_POST["givenTo"]=="") {
        $error['givenTo'] = true;
         $print_again = true;
   
    }

$error['receiveDate'] = false;
    if($_POST["receiveDate"]=="") {
        $error['receiveDate'] = true;
         $print_again = true;
   
    }

?>

Link to comment
Share on other sites

the php has nothing to do with it, you want to put that mysql_query on a seperate php page and put the action of the form to that page. in the javascript function, return false if field is empty (or whatever) else return true for each field in the form. and then do <form onsubmit="return javascriptfunction();">

Link to comment
Share on other sites

thinking it over, that would only make one field a different color, if you want to make them all red you only implement one alert statement and one if statement and use or statements instead like if(frm.field1.value=='' || frm.field2.value=='' and so on ) { rest of script, and then if returns false change all those <td> colors using the method i described above.

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.