dqyitt2954 2013-05-15 18:12
浏览 28
已采纳

从HTML表单执行PHP而无需重定向到PHP

I have a mail form on my HTML that calls a PHP. When I click Submit, It redirects to the PHP and shows various alerts about the form.

I want it not to redirect to the PHP and show alerts on HTML. I tried to redirect back from PHP but in that case it didn't show any alerts.

I couldn't manage to do that. I've read may similar posts but they didn't help.

Can anyone help?

HTML Form:

<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>

Here is the 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.">
";
$headers .= "MIME-Version: 1.0
";
$headers .= "Content-Type: text/html; charset=utf-8
";
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"); 
}
?>

Thanks.

  • 写回答

4条回答 默认 最新

  • dongqie7806 2013-05-15 18:20
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?