简体   繁体   English

弹出窗口和 PHP 表单

[英]Popup Window and PHP form

I've created a contact page and a separate PHP page to receive the posted data.我创建了一个联系页面和一个单独的 PHP 页面来接收发布的数据。 I'd like to make the PHP open in a popup window.我想让 PHP 在弹出窗口中打开。 I've tried methods online without any success, I can make the popup appear but i cant make the PHP send the data.我已经尝试过在线方法但没有任何成功,我可以让弹出窗口出现,但我不能让 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>';
        }
    }
?>

I think that if php script is in the same page of your form just in an hidden div you have to target the action with php self like我认为,如果 php 脚本在您的表单的同一页面中,只是在一个隐藏的 div 中,您必须使用 php self 来定位操作

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

So the variable POST is correctly set.所以变量 POST 设置正确。

If you need to post in popup you can use ajax to send date or jquery to load an external page in your hidden div like:如果您需要在弹出窗口中发布,您可以使用 ajax 发送日期或 jquery 在隐藏的 div 中加载外部页面,例如:

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

In both cases it should work.在这两种情况下它都应该工作。

Since you are submitting the content and doing a POST/GET request to another page the modal won't work the way you are curretly using it.由于您正在提交内容并对另一个页面执行 POST/GET 请求,因此模式将无法按照您当前使用的方式工作。 Since you are redirecting to your PHP code.由于您正在重定向到您的 PHP 代码。 You should use Javascript to display the pop-up.您应该使用 Javascript 来显示弹出窗口。

Maybe adding a onclick to your submit button?也许在你的提交按钮上添加一个onclick

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

Another option will be to use an Ajax call to you PHP file using the jquery ( $.ajax(); ) and then use any library of your preference to display the pop-up window that you need.另一种选择是使用 jquery ( $.ajax(); ) 对 PHP 文件使用 Ajax 调用,然后使用您喜欢的任何库来显示您需要的弹出窗口。 Maybe checking Jquery's or Bootstrap's modal could give you a hint, if you want to be complex approach.如果您想要复杂的方法,也许检查 Jquery 或 Bootstrap 的模式可以给您一个提示。

<input type='submit' onclick='sendMessage()' />

function sendMessage(){
    $.ajax({
        //your stuff
    });
    $('.message').modal();
}

Especially for something like a form, I would make sure it doesn't rely on JavaScript in case the user has JavaScript turned off in their browser.特别是对于表单之类的东西,我会确保它不依赖于 JavaScript,以防用户在浏览器中关闭了 JavaScript。

Why not keep it simple and have everything on the same page, with the error message showing above the form, and re-populate the fields with PHP?为什么不保持简单并将所有内容都放在同一页面上,并在表单上方显示错误消息,然后用 PHP 重新填充字段?

Example:示例:

<?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM