9

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

6 Answers 6

28

Add names to the input types like

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

Now check the $_POST variable.

9

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

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

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.

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

Add the location of the file in action or use:

<form class="form-horizontal" action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
1
  • 1
    1. The action can be added through javascript. 2. Please don't advice to use $_SERVER['PHP_SELF']
    – itachi
    Feb 27, 2013 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.