javascript 弹出窗口和 PHP 表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20021667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Popup Window and PHP form
提问by user2999825
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.
我创建了一个联系页面和一个单独的 PHP 页面来接收发布的数据。我想让 PHP 在弹出窗口中打开。我已经尝试过在线方法但没有成功,我可以让弹出窗口出现,但我不能让 PHP 发送数据。
<!------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>';
}
}
?>
回答by SBO
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
我认为,如果 php 脚本在你的表单的同一页面中,只是在一个隐藏的 div 中,你必须用 php self 来定位动作
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"
So the variable POST is correctly set.
所以变量 POST 设置正确。
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:
如果您需要在弹出窗口中发布,您可以使用 ajax 发送日期或 jquery 在隐藏的 div 中加载外部页面,例如:
$('#submit').click(function(){
load(external.php); //where your script is
});
In both cases it should work.
在这两种情况下它都应该工作。
回答by David
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.
由于您正在提交内容并对另一个页面执行 POST/GET 请求,因此模式将无法按照您当前使用的方式工作。由于您正在重定向到您的 PHP 代码。您应该使用 Javascript 来显示弹出窗口。
Maybe adding a onclick
to your submit button?
也许onclick
在您的提交按钮中添加一个?
<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.
另一种选择是使用 jquery ( $.ajax();
)对您的 PHP 文件进行 Ajax 调用,然后使用您喜欢的任何库来显示您需要的弹出窗口。如果您想要复杂的方法,也许检查 Jquery 或 Bootstrap 的模态可以给您一个提示。
<input type='submit' onclick='sendMessage()' />
function sendMessage(){
$.ajax({
//your stuff
});
$('.message').modal();
}
回答by happypete
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.
特别是对于表单之类的东西,我会确保它不依赖于 JavaScript,以防用户在浏览器中关闭了 JavaScript。
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?
为什么不保持简单并将所有内容都放在同一页面上,错误消息显示在表单上方,然后用 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>