0

我的 HTML 上有一个调用 PHP 的邮件表单。当我单击提交时,它会重定向到 PHP 并显示有关表单的各种警报。

我不希望它重定向到 PHP 并在 HTML 上显示警报。我试图从 PHP 重定向回来,但在这种情况下它没有显示任何警报。

我无法做到这一点。我读过可能类似的帖子,但它们没有帮助。

任何人都可以帮忙吗?

HTML 表单:

<form method="post" action="sendmail.php">
<input name="id" type="hidden" value="1775">
<table cellpadding="10px" border="0">
<tr>
   <td align="right"><iletisimmetni>ad soyad :</iletisimmetni></td>
   <td><input class="yuvarlak" name="sender_name" type="text"></td>
</tr>
<tr>
   <td align="right"><iletisimmetni>e-posta :</iletisimmetni></td>
   <td><input class="yuvarlak" name="sender_email" type="email"></td>
</tr>
<tr>
   <td align="right"><iletisimmetni>konu :</iletisimmetni></td>
   <td><input class="yuvarlak" name="subject" type="text"></td>
</tr>
<tr>
   <td align="right" valign="top"><iletisimmetni>mesaj :</iletisimmetni></td>
   <td><textarea class="yuvarlak" rows="10" cols="40" name="message" 
   onkeyup="textLimit(this, 250);"></textarea></td>
</tr>
<tr>
   <td colspan="2" align="right"><input class="urunbutton" value="Gönder" name="send"  
   type="submit" id="send"></td>
</tr>
</table>
</form>

这是PHP:

<?php
function spamcheck($field)
{
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
if (isset($_REQUEST['sender_email']) && !empty($_REQUEST['sender_name']) &&    
!empty($_REQUEST['subject']) && !empty($_REQUEST['message']))
{

$mailcheck = spamcheck($_REQUEST['sender_email']);
if ($mailcheck==FALSE)
{
echo "<script>alert('Lütfen geçerli bir e-posta adresi girin.')</script>";
//  $URL="http://pantuff.com/testing/iletisim.html"; 
//  header ("Location: $URL"); 
}
else
{//send email
$sender_name = $_REQUEST['sender_name'] ;
$sender_email = $_REQUEST['sender_email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
$headers = "From: ".$sender_name." <".$sender_email.">\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=utf-8\r\n";
mail("info@pantuff.com", "$subject", "$message" . " " . "$sender_name", $headers);
echo "<script>alert('Mesajınız alınmıştır. Teşekkür ederiz.')</script>";
//  $URL="http://pantuff.com/testing/iletisim.html"; 
//  header ("Location: $URL"); 
}
}
else
{
echo "<script>alert('Lütfen tüm alanları doldurun.')</script>";
//  $URL="http://pantuff.com/testing/iletisim.html"; 
//  header ("Location: $URL"); 
}
?>

谢谢。

4

4 回答 4

1

看起来您正在尝试在发送表单之前对其进行验证。要在实际提交表单之前在客户端执行此操作,您可以使用 javascript。jQuery 有一个插件可以节省你一些时间:

http://bassistance.de/jquery-plugins/jquery-plugin-validation/

于 2013-05-15T18:15:38.713 回答
1

将您的代码更改为

<form method="post" action="">

然后在同一个html页面中编写php代码。

为了验证表单,请在同一页面上使用 Javascript 代码。

于 2013-05-15T18:16:24.277 回答
0

您可以将其合并为一个文件示例 index.php

<?php 
    //php code in here
?>
<form action="index.php">
    <input type=text />
    <!-- html code in here -->
</form>

当我们访问 php 文件时,php 将被服务器执行。HTML 和 javascript 将被浏览器读取。当您重定向时,浏览器永远不会读取脚本,因为服务器会为浏览器提供另一个页面。

于 2013-05-15T18:22:40.023 回答
0

I think what you're trying to do here (and what is common practice) is to embed your HTML code into the PHP file, instead of having 2 separate files.

It is perfectly valid to have HTML code outside of <?php ?> tags.

For example you would have a single file sendmail.php look something like this:

<?php

if(isset($_POST['id']))
{
    // checks and alerts
    // ...
}

?>

<form method="post" action="sendmail.php">
<input name="id" type="hidden" value="1775">
<table cellpadding="10px" border="0">
<!-- ... -->
</form>

And your client would access the from via the URL to the sendmail.php.

于 2013-05-15T18:20:37.637 回答