![](/img/trans.png)
[英]How to show success message if there is no empty field after form submission in php
[英]How to show the success message of PHP in html page on Form submission
我正在用html開發網站。 在Careers.html頁面中,我具有“應用”按鈕。 在單擊該應用按鈕時,它將使用php文件發送郵件。 在suceess事件中,我需要設置警報或在同一Careers.html頁面上顯示div。
Careers.html
<form method="post" action="Applyresume.php" enctype="multipart/form-data">
<tr>
<td>First Name </td>
<td><input type="TextBox" name="First_Name" class="applytext" required></td>
</tr>
<tr>
<td>Last Name </td>
<td><input type="TextBox" name="Last_Name" class="applytext" required></td>
</tr>
<tr>
<td>E-mail </td>
<td><input type="TextBox" name="email" class="applytext" required></td>
</tr>
<tr>
<td>Phone </td>
<td><input type="TextBox" name="Phone_No" class="applytext" required></td>
</tr>
<tr>
<td>Attachment </td>
<td><input type="file" name="attachment" maxlength="50" allow="text/*" class="applytext" required></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="button" class="send-resume" value="SEND" style="margin-left:24%;">
<input type="reset" value="RESET" style="margin-left:8%"></td>
</tr>
</form>
Applyresume.php
<?php
if($_POST && isset($_FILES['attachment']))
{
$name= $_POST['First_Name'];
$lname= $_POST['Last_Name'];
$email= $_POST['email'];
$phonenum = $_POST['Phone_No'];
$from_email = $_POST['email']; //sender email
$recipient_email = 'nisha@acute.company'; //recipient email
$subject = 'Test Email '; //subject of email
$message = 'Resume attached below.'; //message body
$emailbod = "$name
$lname
$email
$phonenum";
//get file details we need
$file_tmp_name = $_FILES['attachment']['tmp_name'];
$file_name = $_FILES['attachment']['name'];
$file_size = $_FILES['attachment']['size'];
$file_type = $_FILES['attachment']['type'];
$file_error = $_FILES['attachment']['error'];
$user_email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
if($file_error>0)
{
die('upload error');
}
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
$boundary = md5("sanwebe");
//header
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:".$from_email."\r\n";
$headers .= "Reply-To: ".$user_email."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
//plain text
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($emailbod));
//attachment
$body .= "--$boundary\r\n";
$body .="Content-Type: $file_type; name=\"$file_name\"\r\n";
$body .="Content-Disposition: attachment; filename=\"$file_name\"\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
$body .= $encoded_content;
$sentMail = @mail($recipient_email, $subject, $body, $headers);
if($sentMail) //output success or failure messages
{
echo '<script type="text/javascript">alert("Thanks for your interest. Your Resume has been sent to HR@prominData.com");window.location.assign("Careers.html");</script>';
}else
{
echo"<script>alert('Could not send mail! Please check your PHP mail configuration.')</script>";
}
}
?>
我的問題是我需要在同一HTML頁面中設置成功警報。 但是,這里我的成功警報正在PHP頁面中打開。
如何在同一HTML頁面中添加此php代碼?
要在同一頁面上查看消息或警報,您只需將php
代碼復制到同一HTML頁面中即可。 如下做
your_html.php
<form method="post" action="" enctype="multipart/form-data">
<tr>
<td>First Name </td>
<td><input type="TextBox" name="First_Name" class="applytext" required></td>
</tr>
<tr>
<td>Last Name </td>
<td><input type="TextBox" name="Last_Name" class="applytext" required></td>
</tr>
<tr>
<td>E-mail </td>
<td><input type="TextBox" name="email" class="applytext" required></td>
</tr>
<tr>
<td>Phone </td>
<td><input type="TextBox" name="Phone_No" class="applytext" required></td>
</tr>
<tr>
<td>Attachment </td>
<td><input type="file" name="attachment" maxlength="50" allow="text/*" class="applytext" required></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="button" class="send-resume" value="SEND" style="margin-left:24%;">
<input type="reset" value="RESET" style="margin-left:8%"></td>
</tr>
</form>
<?php
if($_POST && isset($_FILES['attachment']))
{
$name= $_POST['First_Name'];
$lname= $_POST['Last_Name'];
$email= $_POST['email'];
$phonenum = $_POST['Phone_No'];
$from_email = $_POST['email']; //sender email
$recipient_email = 'nisha@acute.company'; //recipient email
$subject = 'Test Email '; //subject of email
$message = 'Resume attached below.'; //message body
$emailbod = "$name
$lname
$email
$phonenum";
//get file details we need
$file_tmp_name = $_FILES['attachment']['tmp_name'];
$file_name = $_FILES['attachment']['name'];
$file_size = $_FILES['attachment']['size'];
$file_type = $_FILES['attachment']['type'];
$file_error = $_FILES['attachment']['error'];
$user_email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
if($file_error>0)
{
die('upload error');
}
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
$boundary = md5("sanwebe");
//header
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:".$from_email."\r\n";
$headers .= "Reply-To: ".$user_email."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
//plain text
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($emailbod));
//attachment
$body .= "--$boundary\r\n";
$body .="Content-Type: $file_type; name=\"$file_name\"\r\n";
$body .="Content-Disposition: attachment; filename=\"$file_name\"\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
$body .= $encoded_content;
$sentMail = @mail($recipient_email, $subject, $body, $headers);
if($sentMail) //output success or failure messages
{
echo '<script type="text/javascript">alert("Thanks for your interest. Your Resume has been sent to HR@prominData.com");window.location.assign("Careers.html");</script>';
}else
{
echo"<script>alert('Could not send mail! Please check your PHP mail configuration.')</script>";
}
}
?>
不要忘記刪除<form>
的action
屬性。 HTML頁面的擴展名也應更改為php
。
設置成功條件創建會話$ _SESSION ['action] =“您的按摩”;
以及頁面刷新時。
會有一個您想回盪按摩的地方。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.