$_POST is empty after form submit

后端 未结 6 525
执笔经年
执笔经年 2020-12-10 17:39

I am using Twitter Bootstrap to build a web application. I have a form where user should enter his name and last name and click submit. The problem is, that the $_POST

相关标签:
6条回答
  • 2020-12-10 18:04
    <?php
    if(isset($_POST["send"])) {
      echo $_POST["inputName"];
        echo $_POST["inputLastname"];
    } 
    else {
        ?>
        <form class="form-horizontal" action="<? echo $_SERVER['PHP_SELF']; ?>" method="POST">
            <div class="control-group"> <!-- Firstname -->
                <label class="control-label" for="inputEmail">First name</label>
                <div class="controls">
                    <input type="text" id="inputName" placeholder="First name" name="inputName"> 
                </div>
            </div>
            <div class="control-group"> <!-- Lastname -->
                <label class="control-label" for="inputEmail">last name</label>
                <div class="controls">
                    <input type="text" id="inputLastname" name="inputLastname" placeholder="Last name">
                </div>
            </div>
            <div class="form-actions">
                <input type="submit" name="send" class="btn btn-primary">Submit data</button>
                <a href="#" class="btn">Cancel</a>
            </div>
        </form>
        <?php
    }
    ?>
    
    0 讨论(0)
  • 2020-12-10 18:04

    I have notice that using 'name' attribute doesn't work well with Bootstrap Validator for (if(isset($_POST["send"]))).So in case if you like to add validation on your form then I recommend you to follow this way!

    Solution

    <button type="submit" class="btn btn-primary">Submit data</button>
    

    php

    if($_SERVER['REQUEST_METHOD'] == 'POST')
    
    0 讨论(0)
  • 2020-12-10 18:06

    Your inputs don't have name attributes. Set those:

    <input type="text" id="inputName" placeholder="First name" name="first_name" />
    <input type="text" id="inputLastname" placeholder="Last name" name="last_name" >
    

    Also, look at the way you detect a form submission:

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

    You check if the submit button is present in the post data. This is a bad idea because in the case of Internet Explorer, the submit button won't be present in the post data if the user presses the enter key to submit the form.

    A better method is:

    if($_SERVER['REQUEST_METHOD'] == 'POST')
    

    Or

    if(isset($_POST['first_name'], $_POST['last_name']))
    

    More info - Why isset($_POST['submit']) is bad.

    0 讨论(0)
  • 2020-12-10 18:10

    Plz try and add names to the input types like

    <input type="text" id="inputLastname" name="inputLastname" placeholder="Last name">
    

    Now check the $_POST variable.

    0 讨论(0)
  • 2020-12-10 18:11

    The problem is that you're checking if $_POST['send'] is set, but it won't be. The "send" field in your form is the button, but the button doesn't have a value, so it won't be included in $_POST.

    Hope that helps.

    0 讨论(0)
  • 2020-12-10 18:17

    Add the location of the file in action or use:

    <form class="form-horizontal" action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
    
    0 讨论(0)
提交回复
热议问题