【问题标题】:Popup Window and PHP form弹出窗口和 PHP 表单
【发布时间】:2013-11-30 01:28:30
【问题描述】:

我创建了一个联系页面和一个单独的 PHP 页面来接收发布的数据。我想让 PHP 在弹出窗口中打开。我在网上试过方法都没有成功,我可以让弹出窗口出现,但我不能让 PHP 发送数据。

<!------Contact Page------->

<form method='post' action='sendemail.php' >
    <label>Name</label>
    <input name="name" placeholder="Type Here">
    <label>Email</label>
    <input name="email" placeholder="Type Here" id="email">
    <label>Message</label>
    <textarea name="message" placeholder="Type Here"></textarea>


    <label>Human Verification</label>
      <input name="human" placeholder="2 + 2 = ? " id="human">
      <input id="submit" name="submit" type="submit" value="Submit">
    </label>     
</form>

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = $email; 
    $to = 'my@email.com'; 
    $subject = 'New Message';
    $human = $_POST['human'];

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if ($_POST['submit']) {
        if ($name != '' && $email != '') {
            if ($human == '4') {                 
                if (mail ($to, $subject, $body, $from)) { 
                    echo '<h4>Your message has been sent!</h4>';
            } else { 
                echo '<h4>Something went wrong, go back and try again!</h4>'; 
            } 
        } else if ($_POST['submit'] && $human != '4') {
            echo '<h4>You answered the anti-spam question incorrectly!</h4>';
            }
        } else {
            echo '<h4>You need to fill in all required fields!!</h4>';
        }
    }
?>

【问题讨论】:

  • 你有什么错误吗?
  • 第一行是sendeail.php,而不是sendemail.php,并且您在另一个&lt;form&gt; 中有一个&lt;form&gt;,这是无效的。

标签: javascript jquery forms php


【解决方案1】:

我认为,如果 php 脚本位于表单的同一页面中,只是在隐藏的 div 中,则必须使用 php self 来定位操作

<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"

所以变量 POST 设置正确。

如果您需要在弹出窗口中发布,您可以使用 ajax 发送日期或 jquery 在隐藏的 div 中加载外部页面,例如:

$('#submit').click(function(){
   load(external.php);  //where your script is
});

在这两种情况下它都应该工作。

【讨论】:

    【解决方案2】:

    由于您正在提交内容并向另一个页面发出 POST/GET 请求,因此该模式将无法按照您当前使用的方式工作。由于您正在重定向到您的 PHP 代码。您应该使用 Javascript 来显示弹出窗口。

    也许将onclick 添加到您的提交按钮?

    <input type='submit' onclick='alert("Message sent!")' />
    

    另一种选择是使用 jquery ($.ajax();) 对 PHP 文件进行 Ajax 调用,然后使用您喜欢的任何库来显示您需要的弹出窗口。如果您想采用复杂的方法,也许检查 Jquery 或 Bootstrap 的模式可以给您一个提示。

    <input type='submit' onclick='sendMessage()' />
    
    function sendMessage(){
        $.ajax({
            //your stuff
        });
        $('.message').modal();
    }
    

    【讨论】:

      【解决方案3】:

      特别是对于表单之类的东西,我会确保它不依赖 JavaScript,以防用户在浏览器中关闭了 JavaScript。

      为什么不保持简单,将所有内容放在同一页面上,并在表单上方显示错误消息,然后使用 PHP 重新填充字段?

      示例:

      <?php
      
      // Check if coming from a POST
      if ($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['submit'])) {
          $name = $_POST['name'];
          $email = $_POST['email'];
          $message = $_POST['message'];
          $from = $email; 
          $to = 'you@youremail.com'; 
          $subject = 'New Message';
          $human = $_POST['human'];
      
          $body = "From: $name\n E-Mail: $email\n Message:\n $message";
      
          if ($_POST['submit']) {
              if ($name != '' && $email != '') {
                  if ($human == '4') {                 
                      if (mail ($to, $subject, $body, $from)) { 
                          echo '<h4>Your message has been sent!</h4>';
                      } else { 
                          echo '<h4>Something went wrong, go back and try again!</h4>'; 
                      } //endif
                  } else if ($_POST['submit'] && $human != '4') {
                      echo '<h4>You answered the anti-spam question incorrectly!</h4>';
                  }
                  } else {
                      echo '<h4>You need to fill in all required fields!!</h4>';
                  } //endif
              } //endif
      
      } //endif
      
      ?>
      <form method='post' action='<?php echo $_SERVER['PHP_SELF']; ?>' >
      
          <label>Name</label>
          <input name="name" placeholder="Type Here" value="<?php if (isset($_POST['name'])) { echo $name;} ?>" >
          <label>Email</label>
          <input name="email" placeholder="Type Here" id="email" value="<?php if (isset($_POST['email'])) { echo $email;} ?>">
          <label>Message</label>
          <textarea name="message" placeholder="Type Here"><?php if (isset($_POST['message'])) { echo $message;} ?></textarea>
      
      
          <label>Human Verification</label>
              <input name="human" placeholder="2 + 2 = ? " id="human" value="<?php if (isset($_POST['human'])) { echo $human;} ?>">
              <input id="submit" name="submit" type="submit" value="Submit">
          </label>     
      </form>
      

      【讨论】:

      • 值得注意的是,通过 javascript 加载表单本身并没有什么坏处,只要提供了回退(即如果 js 被禁用,则重定向到原始表单页面。)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-09
      • 2012-01-26
      • 2020-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多