I’m not really certain where the problem lies but I am passing a variable through ajax and it is not being caught if I pass a blank when I check it with !$varname
Here is my ajax function:
var subscribe = function(){
var dataString = $("#subinput").val();
$.ajax ({
url: '<?php echo $path ?>',
type: 'POST',
data: 'email=' + dataString.value,
datatype: 'JSON',
success: function(results){
if(results.err == '1'){
$('.onconfirmation').css('color','#f00');
}else{
$('.onconfirmation').css('color','#5A5A5A');
}
$('.onconfirmation').innerHTML(results.message);
$('.onconfirmation').fadeIn();
//alert(results);
}
});
and here is my PHP:
<?php
$email = $_POST['email'];
if(!$email){
$o['err'] = '1';
$o['message'] = 'Please do not leave this field blank';
}/*elseif(filter_var($email, FILTER_VALIDATE_EMAIL)){
$o['err'] = '1';
$o['message'] = 'Please enter a valid email address';
}else{
$o['err'] = '0';
$o['message'] = 'Thank you for your subscription';
}*/
ob_start();
var_dump($o);
$out = ob_get_clean();
mail('[REDACTED]','debug',$out);
//$o = json_encode($o);
//return ($o);
?>
As you can see it’s in a debugging state at the moment, but if I pass a blank value through into this, the email I am getting is NULL. If I email myself the $email variable instead of the $out variable, the email I get is undefined, but if I remove the ! from the if statement, the email I get is:
array(2) {
["err"]=>
string(1) "1"
["message"]=>
string(36) "Please do not leave this field blank"
}
I’m sure I am just missing something awfully simple, I always am, but I honestly can’t figure this one out. Any help would be massively appreciated. Cheers.
see the line
Once you have the val() you shouldn’t use ” .value ” and when POST-ing data this way is the correct way to add keys and values.
At PHP do those things..
The exit() stops PHP from reading the next lines at your file so IF emial is not set or empty it wont do any further tasks.