Ramtree Posted July 12, 2009 Share Posted July 12, 2009 i have a php page where user fills up the forms. this is the code i use: if(empty($_POST['form'])) { echo "please fill in the forms"; } else { mysql_query($sql); } when the page loads, it would display "please fill in the forms". i want to prevent the php code from running when the user first go to the page. please tell me how to do so, thanks. Link to comment https://forums.phpfreaks.com/topic/165675-prevent-code-from-running-when-page-loads/ Share on other sites More sharing options...
ignace Posted July 12, 2009 Share Posted July 12, 2009 if(!empty($_POST) && empty($_POST['form'])) Link to comment https://forums.phpfreaks.com/topic/165675-prevent-code-from-running-when-page-loads/#findComment-873937 Share on other sites More sharing options...
Ramtree Posted July 12, 2009 Author Share Posted July 12, 2009 $error=0; if(!empty($_POST) && empty($_POST['form1'])) { echo "Please input the Question."; echo "<br>"; $error++; } if(!empty($_POST) && empty($_POST['form2'])) { echo "Please input the Answer Option 1."; echo "<br>"; $error++; } if(!empty($_POST) && empty($_POST['form3'])) { echo "Please input the Answer Option 2."; echo "<br>"; $error++; } if(!empty($_POST) && empty($_POST['form4'])) { echo "Please input the Answer Option 3."; echo "<br>"; $error++; } if(!empty($_POST) && empty($_POST['form5'])) { echo "Please input the Answer Option 4."; echo "<br>"; $error++; } if(!empty($_POST) && empty($_POST['form6'])) { echo "Please input the Correct Answer."; echo "<br>"; $error++; } if($error==0) { echo "Request submitted successfully."; mysql_query($sql); } if were to use the code like this, the page would display "Request submitted successfully." and blank data would be inserted in to the database. i want to prevent this code from running as well. pls help me, thanks. Link to comment https://forums.phpfreaks.com/topic/165675-prevent-code-from-running-when-page-loads/#findComment-873939 Share on other sites More sharing options...
ignace Posted July 12, 2009 Share Posted July 12, 2009 if (!empty($_POST)) { if (empty($_POST['field'])) { $errors[] = 'field=empty'; } else { //field is not empty } .. if (sizeof($errors)) { print 'errors'; } else { //process } } Link to comment https://forums.phpfreaks.com/topic/165675-prevent-code-from-running-when-page-loads/#findComment-873944 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.