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
https://forums.phpfreaks.com/topic/89305-form-validation/
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
https://forums.phpfreaks.com/topic/89305-form-validation/#findComment-457303
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
https://forums.phpfreaks.com/topic/89305-form-validation/#findComment-457331
Share on other sites

Archived

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

×
×
  • 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.