1

I'm new in JQuery. I have managed to return data to JQuery from php script but now I get to see the message <!--Separate connection with sessions --> when I show the returned message from php script to the user and I only want to display my message to the user using alert().

  1. Can someone explain what is that meaning?
  2. How to avoid it to be shown to the alert box?

PHP code:

<?php
    include "initialize.php";
    if(empty($_POST) === false){
        global $con;
        $message = "";
        $email = trim($_POST["email"]);
        if(isSubscribed($email) === false){
            $message = "Thank you for subscribing to our latest updates";
            mysqli_query($con,"INSERT INTO subscribers(email) VALUES('".$email ."')");
        }
        else{
            $message = "You already subscribed.";
        }
        echo $message;
    }
?>

JQuery Code:

$(function () {
    $('#frms').on('submit', function (e) {
        var email = $("#email").val();
        if (!validateEmail(email)) {
            alert('Error: Make sure you provided a valid email address');
        }
        else {
            $.ajax({
                type: 'post',
                url: 'initialize/subscribe.php',
                data: $('#frms').serialize(),
                success: function (data) {
                    alert(data);
                }
            });
        }

        e.preventDefault();
    });
});
1
  • 1
    what is that meaning - what? Commented Feb 9, 2017 at 12:50

2 Answers 2

2
<!--Separate connection with sessions -->

This is an HTML comment, and it probably means when you call the PHP script from your jQuery, that HTML is also added somewhere to the response.

i.e. Your PHP "page" is not only echoing your message; it is echoing that HTML as well.

A quick fix is to kill the PHP script immediately after you echo your message, to prevent any other output from being returned to the jQuery.

echo $message;
die;   // Add this

This will terminate the script after you echo your message, and not allow any other output to be generated.

If you still see the HTML after adding die, it probably means the HTML is added before you echo your message. In which case it might be in initialize.php and you would have to prevent that other script from echoing anything as well.

Sign up to request clarification or add additional context in comments.

3 Comments

You just need to find <!--Separate connection with sessions --> it's somewhere in your project.
Thanks BadHorsie, I found out that this output <!--Separate connection with sessions --> was being generated on this script initialize.php
@MCi Yes I said that in my answer.
1

return data like this :

$returnArr=array(); returnArr['message']=$message;

echo json_encode(returnArr);

exit;

and at client side use this code alert(data.message);

4 Comments

I did try that code and is now giving me this message undefined
This what i get <!--Separate connection with sessions --> {"message":"You already subscribed."} when i output data alert(data) only
use this: var obj = jQuery.parseJSON(data); alert( obj.message );
Thanks for your effort. your help lead me to find the solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.