Click here to Skip to main content
16,004,761 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys, just had a quick question about something, if it was possible.

I have this div:

HTML
<div id="signup-response"></div>
which outputs the message of responses depending on what the user enters.


my php side:
PHP
$existingSignup = mysql_query("SELECT * FROM newsletter_emails WHERE email='$email'");
            if(mysql_num_rows($existingSignup) < 1){

                $insertSignup = mysql_query("INSERT INTO newsletter_emails (email,category) VALUES ('$email', '9')");
                if($insertSignup){ //if insert is successful
                    $status = "success";
                    $message = "You've successfully subscribed!";

                    $date = date("o/m/d");
                    $insertYo = "INSERT INTO activity ( date, action, icon) VALUES ('$date', 'user has subscribed', 'fa-envelope-o')";
                    mysql_query($insertYo) or die('MySQL Error.');
                }


I want to send an ALERT once the user has successfully subscribed, however i tried this code
PHP
echo '<script>alert("this is the response msg.");</script>';
in replace of the
PHP
$message = "You've successfully subscribed!";


but it didn't work. My javascript is:

C#
//check the form is not currently submitting
        if($(this).data('formstatus') !== 'submitting'){

            //setup variables
            var form = $(this),
                formData = form.serialize(),
                formUrl = form.attr('action'),
                formMethod = form.attr('method'),
                responseMsg = $('#signup-response');


            //add status data to form
            form.data('formstatus','submitting');

            //send data to server for validation
            $.ajax({
                url: formUrl,
                type: formMethod,
                data: formData,
                success:function(data){

                    //setup variables
                    var responseData = jQuery.parseJSON(data),
                        klass = '';

                    //response conditional
                    switch(responseData.status){
                        case 'error':
                            klass = 'alert alert-error';
                        break;
                        case 'success':
                            klass = 'alert alert-success';
                        break;
                    }

                    //show reponse message
                    responseMsg.fadeOut(200,function(){
                        $(this).removeClass('response-waiting')
                               .addClass(klass)
                               .text(responseData.message)
                               .fadeIn(200,function(){
                                   //set timeout to hide response message
                                   setTimeout(function(){
                                       responseMsg.fadeOut(200,function(){
                                           $(this).removeClass(klass);
                                           form.data('formstatus','idle');
                                       });
                                   },3000)
                                });
                    });
                }
            });
        }


my HTML form:

XML
<form id="newsletter-signup" action="?action=signup" method="post">
         <input type="text" size="37" placeholder="Enter your email" name="signup-email" id="signup-email"/>
         <input type="submit" value="SUBMIT" style="padding: 8px;" id="signup-button"/>
         <div id="signup-response"></div>
                                    </form>



Do you guys happen to know what it is i'm doing wrong?

The alert box does seem to trigger once i do this:

C#
var form = $(this),
                formData = form.serialize(),
                formUrl = form.attr('action'),
                formMethod = form.attr('method'),
                responseMsg = $('#signup-response').text();
                    swal({
                        title: "Success",
                        text: "You've successfully subscribed!",
                        type: "success",
                        timer: 3000,
                        allowOutsideClick: "true"
                    });


but it doesn't pass out that response from the php side if the user entered something correctly or not.
Posted
Updated 12-Jan-15 1:21am
v2
Comments
Dennis Baberich 12-Jan-15 8:16am    
Do a console.log(data) in your success function and check what's in there, the problem is quite obvious. I am sure you'll figure it out Wink | ;-)
[no name] 12-Jan-15 8:58am    
i tried that, and I get this:
Uncaught TypeError: undefined is not a function
sub-validater.js:30 {"status":"error","message":"Please enter an email address!"}

I don't understand.
Dennis Baberich 12-Jan-15 9:25am    
Uh.. You are clearly not echoing $message...
Your clientcode does not know what your server is doing unless it's telling him what he's doing. Therefore, your php needs to output something for your client.

1 solution

You have not echo the $message back to the calling page. Let's do an experiment, create 2 pages as follows:
ajax_request.html
XML
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
</script>

<script>
$(document).ready(function(){
    $.ajax( "ajax_response.php" )
      .done(function(result) {
        alert( result );
      })
      .fail(function() {
        alert( "error" );
      })
});
</script>
</head>
<body>
</body>
</html>

then, ajax_response.php:
PHP
<?php
$message = "You've successfully subscribed!";
echo $message;
?>


and test it.
If in doubt, always refer to the relevant jQuery documentation[^] and try out the examples to better understand its usage.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900