ron tornambe
ron tornambe

Reputation: 10780

Bootstrap form not passing value to php

I cannot understand why the input field "name" is not being passed to PHP. I tried commenting out the "name" code, but the same problem occurred with "company". I am setting the id and name properties on both input tags.

Revised

website example

HTML

<form name="createAccount" role="form" method="POST" action="php/AddNewAccount.php" >
    <div data-toggle="buttons">
        <div class="btn-group">
            <label class="btn btn-primary">
                <input type="radio" name="user-type" id="writer" value="writer" class="sr-only" required >Writer
            </label>
            <label class="btn btn-primary">
                <input type="radio" name="user-type" id="enabler" value="enabler" class="sr-only" required>Enabler
            </label>
        </div>
    </div><br/>
    <div class="form-group">
        <label for="name">Name:</label>
        <input class="form-control" required id="name" name="name">
    </div>
    <div class="form-group">
        <label for="company">Company:</label>
        <input class="form-control" required id="company" name="company">
    </div>
...
</form>

PHP

function AddNewAccount() {
try {
    $acctType = $_POST["user-type"];
    if (!isset($acctType) || empty(trim($acctType))) {
        throw new Exception('You must select an account type."');       
    };
    echo "account-type=" . $acctType;
    $name = $_POST["name"];
    if (!isset($name) || empty(trim($name))) {
        throw new Exception('You must enter your name."');       
    };
    $company = $_POST["company"];
    if (!isset($company) || empty(trim($company))) {
        throw new Exception('You must enter your company name."');       
    };

...
}

Error

[03-May-2016 21:41:44 UTC] PHP Fatal error: Uncaught exception 'Exception' with message 'You must enter your company name."' in /home/deje/public_html/writers-tryst/php/AddNewAccount.php:18

Note the "user-type" is being passed correctly.

Upvotes: 4

Views: 946

Answers (3)

ron tornambe
ron tornambe

Reputation: 10780

Believe it or not, the problem was with the input tags ending in ">" instead of "/>". I have been testing in Chrome. I tested in IE now and it works there too. Thanks for everyone's input.

Upvotes: 0

Poiz
Poiz

Reputation: 7617

From what I see; your Code should work... I couldn't spot any errors, though. But, Why do you have an extra (") within your Exception Message? Are you using any Javascript on the Page? If Yes; I'd suggest you disable Javascript Temporarily and try sending the Form again without JS.

Could you try this instead?

function AddNewAccount() {
    // IT MIGHT BE A GOOD IDEA (IF YOU WILL) TO DUMP THE ENTIRE $_POST GLOBAL VARIABLE
    // AND END THE SCRIPT...(TEMPORARILY)... JUST TO SEE WHAT COMES BACK
    // var_dump($_POST); exit; // BYPASSTHE DUMPING SINCE WE ARE SURE COMPANY MAKES IT THROUGH TO THIS POINT...
    try {
        $name      = isset($_POST["name"])      ?  htmlspecialchars(trim($_POST["name"]))       : null;
        $company   = isset($_POST["company"])   ?  htmlspecialchars(trim($_POST["company"]))    : null;
        $acctType  = isset($_POST["user-type"]) ?  htmlspecialchars(trim($_POST["user-type"]))  : null;

        if (!$acctType) {
            throw new Exception('You must select an account type.');       
        }

        if (!$name) {
            throw new Exception('You must enter your name.');       
        }

        if (!$company) {
            throw new Exception('You must enter your company name.');       
        }

...
}

This the Result from the var_dump($_POST) on your App.

enter image description here

As you can see, Your code is still consistent and you now have the Company in the list of your Data.... so you may want to comment out the var_dump(); exit; part

<?php
    //....PREVIOUS CODE
    // var_dump($_POST); exit; //NOW COMMENTED OUT TO CONTINUE WITH THE PROGRAM

Try this and let's know how it goes... I sincerely hope it goes well with you Now, though ;-)

Upvotes: 2

Patrice Laram&#233;e
Patrice Laram&#233;e

Reputation: 96

Try the following:

 <form name="createAccount" role="form" method="POST" action="php/AddNewAccount.php" enctype="application/x-www-form-urlencoded">

You can also parse it as a JSON:

 $rest_json = file_get_contents("php://input");
 $_POST = json_decode($rest_json, true);

Upvotes: 0

Related Questions