|
Post by mjs187 on Sept 13, 2012 15:00:32 GMT -5
Hey everyone I'm having trouble with error checking in the form. It should send print out an error message if one thing is left out yet when i leave the name out of the form and fill in everything else it still prints out everything with the name blank instead of the error msg.
Here is the code:
<?php if($_POST['formSubmit'] == "Submit") { $errorMessage = ""; $varName = $_POST['formName']; if(empty($varName)) { $errorMessage .= "<p>You forgot to enter a Name!</p>"; print_r($errorMessage); } // End of Check Name. else { print_r("<p><strong>Name: </strong> " . $varName . "</p>" . "\n"); } } ?>
|
|
|
Post by Bob P on Sept 13, 2012 19:38:45 GMT -5
First, What does the HTML look like? What is the name of the form element, is it formName?
I would recommend running print_r or vardump on your $_POST array before doing anything else on this page. That way you can visually see each one of your $POST array values for each form element. It's the fastest way to debug.
Bob
|
|
zjh7
New Member
Posts: 15
|
Post by zjh7 on Sept 14, 2012 11:28:40 GMT -5
Perhaps instead of if(empty()) you could use if(!isset())
|
|
|
Post by mjs187 on Sept 16, 2012 17:49:16 GMT -5
I tried the !isset() instead of empty() and it still didnt display the error. The html shows a <p> with Name: in it and would include the info that I place in the Name box if I were to place anything in it before I submit.
Running vardump on $_POST shows that it is adding an element with an empty string so for some reason it isnt computing the if statement.
array(5) { ["formName"]=> string(0) "" ["formGender"]=> string(6) "female" ["formYear"]=> string(8) "sophmore" ["formComments"]=> string(4) "asdf" ["formSubmit"]=> string(6) "Submit" }
|
|
|
Post by Bob P on Sept 17, 2012 10:39:11 GMT -5
I ran your code and it worked - I got the error msg when I left out the name. Doublecheck your html.
Otherwise try if ($varName=="")......
Bob
|
|
|
Post by mjs187 on Sept 17, 2012 11:44:46 GMT -5
Weird. I used if ($varName == '') and it worked. Wonder why it didnt for the other methods of !isset() and empty().
Thanks
-Matt
|
|
|
Post by Bob P on Sept 17, 2012 14:32:55 GMT -5
A couple of important thoughts about checking form elements in html and php.
First, if things seem weird, stop, and clear your cache of your browser. This is very important because your browser may be reading the page from disk and keeping old values - this is impossible to know so simply clear the cache and you will be working with a fresh copy.
Second, read the link on the syllabus, week3, titled isset vs empty. You will find it is not quite what it seems. Isset will view an empty string as being set and I believe that all html form elements are saved as strings so if you don't type anything then you have an empty string and the element isset is true.
Third, I think it's best to test for actual values such as 'submit' or "" because empty and isset can be a bit tricky.
Lastly, print_r is used for mostly debugging. If you want to print out the value of a var use echo for the user. Bob
|
|
kmt51
New Member
Posts: 11
|
Post by kmt51 on Sept 17, 2012 17:49:50 GMT -5
Using print_r() was ugly anyway, put your results into variables and echo them, then format it a bit with some html and things get pretty again.
|
|