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.
var subscribe = function(){
var dataString = $("#subinput").val();
$.ajax ({
url: '<?php echo $path ?>',
type: 'POST',
data: { email : dataString },
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);
}
});
see the line
data: { email : dataString }
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..
<?php
$o = array();
if(!isset($_POST['email']) OR empty($_POST['email'])){
$o['err'] = '1';
$o['message'] = 'Please do not leave this field blank';
echo json_encode($o);
exit();
}
$email = $_POST['email'];
.......other code
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.
Replace this:
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);
}
});
with this:
var subscribe = function(){
var dataString = $("#subinput").val();
$.ajax ({
url: '<?php echo $path ?>',
type: 'POST',
data: 'email=' + dataString,
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);
}
});
val() actually returns the value of an element.
var dataString = $("#subinput").val();
// ...
data: 'email=' + dataString.value,
You used .val() to get the value of #subinput, which is a string. Then, you attempted to read a "value" property of that string, which does not exist. When you concatenate the undefined value with "email=", JavaScript converts it to the string "undefined".
To fix this, you can just change dataString.value
to dataString
. However, for the sake of those who use the +
symbol in their e-mail addresses (especially Gmail users), which your PHP code would interpret as a space, you probably should change the entire line to:
data: {email: dataString},
Passing a JavaScript object with the name-value pairs to jQuery allows it to properly query string escape the +
.