pangi
pangi

Reputation: 2723

$_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
}
?>

Upvotes: 9

Views: 48549

Answers (6)

Sid
Sid

Reputation: 856

Add names to the input types like

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

Now check the $_POST variable.

Upvotes: 28

ErcanE
ErcanE

Reputation: 1641

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')

Upvotes: 0

Nirav Ranpara
Nirav Ranpara

Reputation: 13785

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

Upvotes: -1

Dan
Dan

Reputation: 548

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.

Upvotes: -1

RandomCoder
RandomCoder

Reputation: 1428

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.

Upvotes: 9

Vineet1982
Vineet1982

Reputation: 7918

Add the location of the file in action or use:

<form class="form-horizontal" action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">

Upvotes: -2

Related Questions