我创建了一个联系页面和一个单独的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 = 'my@email.com';
$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>';
}
}
?>
答案 0 :(得分:0)
我认为如果php脚本在你的表单的同一页面中只是在一个隐藏的div中你必须用php self来定位动作
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"
因此,变量POST已正确设置。
如果您需要在弹出窗口中发帖,您可以使用ajax发送日期或jquery来加载隐藏div中的外部页面,如:
$('#submit').click(function(){
load(external.php); //where your script is
});
在这两种情况下都应该有效。
答案 1 :(得分:0)
由于您要提交内容并对另一个页面执行POST / GET请求,因此模式将无法按照您当前使用它的方式运行。因为您重定向到PHP代码。您应该使用Javascript来显示弹出窗口。
可能会在提交按钮中添加onclick
吗?
<input type='submit' onclick='alert("Message sent!")' />
另一种选择是使用jquery($.ajax();
)使用Ajax调用PHP文件,然后使用您喜欢的任何库来显示所需的弹出窗口。如果你想要复杂的方法,也许检查Jquery或Bootstrap的模态可以给你一个提示。
<input type='submit' onclick='sendMessage()' />
function sendMessage(){
$.ajax({
//your stuff
});
$('.message').modal();
}
答案 2 :(得分:0)
特别是对于像表单这样的东西,我会确保它不依赖于JavaScript,以防用户在浏览器中关闭JavaScript。
为什么不保持简单并将所有内容放在同一页面上,并在表单上方显示错误消息,并使用PHP重新填充字段?
示例:强>
<?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 = 'you@youremail.com';
$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>