Jump to content

[SOLVED] if empty?


merylvingien

Recommended Posts

Hi guys, i have a form with a nifty bit of ajax code going on that checks a email address is valid.

This works out ok, and if the email is valid it displays a hidden field with a true value or if it is invalid it displays different hidden field with a false value.

This value gets carried over to the next page.

 

The trouble is, if a user fills out some of the form including the email field and then for some reason refreshes the page, the hidden field is no longer shown, along with the valid message or invalid message and on the next page where the value is posted there is nothing.

 

I have tried

if (empty($emailcheck)) {die etc

and that stops the user going any further, but i still get a Notice: Undefined index: emailcheck

I have also tried

if (empty($emailcheck)) {$emailcheck == "false";}

But that dont work either...

 

here is the code as it is at the moment:

 

$emailcheck = $_POST['emailcheck'];


if (empty($emailcheck)) {die ('<p>There appears to be an error with the email validation, please go back and re-enter your email address</p>');}
 else
 if ($emailcheck == 'false') {die ('<p>It appears that you have entered an invalid email address, please hit the back button in your browser and check that you have entered a valid email address.</p>');}
 else
  {

 

How can i get round this notice?

Link to comment
https://forums.phpfreaks.com/topic/181498-solved-if-empty/
Share on other sites

Tried it LOL

if (isset($_POST['emailcheck']) as $emailcheck) {
if ($emailcheck == 'false') {die ('bla bla');}
}
else

Like that you mean?

 

I tried that out before coming here, if it is not set, then it just carries on as normal, without checking if the email is valid.

Link to comment
https://forums.phpfreaks.com/topic/181498-solved-if-empty/#findComment-957403
Share on other sites

rarebit that part works, i just can't get the other bit to work now.

 

Should this work? Cause it dont..

 

if (isset($_POST['emailcheck'])) {
 $emailcheck = 'emailcheck';
 if ($emailcheck == 'false') {die ('<p>bla bla bla</p>');}
 }
 else
 if (!isset($_POST['emailcheck'])){die ('bla bla');}
     else
  {

Link to comment
https://forums.phpfreaks.com/topic/181498-solved-if-empty/#findComment-957488
Share on other sites

the reason it disappears is because the AJAX was run in order to get that hidden input.  when the user refreshes the page, the AJAX then needs to be run again to re-obtain that value.

 

just do a valid email check in the form processing script.  this way, if somebody refreshes the page, the process script will catch them.

 

you should always have a secondary line of defense with PHP.  AJAX/javascript is all fine and dandy, it looks nice and can be a great tool for form efficiency, but it can be easily manipulated (as well as turned off), so don't ever, ever count on it.

Link to comment
https://forums.phpfreaks.com/topic/181498-solved-if-empty/#findComment-957490
Share on other sites

yes, but you were wondering why the hidden value was disappearing.  you sounded like you counting on that value being there which isn't a good thing.

 

and just use:

 

if ((!isset($_POST['emailcheck'])) || (empty ($_POST['emailcheck'])))

 

empty() will check if it's FALSE.

Link to comment
https://forums.phpfreaks.com/topic/181498-solved-if-empty/#findComment-957496
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.