Blake
Blake

Reputation: 91

php with form alert box

I would like this php form to pop up an alert box on completion or if there is error. What do I need to add or change to this to make that happen, or is that added javascript?

<?php
    $name = $_POST['fullname'];
    $email = $_POST['email'];
    $message = $_POST['comment'];
    $from = 'From: Contact Form'; 
    $to = '[email protected]'; 
    $subject = 'Hello';


    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if (mail ($to, $subject, $body, $from)) { 
        echo '<p>Your message has been sent!</p>';
    } else { 
        echo '<p>Something went wrong, go back and try again!</p>'; 
    }

?>

Upvotes: 1

Views: 6481

Answers (2)

Brian Duncan
Brian Duncan

Reputation: 1186

In the page that would display the error, you would add javascript to pop up the message using the alert() function. You could use php to conditionally output this javascript. Oy vey.

Example:

<script language='javascript'>
    <?php if ($error) { ?>
    window.onload = function() {
        alert('<?php echo $error?>');
    }        
    <?php } ?>
</script>

Something like that.

Upvotes: 2

WhoaItsAFactorial
WhoaItsAFactorial

Reputation: 3558

To do this you will need to add some more technologies, specifically Javascript and AJAX.

PHP is a sever-side language, which means that by the time the browser is rendering the page PHP has completed running your code.

Upvotes: 4

Related Questions