【问题标题】:showing text in div without reloading the page在不重新加载页面的情况下在 div 中显示文本
【发布时间】:2014-10-08 12:44:34
【问题描述】:

在以下代码中,我有一个联系表单,并且在该表单中有一个电子邮件验证脚本。作为验证的结果,我希望错误消息显示在名为确认的div 中,而无需重新加载页面。此外,如果电子邮件有效,邮件将被发送,我希望感谢消息显示在相同的div 确认中。问题是如何防止重新加载页面并让错误消息或感谢消息显示在确认 div 中?

<html>
<body>
<?php
function spamcheck($field) {
  // Sanitize e-mail address
  $field=filter_var($field, FILTER_SANITIZE_EMAIL);
  // Validate e-mail address
  if(filter_var($field, FILTER_VALIDATE_EMAIL)) {
    return TRUE;
  } else {
    return FALSE;
  }
}
?>

<?php
if (!isset($_POST["submit"])) {
  ?>
  <form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
  From: <input type="text" name="from"><br>
  Subject: <input type="text" name="subject"><br>
  Message: <textarea rows="10" cols="40" name="message"></textarea><br>
  <input type="submit" name="submit" value="Submit Feedback"><br>
  <div id="confirmation" style="display:none" align="center"></div>
  </form>
  <?php 
} else {  // the user has submitted the form
  // Check if the "from" input field is filled out
  if (isset($_POST["from"])) {
    // Check if "from" email address is valid
    $mailcheck = spamcheck($_POST["from"]);
    if ($mailcheck==FALSE) {
      echo"
      <script>
        document.getElementById('confirmation').text ='invalid email';
      </script>";
    } else {
      $from = $_POST["from"]; // sender
      $subject = $_POST["subject"];
      $message = $_POST["message"];
      // message lines should not exceed 70 characters (PHP rule), so wrap it
      $message = wordwrap($message, 70);
      // send mail
      mail("nawe11@gmail.com",$subject,$message,"From: $from\n");
      echo"
      <script>
        document.getElementById('confirmation').text ='Thank you';
      </script>";
    }
  }
}
?>
</body>
</html>

谢谢

【问题讨论】:

  • 你可以使用 Ajax 做你想做的事
  • 您需要使用 AJAX 提交表单,然后您将获得响应并显示适当的绝对定位 div(错误消息,感谢消息)
  • document.getElementById('confirmation').innerHTML = '&lt;div&gt;test&lt;/div&gt;';

标签: javascript php html email


【解决方案1】:
<input type="text" name="from" id ="from">

调用示例:

var request = $.ajax({
  url: "file.php",
  type: "POST",
  data: { email : $('#from').val() }
});

request.done(function( msg ) {
  //handle HTML
});

request.fail(function( jqXHR, textStatus ) {
  //Handle problem at server side
});

PHP端

<?php

$email = $_POST["email"]

function spamcheck($field) {
  // Sanitize e-mail address
  $field=filter_var($field, FILTER_SANITIZE_EMAIL);
  // Validate e-mail address
  if(filter_var($field, FILTER_VALIDATE_EMAIL)) {
    return 'valid';
  } else {
    return 'no_valid';
  }
}

echo spamcheck($email);

【讨论】:

  • 注意:我没有尝试过代码,这只是一个指导。
【解决方案2】:

仅使用 PHP 是无法做到这一点的。

您正在查看的内容通常称为 AJAX,并使用客户端语言 (Javascript) 它很常见,并且在互联网上广泛使用。您可以通过在 google 上搜索 ajax 找到许多示例和生产就绪的脚本。

更多信息在这里:http://www.w3schools.com/ajax/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    相关资源
    最近更新 更多