Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 尝试使用jQuery与php echo连接来显示suces消息_Javascript_Php_Jquery - Fatal编程技术网

Javascript 尝试使用jQuery与php echo连接来显示suces消息

Javascript 尝试使用jQuery与php echo连接来显示suces消息,javascript,php,jquery,Javascript,Php,Jquery,我试图用jQuery消息来恢复密码系统,但我遇到了一个问题。 下面的代码工作正常,当我点击按钮恢复用户名和密码时,我收到一封邮件,其中包含您的数据。。 但是我想把用户的电子邮件放在消息中。像这样: 将您的数据发送给email@example.com! 我试着在我的php中这样做 else { echo 'sucess'; //here I show the jQuery message echo $result['email']; //and then I wa

我试图用jQuery消息来恢复密码系统,但我遇到了一个问题。 下面的代码工作正常,当我点击按钮恢复用户名和密码时,我收到一封邮件,其中包含您的数据。

。 但是我想把用户的电子邮件放在消息中。像这样:

将您的数据发送给email@example.com!

我试着在我的php中这样做

else {
        echo 'sucess'; //here I show the jQuery message
        echo $result['email']; //and then I want to show my $result['email']
        return; 
      }
我已经这样试过了:

echo 'sucess'.$result['email'].'';
但我总是遇到同样的问题,我在jQuery中输入:

else
  {
     alert('Error in system');
}
如果我不把这个回音放在$result['email']中,success消息可以正常工作,但是当我尝试回音我的$result['email']时,我总是在这个jQuery条件下输入。 有人知道为什么会这样吗

我的php:

   switch ($action){
    case 'recover':
        $email = $_POST['email'];

        if($email == ''){
        echo 'errorempty';
        }else{  
        $searchEmail = $pdo->prepare("SELECT * FROM admins WHERE email=:email"); 
        $searchEmail->bindValue(":email", $email);   
        $searchEmail->execute();
        $num_rows = $searchEmail->rowCount();
        $result = $searchEmail->fetch(PDO::FETCH_ASSOC);

        if($num_rows <=0 )
        {

        echo 'erroremail';
        return;
        }

        else {

        echo 'sucess';
        echo $result['email'];
        return; 
        }
        }
        break;
        default:

        echo 'Error';

    }
}
$('#recover').submit(function(){
        var login = $(this).serialize() + '&action=recover';
        $.ajax({
            url: 'switch/login.php',
            data: login,
            type: 'POST',
            success: function(answer){
                if(answer== 'erroempty'){
                    $('.msg').empty().html('<p class="warning">Inform your email!</p>').fadeIn('slow');
                }
                else if (answer == 'erroemail'){
                    $('.msg').empty().html('<p class="error">Email dont exist!</p>').fadeIn('slow');

                }
                else if(answer == 'sucess'){
                    $('.msg').empty().html('<p class="sucess">Email sent with your data..</p>').fadeIn('slow');
                    window.setTimeout(function(){
                        $(location).attr('href','dashboard.php');
                 },1000);
               }
                else{
                    alert('Error in system');
                }

            },
            beforeSend: function(){
                $('.loginbox h1 img').fadeIn('fast');
            },
            complete: function(){
                $('.loginbox h1 img').fadeOut('slow');
            },
            error: function(){
                alert('Error in system');
            }

       });
        return false;
    });
开关($action){
“恢复”案例:
$email=$_POST['email'];
如果($email=''){
回声“errorempty”;
}否则{
$searchEmail=$pdo->prepare(“从管理员处选择*,其中email=:email”);
$searchEmail->bindValue(“:email”,$email);
$searchEmail->execute();
$num_rows=$searchEmail->rowCount();
$result=$searchEmail->fetch(PDO::fetch_ASSOC);

如果($num_rows您可以像这样简单地回显
$email

$email=$result["email"];
echo $email;
然后在ajax成功函数中

if(answer.email)
{
 $('.msg').empty().html('<p class="sucess">Email sent with your data..'+answer.email+'</p>').fadeIn('slow');
}
if(应答.电子邮件)
{
$('.msg').empty();
}

问题是,服务器端只返回非结构化数据,而客户端部分只接收一个普通字符串,如
sucessdummy@example.com
在应答变量中

这个answer变量与不匹配的字符串进行了比较,但您的脚本以错误结尾

您的服务器端代码可能类似于

$result = array('msg'=>'success','mail'=>$result['email']);
header('Content-type: application/json');
echo json_encode($result);

您必须在jquery ajax调用中传递dataType:'json'选项才能使其工作,并且可以访问回调函数中的数据,如answer.msg、answer.mail

当涉及到JS时,什么是
answer
?只需将其添加到JS消息->
$('.msg').empty().html('

为'+$('input)发送的电子邮件中[name=“email”]).val()+“

”).fadeIn('slow');
由于您在客户端拥有该值,因此无需从php传回该值。谢谢,它工作了!但现在我遇到了另一个问题,当我这样做时,在php中回显“success”,然后我想调用我的函数sendMail(),然后像这样做:else{echo'success';$msg='Hi,这是您的数据';sendMail('Recover your data!',$msg,MAILUSER,SITENAME,$result['email'],$result['nome']);return;}但是它再次显示了系统错误中的错误!在发送邮件后,将echo“success”放在最后一行。现在,我的sendMail函数中有一个错误,但很奇怪,因为我没有任何错误消息!非常感谢,您的解决方案也可以!但我没有使用,因为我还不太了解json数据!