Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$_POST is empty after form submit

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 comes back empty. I am using WAMP on Windows 8 machine.

<?php
if(isset($_POST["send"])) {
    print_r($_POST)
} else {
    ?>
    <form class="form-horizontal" action="" 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">
            </div>
        </div>
        <div class="control-group"> <!-- Lastname -->
            <label class="control-label" for="inputEmail">last name</label>
            <div class="controls">
                <input type="text" id="inputLastname" placeholder="Last name">
            </div>
        </div>
        <div class="form-actions">
            <button type="submit" name="send" class="btn btn-primary">Submit data</button>
            <a href="<?php $mywebpage->goback(); ?>" class="btn">Cancel</a>
        </div>
    </form>
    <?php
}
?>

Thanks in advance

like image 226
pangi Avatar asked Feb 27 '13 10:02

pangi


2 Answers

Add names to the input types like

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

Now check the $_POST variable.

like image 168
Sid Avatar answered Oct 10 '22 18:10

Sid


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.

like image 28
RandomCoder Avatar answered Oct 10 '22 17:10

RandomCoder