Merijndk
Merijndk

Reputation: 1693

storing html(response) in var

Im using ajax to send a form without reloading. I use the following method for this:

My mail sender:

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    mailTo($to, $subject, $message, $headers); 
    echo "0";
}else{
    echo "1";
}

The script that sends the info to the php file above

$(document).ready(function(){
    $('#submitbtn').click(function(){
        $.post("send.php", $("#mycontactform").serialize(),  function(response) {   
            $('#success').html(response);
        });
        return false;
    });
});

Whenever I give a valid email and its send it returns 0. when it fails it returns 1. Now instead of displaying this value in the div #SUCCESS I want to store this value in a var so I can see if the mail is send or not. and if send can hide the contact form.

I came up with this:

$(document).ready(function(){
    $('#submitbtn').click(function(){
        $.post("send.php", $("#mycontactform").serialize(),  function(response) {   
            var x = html(response);
            if (x == 0){
            $('#mycontactform.hide').hide;
                document.getElementByID('succes').innerHTML = 'Message send';
            }else{
                //display some error message
            }
        });
        return false;
    });
});

But its not working. anyone know why. I assume it has something to do with var x = is not storing Html response

Upvotes: 0

Views: 562

Answers (2)

Deepa MG
Deepa MG

Reputation: 196

You can prepare your html after you get response.and then append it to the previous devision of where ever you want.It will avoid refreshing the page.

Can do something like this

 $(document).ready(function()
        {
         var html = '';
         html += '<div class="item col-xs-12 col-lg-6">';
         html += '<div class="thumbnail">';
         html += '<div class="caption">';
         html += '<h4 class="group inner list-group-item-heading">';
         html += '<b>'+data.subject+'</b></h4>';
         html += '<h5><b>'+data.projectname+'</b></h5><br />';
         html += '</div>';
         html += '</div></div>';
         $('#content').append(html);
        });

content is id of my division.i m preparing html and appending it to content.data is response .

Upvotes: 0

Steve Claridge
Steve Claridge

Reputation: 11090

In your first snippet, you did this:

$('#success').html(response);

Here, html() is a function of $, in your second snippet you did this:

var x = html(response);

But presumably you have no global function called html()? You probably want:

var x = parseInt( response,10 );

I also highly recommend looking into using Chrome/FireFox/IE DevTools in order to debug stuff like this in the future.

Upvotes: 1

Related Questions