我有一个PHP表单,每次打开/刷新页面时都会提交。我只希望在按下提交按钮时提交。我是PHP的新手,所以非常感谢任何帮助。
另外,如果可能的话,我希望在按下提交按钮时不刷新页面。我想在按钮下面显示一条消息,表示类似的内容,感谢您提交。我搜索了这个主题,但由于它们太模糊,无法理解其他答案。
这是form.php:
<?php
$field_name = $_POST['cf_name'];
$field_phone = $_POST['cf_phone'];
$field_email = $_POST['cf_email'];
$field_postcode = $_POST['cf_postcode'];
$field_message = $_POST['cf_message'];
$mail_to = 'info@domain.com';
$subject = 'Call back form submission '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'Phone: '.$field_phone."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Postcode: '.$field_postcode."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
print('Thanks, you will receive a call back within the next hour.');
window.location = 'http://www.domain.com';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to info@domain.com to recieve a call back');
window.location = 'http://www.domain.com';
</script>
<?php
}
?>
以下是HTML:
<form class="form-horizontal" action="bat/form.php" method='post'>
<div id="container">
<div id="form1"><div class="form-group">
<div class="col-sm-10 input-group-lg">
<input type="text" class="form-control" placeholder="E-mail" name='cf_email'>
</input>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 input-group-lg">
<input type="text" class="form-control" placeholder="Postcode" name='cf_postcode'>
</input>
</div>
</div>
</div>
<div id="form2"><div class="form-group">
<div class="col-sm-10 input-group-lg">
<input type="text" class="form-control" placeholder="Name" name='cf_name'>
</input>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 input-group-lg">
<input type="text" class="form-control" placeholder="Phone number" name='cf_phone'>
</input>
</div>
</div></div>
</div>
<div class="form-group">
<div class="col-sm-10 input-group-lg">
<input type="text" class="form-control" placeholder="Message" name='cf_message'>
</input>
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<input type="submit" class="btn btn-primary btn-med" value='Submit »'>
</input>
</div>
</div>
</form>
答案 0 :(得分:0)
您应该使用java脚本重定向页面,并且在刷新页面时阻止您提交表单
echo "<script>window.open('samepage.php' , '_self')</script>";
在解析表单请求结束时使用此代码 我认为你的问题得到了解决
答案 1 :(得分:0)
chmod
&#13;
试试这个
将文件另存为.php