BritishSam
BritishSam

Reputation: 1080

staying on same page after form post

and when you click send a pop up window comes up saying if it has sent successfully or not. thats all i want to happen but at the moment the pop up window comes up and in the window from where the user presses send it goes to the php page the action calls, how do i stop that window going to the php page and just have a pop up window come up?

form code:

<form method="post" class="contact-form" action="php/callback.php" >
                <input name="name" type="text" id="formbox" value="Name" onfocus="(this.value == 'Name') && (this.value = '')" onblur="(this.value == '') && (this.value = 'Name')"/><br />
                <input name="number" type="text" id="formbox" value="Number" onfocus="(this.value == 'Number') && (this.value = '')" onblur="(this.value == '') && (this.value = 'Number')"/><br />
                <input type="submit" value="Send" id="formbutton">
    </form>

php code:

<?php

$name = $_POST['name'];
$number = $_POST['number'];

$to = "[email protected]";
$subject = "Call Back Enquiry";
$message = "Hi Raymond, someone that came to your website wants you to call them back, their name is '$name' and their number is '$number'";
$from = "Your Website";
$headers = "From:" . $from;
$send_contact = mail($to,$subject,$message,$headers);

if($send_contact){
echo "<script>alert('We will contact you shortly');</script>";
}
else {
echo "<script>alert('Something went wrong, try again!');</script>";
}
?>

Upvotes: 0

Views: 1711

Answers (2)

Voitcus
Voitcus

Reputation: 4446

I would do this by normal button ie. <input type="button" /> not submit, without form. At onclick I open a new window (what about popup-blocker?), that has form inside, and in the same event procedure I send (still by Javascript) values of my fields eg. win.getElementById[someControlName].value=document.getElementById(sourceControlName).value. win is a variable of new window, while document is the window that is the opener. Then, if I am ready, still in the same event, I call win.forms[0].submit().

I have not tested it, maybe some errors, but the main idea is clear, I hope.

Upvotes: 0

silkfire
silkfire

Reputation: 25935

Use jQuery with AJAX to create seamless forms that don't involve POSTing to another page.

More user-friendly popups than alert() can be created with UI frameworks like jQuery UI or Bootstrap.

Upvotes: 1

Related Questions