2

Here is my script :

<script>
    $('form.subscribe').on('submit', function() {
        var that = $(this);
        url = that.attr('action');
        method = that.attr('method');
        data ={};
        that.find('[name]').each(function(index, value) {
              var that = $(this),
              name = that.attr('name'),
              value = that.val();
              data[name] = value;
        });

        $.ajax({
            url: url,
            type:method,
            data: data,
            success: function(response){
                console.log(response);
            }
        });

        return false;
     });
</script>

I have an ajax function that gives me either an error message or a success message depending on what your input was on the form.

The messages that I get come from this php script:

<?php
    header("Refresh:7; url=contact.php");
    $email = $_POST['subscribefield'];

    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        echo "Dit emailadres klopt niet";
        die();
    }

    $to = "[email protected]";
    $subject = "Abonee voor de nieuwsbrief";
    $body = "$email \n Heeft zich aangemeld voor de nieuwsbrief";

    mail($to, $subject, $body);
    echo "U heeft zich zojuist aangemeld voor de vandenberg nieuwsbrief";
?>

Right now im displaying the outputs with a console.log(). But I would like to display those in a <div> instead.

1
  • 2
    instead of the console.log have $('#divid').html(response) in success Commented Jun 9, 2016 at 10:05

5 Answers 5

5

You just have to use jQuery .html() to set the response content in a DIV.

Replace following line

console.log(response);

with

$('#idOfDiv').html(response);
//or
$('.classOfDiv').html(response);
Sign up to request clarification or add additional context in comments.

Comments

4

Create a div element in your html page in which you want to show your message and give it a id like

<div id="myMessage"></div>

and replace your console.log line with

$('#myMessage').text(response);

and your message will start coming in div.

Comments

3

Use $(selector).html() to output HTML or $(selector).text() to output as plain text:

$.ajax({
    url: url,
    type:method,
    data: data,
    success: function(response){
        $('div').html(respons);
    }
});

Comments

3

If you want to create your div only in some specific case, then you can create div and then append data in AJAX success call.

In place of console.log(response);

Use :

$('#elementId').append("<div class=''>response</div>");

Comments

2

Replace console.log() with $('#div-id').html(response);

Comments

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.