我有一个包含 2 个输入字段的表单,提交时 ajax 表单应该刷新页面并淡入下载按钮,但它会转到 php 文件“download.php”并在左上角显示数字 1 .
由于我收到了来自输入的信息,我猜测 1 表示成功,0 表示失败。我如何阻止它这样做并保持在同一页面/刷新。
TL;DR - 我有一个表单可以正常提交并将信息发送到电子邮件 ID,但它不会刷新页面,它会转到 php 文件并显示数字 1 表示成功。
请帮忙。
后面是我的 ajax php 'download.php' 代码(它确实有 php 开始标记,由于某种原因没有显示在此处)
<?php
$name =$_POST['name'];
$email =$_POST['email'];
// Example
$HTML = '<table cellspacing="0" cellpadding="8" border="0" width="600" style="background:#EEE">
<tr>
<td>File Download</td>
<td></td>
</tr>
<tr>
<td>Name:</td>
<td>'.$name.'</td>
</tr>
<tr>
<td>email:</td>
<td>'.$email.'</td>
</tr>
</table>';
$from = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a1c4ccc0c8cdc8c5e1c6ccc0c8cd8fc2cecc" rel="noreferrer noopener nofollow">[email protected]</a>';
$to = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c4a1a9a5ada8ada084a3a9a5ada8eaa7aba9" rel="noreferrer noopener nofollow">[email protected]</a>";
$subject = "Solutions Enquiry by Mr/ Mrs : ".$name;
sendHTMLemail($HTML,$from,$to,$subject);
function sendHTMLemail($HTML,$from,$to,$subject)
{
// First we have to build our email headers
// Set out "from" address
$headers = "From: $from\r\n";
// Now we specify our MIME version
$headers .= "MIME-Version: 1.0\r\n";
// Create a boundary so we know where to look for
// the start of the data
$boundary = uniqid("HTMLEMAIL");
// First we be nice and send a non-html version of our email
$headers .= "Content-Type: multipart/alternative;".
"boundary = $boundary\r\n\r\n";
$headers .= "This is a MIME encoded message.\r\n\r\n";
$headers .= "--$boundary\r\n".
"Content-Type: text/plain; charset=ISO-8859-1\r\n".
"Content-Transfer-Encoding: base64\r\n\r\n";
$headers .= "Message-ID: <".$now." TheSystem@".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";
$headers .= chunk_split(base64_encode(strip_tags($HTML)));
// Now we attach the HTML version
$headers .= "--$boundary\r\n".
"Content-Type: text/html; charset=ISO-8859-1\r\n".
"Content-Transfer-Encoding: base64\r\n\r\n";
$headers .= chunk_split(base64_encode($HTML));
// And then send the email ....
$sent=mail($to,$subject,"",$headers);
if($sent)
echo '1';
else
echo '0';
}
?>
最佳答案
在 JSFiddle 示例中,您会忘记在沙箱中添加 jQuery 库。这就是为什么你的代码不起作用。在纯 JS 中,没有这样的选择器:$('#...')。
第二个问题是 - 你添加了
此后我的页面上没有任何刷新。
更新: 我建议您使用 JSON 将数据从服务器转换到客户端。添加:
dataType: 'json'
到您的 $.ajax 调用和服务器端:
echo json_encode(array('status'=>1));
它可以帮助您从服务器向客户端脚本提供更多详细信息和数据。
关于javascript - Ajax 表单不刷新 - 成功后显示数字 ' 1 ',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22280196/