简体   繁体   中英

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.

My issue is that even when refreshing the cache for the browser, my $_POST values are already set from a previous submit. Ideally I would want the post to reset every time.

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

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

if(isset($_POST))

$_POST will always be set, it is just empty if it's not a POST request. So this if-statement will always return 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’]) { ... 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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