How to get success message after submit on same page - not in new page
<p>My submit success message is displaying in a new page.</p>

<p>How can I have it display in the same page as the submit button?</p>

<p>contact.php</p>

<pre><code>&lt;?php 
// configure
$from = 'email'; 
$sendTo = 'email';
$subject = 'new message';
$fields = array('uname' =&gt; 'Jmeno', 'surname' =&gt; 'Spolecnost', 'phone' =&gt; 'Telefon', 'uemail' =&gt; 'Email', 'message' =&gt; 'Obsah zpravy'); // array variable name =&gt; Text to appear in email
$okMessage = 'success';
$errorMessage = 'error';

// let's do the sending  
try
{
    $emailText = "Mate novou zpravu z web formulare: example.cz\n=============================\n";
    foreach ($_POST as $key =&gt; $value) {
        if (isset($fields[$key])) {
            $emailText .= "$fields[$key]: $value\n";
        }
    } 
    mail($sendTo, $subject, $emailText, "From: " . $from);

    $responseArray = array('type' =&gt; 'success', 'message' =&gt; $okMessage);
}
catch (\Exception $e)
{
    $responseArray = array('type' =&gt; 'danger', 'message' =&gt; $errorMessage);
}

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &amp;&amp; strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);

    header('location: contact.php?success=1');

    echo $encoded;
}
else {
    echo $responseArray['message'];
}
?&gt;
</code></pre>

<p>JS for submit button:</p>

<pre><code> $(".open4").click(function() {
        $('#basicform').validator();
            $('#basicform').on('submit', function (e) {
                if (!e.isDefaultPrevented()) {
                    var url = "contact.php";
                    $.ajax({
                        type: "POST",
                        url: url,
                        data: $(this).serialize(),
                        success: function (data)
                        {
                            var messageAlert = 'alert-' + data.type;
                            var messageText = data.message;

                            var alertBox = '&lt;div class="alert ' + messageAlert + ' alert-dismissable"&gt;&lt;button type="button" class="close" data-dismiss="alert" aria-hidden="true"&gt;&amp;times;&lt;/button&gt;' + messageText + '&lt;/div&gt;';
                            if (messageAlert &amp;&amp; messageText) {
                                $('#basicform').find('.messages').html(alertBox);
                                $('#basicform')[0].reset();
                            }
                        }
                    });
                    return false;
                }
            })
</code></pre>