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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料