0

我不确定问题出在哪里,但是我正在通过 ajax 传递一个变量,如果我在使用 !$varname 检查它时传递一个空白,它不会被捕获

这是我的ajax函数:

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);
   }
 });

这是我的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);
?>

正如您所看到的,它目前处于调试状态,但如果我将一个空白值传递给它,我收到的电子邮件是NULL. 如果我通过电子邮件给自己发送$email变量而不是$out变量,我收到的电子邮件是undefined,但是如果我!从 if 语句中删除 ,我收到的电子邮件是:

array(2) {
  ["err"]=>
  string(1) "1"
  ["message"]=>
  string(36) "Please do not leave this field blank"
} 

我确定我只是错过了一些非常简单的东西,我一直都是,但老实说,我无法弄清楚这一点。任何帮助将不胜感激。干杯。

4

3 回答 3

1
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);
   }
 });

看线

数据:{电子邮件:数据字符串}

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.

于 2013-01-04T10:46:20.927 回答
1

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.

于 2013-01-04T10:58:00.053 回答
0
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 +.

于 2013-01-04T11:12:03.500 回答