OzzC
OzzC

Reputation: 821

Trying to show sucess message with jQuery concatenated with php echo

Im trying to do a recover password system with jQuery messages and Im having a problem. The code below is working fine, when I click in the button to recover my username and password I get the message <p class="sucess">Email sent with your data..</p>. But I want to put the email of the user in the message. Like this:

<p class="sucess">Email sent with your data for [email protected]!</p>

Im trying like this in my php

else {
        echo 'sucess'; //here I show the jQuery message
        echo $result['email']; //and then I want to show my $result['email']
        return; 
      }

I already try like this:

echo 'sucess'.$result['email'].'';

But I have always the same problem, Im entering here in my jQuery:

else
  {
     alert('Error in system');
}

And if I dont put this echo in $result['email'] the sucess message works fine, but when I try to echo my $result['email'] Im always entering in this jQuery condition. Somebody there have any idea why this is happening?

My 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';

    }
}

My jQuery:

$('#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;
    });

Upvotes: 0

Views: 833

Answers (2)

AthanasiusKirchner
AthanasiusKirchner

Reputation: 198

The problem is, that you server side is returning just unstructured data and the client side part will just receive a plain string like [email protected] in the answer variable.

This answer variable is compared with strings that do not match thought your script ends in the error case. I would go for a solution by returning some kind of structured data like json.

Your server side code could look something like

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

You have to pass the dataType:'json' option in your jquery ajax call to make it work and can access the data in the call back function like answer.msg, answer.mail

Upvotes: 1

ɹɐqʞɐ zoɹǝɟ
ɹɐqʞɐ zoɹǝɟ

Reputation: 4370

you can simple echo the $email like this

$email=$result["email"];
echo $email;

then in ajax success function

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

Upvotes: 1

Related Questions