psquillace Posted January 3, 2008 Share Posted January 3, 2008 Hello All: I have a HTML form set up that works fine but if there is nothing entered in any of the fields I get a blank screen. I wanted to set it up so that it comes back with saying what is missing so I wrote out if (empty($_POST["First_Name"])) { $First_Name = FALSE; $message .= 'Please Enter Your First Name'; }else{ $data .= "&First_Name=" . urlencode(strip_tags($_POST["First_Name"])); } However, when I fill out the form and leave the first name blank, it still goes ahead and dumps my info in my database. Any thoughts on what I might be doing wrong here? Thanks, Paul Quote Link to comment https://forums.phpfreaks.com/topic/84318-if-empty-is-not-working-for-me-please-help/ Share on other sites More sharing options...
suttercain Posted January 3, 2008 Share Posted January 3, 2008 Try if ($_POST["First_Name"] == "" || $_POST["First_Name"] == NULL) { $First_Name = FALSE; $message .= 'Please Enter Your First Name'; }else{ $data .= "&First_Name=" . urlencode(strip_tags($_POST["First_Name"])); } Let me know if this works. Quote Link to comment https://forums.phpfreaks.com/topic/84318-if-empty-is-not-working-for-me-please-help/#findComment-429401 Share on other sites More sharing options...
psquillace Posted January 3, 2008 Author Share Posted January 3, 2008 Nope, it just entered it into my database. Quote Link to comment https://forums.phpfreaks.com/topic/84318-if-empty-is-not-working-for-me-please-help/#findComment-429409 Share on other sites More sharing options...
suttercain Posted January 3, 2008 Share Posted January 3, 2008 You could also try if (!isset($_POST["First_Name"])) { $First_Name = FALSE; $message .= 'Please Enter Your First Name'; }else{ $data .= "&First_Name=" . urlencode(strip_tags($_POST["First_Name"])); } Quote Link to comment https://forums.phpfreaks.com/topic/84318-if-empty-is-not-working-for-me-please-help/#findComment-429412 Share on other sites More sharing options...
suttercain Posted January 3, 2008 Share Posted January 3, 2008 Try echoing $First_Name Does anything print to the browser? Quote Link to comment https://forums.phpfreaks.com/topic/84318-if-empty-is-not-working-for-me-please-help/#findComment-429414 Share on other sites More sharing options...
psquillace Posted January 3, 2008 Author Share Posted January 3, 2008 Yes, what happens is after I fill out the form it shows me the successfull message that is in there. Thanks for signing up and then when I go look in my database, it is in there. Quote Link to comment https://forums.phpfreaks.com/topic/84318-if-empty-is-not-working-for-me-please-help/#findComment-429416 Share on other sites More sharing options...
suttercain Posted January 3, 2008 Share Posted January 3, 2008 try commenting everything out except echo $First_Name; Even if you leave the First_Name blank in the text area, does it display anything? Can you post the form code? Quote Link to comment https://forums.phpfreaks.com/topic/84318-if-empty-is-not-working-for-me-please-help/#findComment-429420 Share on other sites More sharing options...
roopurt18 Posted January 3, 2008 Share Posted January 3, 2008 <?php if (empty($_POST["First_Name"])) { $First_Name = FALSE; $message .= 'Please Enter Your First Name'; }else{ $data .= "&First_Name=" . urlencode(strip_tags($_POST["First_Name"])); } ?> What are you doing after this? If your code is set to just go ahead and process the form afterwards it doesn't matter what checks you perform, right? The correct logic for processing a form is: IF validate() THEN process() ELSE show() END I'm guessing that your code expresses this logic: IF submitted() THEN process() ELSE show() END (edit) Also, never jump to conclusions while debugging. Your topic title states if empty isn't working. How do you know that? Which test did you specifically run to make sure that empty() is failing? Perhaps empty() is working perfectly and your code is failing elsewhere. Quote Link to comment https://forums.phpfreaks.com/topic/84318-if-empty-is-not-working-for-me-please-help/#findComment-429435 Share on other sites More sharing options...
psquillace Posted January 3, 2008 Author Share Posted January 3, 2008 Yes, but then how do I tell it to stop if $_POST["First_Name"] is not valid? that is where I get lost.. Quote Link to comment https://forums.phpfreaks.com/topic/84318-if-empty-is-not-working-for-me-please-help/#findComment-429440 Share on other sites More sharing options...
awpti Posted January 3, 2008 Share Posted January 3, 2008 If $First_Name is blank after all that, try this: echo ".{$_POST['First_Name']}."; If there's a space, then $_POST['First_Name'] is a space (which is a value and will not eval as empty). Something is causing empty() and isset() to eval to true, which plainly indicates it does, definately have a value. You can also var_dump the variable and see what it shows. Proves " " is a value: <?php $string = " "; if(isset($string)) { print '$string has a value'."\n\n<br /><br />"; } if(!empty($string)) { print '$string has a value'."\n\n<br /><br />"; } echo ".$string."; // Output: . . ?> Quote Link to comment https://forums.phpfreaks.com/topic/84318-if-empty-is-not-working-for-me-please-help/#findComment-429442 Share on other sites More sharing options...
psquillace Posted January 3, 2008 Author Share Posted January 3, 2008 (edit) Also, never jump to conclusions while debugging. Your topic title states if empty isn't working. How do you know that? Which test did you specifically run to make sure that empty() is failing? Perhaps empty() is working perfectly and your code is failing elsewhere. Yes, you are correct. I did not test it properly to see if that was the case. Just newbie frustration I guess. :'( Quote Link to comment https://forums.phpfreaks.com/topic/84318-if-empty-is-not-working-for-me-please-help/#findComment-429444 Share on other sites More sharing options...
redarrow Posted January 3, 2008 Share Posted January 3, 2008 $data .= "&First_Name=" . urlencode(strip_tags($_POST["First_Name"])); your need trim aswell i am sure $data .= "&First_Name=" . urlencode(trim(strip_tags($_POST["First_Name"]))); Quote Link to comment https://forums.phpfreaks.com/topic/84318-if-empty-is-not-working-for-me-please-help/#findComment-429455 Share on other sites More sharing options...
roopurt18 Posted January 3, 2008 Share Posted January 3, 2008 Yes, but then how do I tell it to stop if $_POST["First_Name"] is not valid? that is where I get lost.. You don't tell it to stop. You want it to continue. What you don't want it to do is process. Here is a simple form class. <?php class form { // array of error messages var $errs = Array(); // constructor function form(){ // This would be a good place to check if magic quotes is on and if it is, // call stripslashes recursively on all $_POST elements } // process - this function processes the form; i.e. does DB stuff, whatever function process(){ // insert into db and display success message } // show - this function shows the form function show(){ $errs = ''; if(!empty($this->errs)){ // We have errors $errs = implode('</li><li>', $this->errs); $errs = " <div class=\"errors\"> <p> The following errors were encountered while processing your request: </p> <ul> <li>{$errs}</li> </ul> </div> "; } // prepare the default values $fields = Array( 'fname', 'lname' ); $defaults = Array(); foreach($fields as $f){ $defaults[$f] = isset($_POST[$f]) ? _$POST[$f] : ''; } // Display the form echo <<<HTML <form method="post"> {$errs} <fieldset> <legend>Name</legend> <label for="fname">First:</label> <input type="text" id="fname" name="fname" value="{$defaults['fname']}" /> <label for="lname">Last:</label> <input type="text" id="lname" name="lname" value="{$defaults['lname']}" /> <input type="submit" name="submit" value="Submit" /> </fieldset> </form> HTML; } // validate - this function validates the form; returns false if invalid function validate(){ $has_errors = false; // Assume it has no errors // If $_POST is empty, form has not been submitted, so we want to return // false (as if the form *DID* have errors) if(empty($_POST)){ return false; } // Now we can validate // First name field - required if(empty($_POST['fname'])){ $has_errors = true; $this->errs[] = "First name is required."; } // last name - required if(empty($_POST['lname'])){ $has_errors = true; $this->errs[] = "Last name is required."; } // name fields are alpha only if(!ctype_alpha($_POST['fname']) || !ctype_alpha($_POST['lname'])){ $has_errors = true; $this->errs[] = "Name fields are alphabetic only."; } // and so on for other fields // we return, using reverse logic return !$has_errors; } // do - this function runs the form function do(){ // run the form if($this->validate()){ $this->process(); }else{ $this->show(); } } } ?> Example code to run the form <?php $form = new form(); $form->do(); ?> Take a look at that, absorb it, maybe even run it on your own server and see how it works. It's not perfect code, or even how I do things, but it should give you some ideas. Quote Link to comment https://forums.phpfreaks.com/topic/84318-if-empty-is-not-working-for-me-please-help/#findComment-429470 Share on other sites More sharing options...
psquillace Posted January 3, 2008 Author Share Posted January 3, 2008 Thanks Guys for all your help with this. I will have to take a step back and re-evaluate all this to be better prepared. Thanks again, Paul Quote Link to comment https://forums.phpfreaks.com/topic/84318-if-empty-is-not-working-for-me-please-help/#findComment-429478 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.