javascript - 如何在同一页面上以html形式操作php函数?

标签 javascript php jquery html forms

我现在有一个联系表单,它操作一个单独的 php 页面。然而,确认信息只是一张白页,上面有一些纯文本。如何将此 php 代码放入我的联系表单页面并从那里执行操作,以便我可以在同一页面上进行确认?

PHP代码如下:

    <?php
if(isset($_POST['email'])) {

    // CHANGE THE TWO LINES BELOW
    $email_to = "dkxclanx@gmail.com";

    $email_subject = "Movie Night Review Submissions";


    function died($error) {
        // your error code can go here
        echo "We're sorry, but there's errors found with the form you submitted.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }

    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // not required
    $comments = $_POST['comments']; // required

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

<!-- place your own success html below -->

Thank you for contacting us. We will be in touch with you very soon.

You will now be redirected back to the original page within 10 seconds.

If the page does not load click <a href ="http://movienightreviews.tk/contact_us.html">here</a> to be redirected now.

<?php
header('Refresh: 10; URL=http://movienightreviews.tk/contact_us.html');
}
die();
?>

当前html页面代码为:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Contact Us</title>
<link href="Styles/main.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="wrapper">
    <header id="top">
        <h1>Movie Night Reviews</h1>
        <nav id="mainnav">
            <ul>
                <li><a href="index.html">Home</a></li>
                <li><a href="trailers.php">Trailers</a></li>
                <li class = "review"><a href="#">Reviews</a>
                    <ul id = "dropdown1">
                        <li><a href="civil_war.php">Civil War</a></li>
                        <li><a href="furious_7.html">Furious 7</a></li>
                        <li><a href="fantastic_four.html">Fantastic Four</a></li>
                        <li><a href="jurassic_world.html">Jurassic World</a></li>
                        <li><a href="agents_of_shield.html">Agents of Shield</a></li>
                    </ul>
                </li>
                <li><a href="about_us.html">About Us</a></li>
                <li><a href="contact_us.html"id="thispage">Contact Us</a></li>
            </ul>
        </nav>
    </header>
    <article id="main">
        <h2>Contact Us by E-Mail</h2>
        <p>To contact us by E-Mail please fill out the following form and click Submit.</p>
       <form name="htmlform" method="post" action="html_form_send.php">
<table width="450px">
</tr>
<tr>
 <td valign="top">
  <label for="first_name">First Name *</label>
 </td>
 <td valign="top">
  <input  type="text" name="first_name" maxlength="50" size="30">
 </td>
</tr>

<tr>
 <td valign="top">
  <label for="last_name">Last Name *</label>
 </td>
 <td valign="top">
  <input  type="text" name="last_name" maxlength="50" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="email">Email Address *</label>
 </td>
 <td valign="top">
  <input  type="text" name="email" maxlength="80" size="30">
 </td>

</tr>
<tr>
 <td valign="top">
  <label for="telephone">Telephone Number</label>
 </td>
 <td valign="top">
  <input  type="text" name="telephone" maxlength="30" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="comments">Comments *</label>
 </td>
 <td valign="top">
  <textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea>
 </td>

</tr>
<tr>
 <td colspan="2" style="text-align:center">
  <input type="submit" value="Submit">
  </td>
</tr>
</table>
</form>
    <h2>Contact Us by Post</h2>
    <p>To contact us by Post please send a letter to the following address:</p>
    <p>
    Joe Bloggs<br>
    The Shed<br>
    Moss Side<br>
    Manchester<br> 
    M15 SHED</p>
    </article>
    <aside id="sidebar">
        <h2>Disclaimer</h2>
        <p>For those who haven't realised, this website is just a test website and there is no such company or people tht rementioned inthis website so do not contact us. </p>
    </aside>
    <footer>
        <p>&copy; Copyright 2015 Movie Night Reviews</p>
    </footer>
</div>
</body>
</html>

谢谢各位的解答!!我按照你所说的删除了该操作并将 php 放入我的页面中。该功能现在正在工作,但是由于某种原因,我的 apge 的一半不会显示,但不确定为什么。我的新代码现在是:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Contact Us</title>
<link href="Styles/main.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="wrapper">
    <header id="top">
        <h1>Movie Night Reviews</h1>
        <nav id="mainnav">
            <ul>
                <li><a href="index.html">Home</a></li>
                <li><a href="trailers.php">Trailers</a></li>
                <li class = "review"><a href="#">Reviews</a>
                    <ul id = "dropdown1">
                        <li><a href="civil_war.php">Civil War</a></li>
                        <li><a href="furious_7.html">Furious 7</a></li>
                        <li><a href="fantastic_four.html">Fantastic Four</a></li>
                        <li><a href="jurassic_world.html">Jurassic World</a></li>
                        <li><a href="agents_of_shield.html">Agents of Shield</a></li>
                    </ul>
                </li>
                <li><a href="about_us.html">About Us</a></li>
                <li><a href="contact_us.html"id="thispage">Contact Us</a></li>
            </ul>
        </nav>
    </header>
    <article id="main">
        <h2>Contact Us by E-Mail</h2>
        <p>To contact us by E-Mail please fill out the following form and click Submit.</p>
       <form name="htmlform" method="post" action="#">
<table width="450px">
</tr>
<tr>
 <td valign="top">
  <label for="first_name">First Name *</label>
 </td>
 <td valign="top">
  <input  type="text" name="first_name" maxlength="50" size="30">
 </td>
</tr>

<tr>
 <td valign="top">
  <label for="last_name">Last Name *</label>
 </td>
 <td valign="top">
  <input  type="text" name="last_name" maxlength="50" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="email">Email Address *</label>
 </td>
 <td valign="top">
  <input  type="text" name="email" maxlength="80" size="30">
 </td>

</tr>
<tr>
 <td valign="top">
  <label for="telephone">Telephone Number</label>
 </td>
 <td valign="top">
  <input  type="text" name="telephone" maxlength="30" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="comments">Comments *</label>
 </td>
 <td valign="top">
  <textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea>
 </td>

</tr>
<tr>
 <td colspan="2" style="text-align:center">
  <input type="submit" value="Submit">
  </td>
</tr>
</table>
</form>
<h2>Contact Us by Post</h2>
    <p>To contact us by Post please send a letter to the following address:</p>
    <p>
    Joe Bloggs<br>
    The Shed<br>
    Moss Side<br>
    Manchester<br> 
    M15 SHED</p>
    </article>
    <aside id="sidebar">
        <h2>Disclaimer</h2>
        <p>For those who haven't realised, this website is just a test website and there is no such company or people tht rementioned inthis website so do not contact us. </p>
    </aside>
    <footer>
        <p>&copy; Copyright 2015 Movie Night Reviews</p>
    </footer>
    </div>
</body>
<?php
if(isset($_POST['email'])) {

    // CHANGE THE TWO LINES BELOW
    $email_to = "dkxclanx@gmail.com";

    $email_subject = "Movie Night Review Submissions";


    function died($error) {
        // your error code can go here
        echo "We're sorry, but there's errors found with the form you submitted.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }

    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // not required
    $comments = $_POST['comments']; // required

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

<!-- place your own success html below -->

Thank you for contacting us. We will be in touch with you very soon.
</html>

最佳答案

您的 HTML 内容必须有条件地打印,因此只有在用户第一次访问时才显示整个页面:

<?php
if(isset($_POST['email'])) {
    // prepare to send email...
    @mail($email_to, $email_subject, $email_message, $headers);
?> 

<!-- success message goes here -->

<?
} else {
?>

<!-- full html goes here -->

<?php
}
?>

顺便说一句,您可以使用$_SERVER["SCRIPT_NAME"],这样您就不必担心在重命名 PHP 文件时更改代码:

<form name="htmlform" method="post" action="<?php echo $_SERVER["SCRIPT_NAME"]; ?>">

关于javascript - 如何在同一页面上以html形式操作php函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31711377/

相关文章:

php - 在 sentora 网站面板 w.r.t Cpanel [控制面板] 中设置 crons

php - 使用PHP向MYSQL数据库提交数据

javascript - 使用 JQuery 查找和复制图像

javascript - Jquery 不在 for 循环中工作,但在单击时工作?

javascript - 是否可以将所有浏览器控制台消息/错误通过管道传输到远程计算机?

javascript - 未捕获的类型错误 : $ is not a function

php - 如何使用python绕过WP super 缓存?

c# - 在 asp.net mvc 4 中使用带有部分 View 的 jQuery

javascript - Ajax |使用警报();其他文件中的函数不起作用

javascript - 如何在 React js 中增加 .map() 中的变量