Javascript 在不更改url页面的情况下发送表单

Javascript 在不更改url页面的情况下发送表单,javascript,php,html,forms,Javascript,Php,Html,Forms,交易如下: 通过创建两个重定向到索引的html页面,我成功地使sendmail.php正常工作。 但是我想要的是(如果($sent)==true)在html表单页面上显示一个警报,提示消息已发送。但当我改变 {echo”window.location='/sent.html';“;} 到 {echo”window.alert='messagesent';“;} 页面重定向到…url…com/sendmail.php,为空 它是live@www.aroundgalaxy.pt/NEW 这是htm

交易如下: 通过创建两个重定向到索引的html页面,我成功地使sendmail.php正常工作。 但是我想要的是(如果($sent)==true)在html表单页面上显示一个警报,提示消息已发送。但当我改变

{echo”window.location='/sent.html';“;}

{echo”window.alert='messagesent';“;}
页面重定向到…url…com/sendmail.php,为空

它是live@www.aroundgalaxy.pt/NEW

这是html的表单

<form action="sendmail.php" method="post">
    <div>
        <div class="row half">
            <div class="6u">
                <input type="text" class="text" name="name" placeholder="Nome" />
            </div>
            <div class="6u">
                <input type="text" class="text" name="email" placeholder="E-mail" />
            </div>
        </div>
        <div class="row half">
            <div class="12u">
                <input type="text" class="text" name="subject" placeholder="Assunto" />
            </div>
        </div>
        <div class="row half">
            <div class="12u">
                <textarea name="message" placeholder="Messagem"></textarea>
            </div>
        </div>
        <div class="row">
            <div class="12u">
                <input type="submit" class="button" value="Enviar" />
            </div>
        </div>
    </div>
</form>

以及sendmail.php

<?php
$to = "info@aroundgalaxy.pt";
$email = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
//$site = $_REQUEST['site'] ;
$subject = "Message from: $name";
$message = $_REQUEST['message'] ;
$headers = "noreply@aroundgalaxy.pt";
$body = "From: $name \n\n Email: $email \n\n Message: $message";
$sent = mail($to, $subject, $body, $headers) ;
if($sent)
{echo "<script language=javascript>window.location = '/sent.html';</script>";}
else
{echo "<script language=javascript>window.location = '/notsent.html';</script>";}
?>
警报()不警报=

{echo "<script language=javascript>window.alert('Message Sent');</script>";}
{echo“window.alert('Message Sent');“;}

如果您想要其他选择,请尝试以下方法:

在sendmail.php中添加以下内容:

sendmail.php:

setcookie("msg","Mail Successfully Sent",time()+5,"/");
header("location:htmlpage.php");
htmlpage.php中

<?php if(isset($_COOKIE['msg'])){?>
<div>
    <?php echo $_COOKIE['msg'];setcookie("msg","",time()-5,"/");?>  
</div>
<?php }?>


将HTML和PHP组合成一个文件,然后将表单发布到itself@meda将php代码放在html文件中?为什么不使用
header('Location:/sent.html')
进行重定向?为什么不使用ajax?@raheelshan有什么好处吗?谢谢,可以使用了。语法错误hehe{echo“alert('mensageenviada');window.location='index.html';“;}
<?php if(isset($_COOKIE['msg'])){?>
<div>
    <?php echo $_COOKIE['msg'];setcookie("msg","",time()-5,"/");?>  
</div>
<?php }?>