div中的php输出

Kev*_*vin 2 javascript php jquery

这是我的脚本:

<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>
Run Code Online (Sandbox Code Playgroud)

我有一个 ajax 函数,它根据您在表单上的输入给我一条错误消息或一条成功消息。

我得到的消息来自这个 php 脚本:

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

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

    $to = "flash1996mph@hotmail.com";
    $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";
?>
Run Code Online (Sandbox Code Playgroud)

现在我用console.log(). 但我想在 a 中显示它们<div>

Alo*_*tel 5

您只需要使用 jQuery .html()在 DIV 中设置响应内容。

替换以下行

console.log(response);
Run Code Online (Sandbox Code Playgroud)

$('#idOfDiv').html(response);
//or
$('.classOfDiv').html(response);
Run Code Online (Sandbox Code Playgroud)