'Popup Window and PHP form
I've created a contact page and a separate PHP page to receive the posted data. I'd like to make the PHP open in a popup window. I've tried methods online without any success, I can make the popup appear but i cant make the PHP send the data.
<!------Contact Page------->
<form method='post' action='sendemail.php' >
<label>Name</label>
<input name="name" placeholder="Type Here">
<label>Email</label>
<input name="email" placeholder="Type Here" id="email">
<label>Message</label>
<textarea name="message" placeholder="Type Here"></textarea>
<label>Human Verification</label>
<input name="human" placeholder="2 + 2 = ? " id="human">
<input id="submit" name="submit" type="submit" value="Submit">
</label>
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $email;
$to = '[email protected]';
$subject = 'New Message';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if ($name != '' && $email != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<h4>Your message has been sent!</h4>';
} else {
echo '<h4>Something went wrong, go back and try again!</h4>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<h4>You answered the anti-spam question incorrectly!</h4>';
}
} else {
echo '<h4>You need to fill in all required fields!!</h4>';
}
}
?>
Solution 1:[1]
I think that if php script is in the same page of your form just in an hidden div you have to target the action with php self like
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"
So the variable POST is correctly set.
If you need to post in popup you can use ajax to send date or jquery to load an external page in your hidden div like:
$('#submit').click(function(){
load(external.php); //where your script is
});
In both cases it should work.
Solution 2:[2]
Since you are submitting the content and doing a POST/GET request to another page the modal won't work the way you are curretly using it. Since you are redirecting to your PHP code. You should use Javascript to display the pop-up.
Maybe adding a onclick
to your submit button?
<input type='submit' onclick='alert("Message sent!")' />
Another option will be to use an Ajax call to you PHP file using the jquery ($.ajax();
) and then use any library of your preference to display the pop-up window that you need. Maybe checking Jquery's or Bootstrap's modal could give you a hint, if you want to be complex approach.
<input type='submit' onclick='sendMessage()' />
function sendMessage(){
$.ajax({
//your stuff
});
$('.message').modal();
}
Solution 3:[3]
Especially for something like a form, I would make sure it doesn't rely on JavaScript in case the user has JavaScript turned off in their browser.
Why not keep it simple and have everything on the same page, with the error message showing above the form, and re-populate the fields with PHP?
Example:
<?php
// Check if coming from a POST
if ($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $email;
$to = '[email protected]';
$subject = 'New Message';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if ($name != '' && $email != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<h4>Your message has been sent!</h4>';
} else {
echo '<h4>Something went wrong, go back and try again!</h4>';
} //endif
} else if ($_POST['submit'] && $human != '4') {
echo '<h4>You answered the anti-spam question incorrectly!</h4>';
}
} else {
echo '<h4>You need to fill in all required fields!!</h4>';
} //endif
} //endif
} //endif
?>
<form method='post' action='<?php echo $_SERVER['PHP_SELF']; ?>' >
<label>Name</label>
<input name="name" placeholder="Type Here" value="<?php if (isset($_POST['name'])) { echo $name;} ?>" >
<label>Email</label>
<input name="email" placeholder="Type Here" id="email" value="<?php if (isset($_POST['email'])) { echo $email;} ?>">
<label>Message</label>
<textarea name="message" placeholder="Type Here"><?php if (isset($_POST['message'])) { echo $message;} ?></textarea>
<label>Human Verification</label>
<input name="human" placeholder="2 + 2 = ? " id="human" value="<?php if (isset($_POST['human'])) { echo $human;} ?>">
<input id="submit" name="submit" type="submit" value="Submit">
</label>
</form>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | SBO |
Solution 2 | David |
Solution 3 | Insomnophobia |