简体   繁体   English

在提交之前填充PHP`POST`

[英]PHP `POST` Populated Before Submit

I apologize if this is a duplicate, I was not able to find a previous question. 抱歉,如果这是重复的,我找不到上一个问题。

I have a form that accepts some user input and then upon submitting the input, a mailto function is supposed to happen. 我有一个接受某些用户输入的表单,然后在提交输入后,应该发生mailto函数。

My issue is that even when refreshing the cache for the browser, my $_POST values are already set from a previous submit. 我的问题是,即使刷新浏览器的缓存时,我的$_POST值也已经从先前的提交中设置了。 Ideally I would want the post to reset every time. 理想情况下,我希望该帖子每次重设。

PHP 的PHP

if(isset($_POST)){sendIt();}



        function sendIt(){
            global $disabled,$b;            
            if (!empty($_POST)){
                $name = $_POST['name'];$name = filter_var($name, FILTER_SANITIZE_SPECIAL_CHARS);
                $branch = $_POST['branch'];
                $numb = $_POST['numb'];
                $subject = $_POST['subject'];
                if (isset($_POST['email'])){
                    $email = $_POST['email'];$email = filter_var($email, FILTER_SANITIZE_EMAIL);
                }else{
                    $email = '';
                }
                if ($disabled!=='disabled'){
                    $b = $_POST['b'];
                }
                echo ('<script>window.location.href = "mailto:bobby@bboi.com?subject=Support Requested!&body=Contact: "+"'.$name.'"+"Bank: "+"'.$b.'"+" - "+"'.$branch.'"+"Contact Number: "+"'.$numb.'"+"Email: "+"'.$email.'"+"Summary: "+"'.$subject.'"</script>');
            }
        }

HTML 的HTML

      <form method="post" action="#">
        <center><div class="user-form">
        Name :<br>
        <input type="text" id="name" name="name" style="width: 300px" required>
        <br>
        Main Location :<br>
        <input <?php echo $disabled?> value='<?php echo $b?>' type="text" id="bank" name="bank" style="width: 300px" required>
        <br>
        Branch Needing Support :<br>
        <input type="text" id="branch" name="branch" style="width: 300px" required>
        <br>
        Contact Number :<br>
        <input type="text" id="numb" name="numb" style="width: 300px" required>
        <br>
        Email (optional) :<br>
        <input type="email" id="email" name="email" style="width: 300px">
        <br>
        Subject :<br>
        <textarea id="subject" name="subject" style="height:200px; width: 300px;" required></textarea>
        <br><br>
        </div></center>
        <center><input name="submit" type="submit" value="Submit"></center>
      </form>

EDIT Ive played with this a lot. 编辑我已经玩了很多。 I realize that isset($_POST) will return true. 我意识到isset($_POST)将返回true。 I suppose I really just need a way to run my function once values are submitted. 我想我真的只需要一种方法就可以在提交值后运行我的函数。 That way I can use the current POST values for my mailto . 这样,我可以将当​​前POST值用于我的mailto

if(isset($_POST))

$_POST will always be set, it is just empty if it's not a POST request. $_POST将始终被设置,如果不是POST请求,则为空。 So this if-statement will always return true. 因此,此if语句将始终返回true。

A simple fix would be to change this to: 一个简单的解决方法是将其更改为:

if(!empty($_POST))

But the better solution would be to check whether anything specific was actually posted: 但是更好的解决方案是检查是否实际发布了任何特定的东西:

if(isset($_POST['name']))

When form post u can handle too basicly 当表格发布时,您可以从根本上处理

if(count($_POST)>0)
{
    echo "form posted with fill inputs";

}

You need to use the post/redirect/get pattern 您需要使用发布/重定向/获取模式

Any time you receive post data, you need to first accomplish whatever the data was submitted for, and then redirect to another (or the same) page. 每次收到帖子数据时,您都需要先完成提交的数据,然后再重定向到另一个(或相同)页面。 This prevents unintended resubmissions because the browser history effectively skips the submission. 由于浏览器历史记录有效地跳过了提交,因此可以防止意外的重新提交。

Also, you should check for a specific value that you're expecting, Ie, 另外,您应该检查期望的特定值,即,

<?php
if(isset($_POST[‘submit’]) { ... 

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

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