D-Boy
D-Boy

Reputation: 17

Form submit dialog

I have a basic form that asks for certain information and then validates the form using javascript before actually submitting it to a seperate php file to email the form submission to me however, after successfully submitting the form, it goes to blank page and then a Thank you popup shows up. How do I set it so hitting the submit button doesn't go to a new page but just displays the popup on the current page?

My code for filling out the form is:

<form action="send_group.php" method="post" onsubmit='return formValidator()'>
//Asks to input information
    <input type="submit" value="Submit" />
</form>

My PHP code is:

<?php
$webmaster_email = "[email protected]"; //E-mail the message will be sent to

$info = $_REQUEST['info'] ;
$info1= $_REQUEST['info1'] ;
$info2= $_REQUEST['info2'] ;
$info3= $_REQUEST['info4'] ;

mail( "$webmaster_email", "Information Form Submission",
    "Info: $info
Info1: $info1
Info2: $info2
Info3: $info3" );

echo "<script type='text/javascript'>\n";
echo "alert('Thank you for your booking request. We will get back to you as soon as possible.');\n";
echo "</script>";
?>

Upvotes: 1

Views: 186

Answers (5)

chessweb
chessweb

Reputation: 4655

Well, the browser shows exactly what it receives from your php-script.

One way to solve your problem without Ajax could be the following approach: To display your original page after the request has been processed add e.g.

header("location: your_form_page.php?req=1");

in case of success, and e.g.

header("location: your_form_page.php?req=-1");

in case an error occurred to send_group.php. The GET parameter req can be used to display either the thank-you-box or an error message. The switching logic must of course be implemented already in your_form_page.php, e.g.

if ($_GET['req'] == '1') {
    echo "<script type='text/javascript'>\n";
    echo "alert('Thank you for ...');\n";
    echo "</script>";
} else if ($_GET['req'] == '-1') {
    echo "<script type='text/javascript'>\n";
    echo "alert('Error ...');\n";
    echo "</script>";
}

Upvotes: 1

Rainer.R
Rainer.R

Reputation: 458

You can either use AJAX to submit the data in the background, or, the easier option put the from and php into the same php file.

Upvotes: 2

JBENOIT
JBENOIT

Reputation: 421

You have to use an AJAX request. So you bind an onclick event on your submit button, then you send an AJAX request, and when the AJAX request suceeded you display your response (Bad or not). JQuery is really powerful and easy for AJAX request, look here : http://api.jquery.com/jQuery.ajax/

Upvotes: 2

Setthase
Setthase

Reputation: 14418

Use xhrRequest (ajax) for retrieve information from validation page and then show result to your user.

Upvotes: 0

Matt
Matt

Reputation: 7040

You have to redirect the user after the script successfully finishes, using the location header

header("location: formPage.php?success=1");

Then on your original form page you can run the javascript.

Upvotes: 0

Related Questions