0

I've been experimenting with Pjax and love it so far. It works great, but I've run into an issue. When submitting a form (post) with pjax, everything works, but I'm getting a "PHP Notice: Undefined index: name" in the error log.

Here's my code for submitting the form:

$(document).on('submit', 'form[data-pjax]', function(event) {
    event.preventDefault();
    $.pjax.submit(event, '#containercontainer');
    return false;
});

I've got all the required .js files included but not here for simplicity.

Here's an abridged version of my form:

<form data-pjax method="POST" action="add-process.php" id="addform">
    <div class="row">
        <div class="col-sm-5">
            <div class="form-group">
                <label for="name">Name:</label>
                <input type="text" class="form-control" id="name" placeholder="Name" name="name" autocomplete="off" autofocus>
            </div>
        </div>
        ...
    </div>
    <button id="submit_btn" type="submit" class="btn btn-success btn-block" style="font-weight:bold;">ADD</button>
</form>

In the add-process.php file, I'm getting the variables with $name = $_POST['name']; but the error log says undefined index for these lines.

UPDATE:

I changed my PHP code to include the recommended isset() as seen below:

// GET INFO FROM FORM
if(isset($_POST['userID'])) {
    $userID = $_POST['userID'];
}
if(isset($_POST['reason'])) {
    $reason = $_POST['reason'];
}
if(isset($_POST['otherreason'])) {
    $otherreason = $_POST['otherreason'];
}

if($reason == 3) {
    $reason = $otherreason;
}

if(!is_numeric($userID)) {
    echo 'User ID not numeric!';
    exit();
}

That fixed several things, but I'm still getting errors in the log:

PHP Notice:  Undefined variable: reason
PHP Notice:  Undefined variable: userID

Again, Thank you for any help!

2 Answers2

0

You get

PHP Notice:  Undefined variable: reason
PHP Notice:  Undefined variable: userID

because your isset($_POST['userID']) AND isset($_POST['reason']) returns false, because they are don't exist. That is why your variables undefined. You can add "default" values.

You can trace what your form send to server-side. Try this:

print_r($_POST);

or

print_r($_REQUEST);

And about form: check names of inputs. Are inputs with names 'userID' AND 'reason' exists?

Mykola Vasilaki
  • 416
  • 6
  • 11
  • Thank you! I commented out all the headers and put in echo statements to let me know how far it got and then tried print_r($_POST); and got this: "Array ( [userID] => 7 [reason] => 1 [otherreason] => ) Done!" Which I think is good! Would you mind helping me figure out what to do with this now? Thank you so much!!! – Matthew Greene May 27 '15 at 00:09
  • What you exactly want to say. ? explain a bit – Alive to die - Anant May 27 '15 at 00:16
  • I just want to be able to use these in php as regular variables... Like have them available for use as "echo $userID;" – Matthew Greene May 27 '15 at 00:34
0

Any time that you set variables inside of conditionals, it is best to initialize them first:

$reason = null;
$userID = false;

/* .... your current code starts here   */

PHP throw the notices to help you avoid typo-induced bugs (ie later referencing uesrID, etc). Initializing them first makes it clear that you are in fact using the name you intended.

Bryan Agee
  • 4,924
  • 3
  • 26
  • 42