proctk Posted February 4, 2008 Share Posted February 4, 2008 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; } ?> Quote Link to comment Share on other sites More sharing options...
mikefrederick Posted February 4, 2008 Share Posted February 4, 2008 onsubmit='return validatefunction();' if the function returns true, form submits to your action page, otherwise fails Quote Link to comment Share on other sites More sharing options...
haku Posted February 4, 2008 Share Posted February 4, 2008 For that to work, you need your form checking function to return either a value of true or false. So build that into your form - if all checks are ok, the function returns true, else if any of the checks are failed, it returns false. Quote Link to comment Share on other sites More sharing options...
proctk Posted February 4, 2008 Author Share Posted February 4, 2008 here is the mysql statement mysql_query("UPDATE wishlist SET giver_name = '$give_by', familymember = '$give_to', giver_change_date = '$receive_date', notes = '$notes' WHERE wish_id = '$giftID'")or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
proctk Posted February 4, 2008 Author Share Posted February 4, 2008 I'm not an expert at this how can I put your recommendations into action thank you Quote Link to comment Share on other sites More sharing options...
mikefrederick Posted February 4, 2008 Share Posted February 4, 2008 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();"> Quote Link to comment Share on other sites More sharing options...
proctk Posted February 4, 2008 Author Share Posted February 4, 2008 Thanks for the post. any idea how to do the javascript piece or where I can find a tutorial. Quote Link to comment Share on other sites More sharing options...
mikefrederick Posted February 4, 2008 Share Posted February 4, 2008 <script type="text/javascript"> function validate(frm) { if(frm.field1.value=="") { alert("Message 1"); frm.field1.focus(); return false; } if(frm.field2.value=="") { alert("Message 2"); frm.field2.focus(); return false; } </script> and so on Quote Link to comment Share on other sites More sharing options...
mikefrederick Posted February 4, 2008 Share Posted February 4, 2008 need an closing } for the function up there Quote Link to comment Share on other sites More sharing options...
proctk Posted February 4, 2008 Author Share Posted February 4, 2008 Thanks for the post, are you suggesting that I change how I do all the validation, My initial goal was to have the text change color when validation failed Quote Link to comment Share on other sites More sharing options...
mikefrederick Posted February 4, 2008 Share Posted February 4, 2008 so then give each <td> that you are checking a different id. and then add in document.getElementById('field1').style.color="#xxxxxx"; if it returns false, or else set it back to what the original color was Quote Link to comment Share on other sites More sharing options...
proctk Posted February 4, 2008 Author Share Posted February 4, 2008 how would I call a php function Quote Link to comment Share on other sites More sharing options...
mikefrederick Posted February 4, 2008 Share Posted February 4, 2008 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. Quote Link to comment Share on other sites More sharing options...
mikefrederick Posted February 4, 2008 Share Posted February 4, 2008 if you mean that the input name is set at a php variable then you would call it just like you always would <?=$whatever?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.