Notice: Undefined index: buttonClicked in

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Notice: Undefined index: buttonClicked in

Post by jonnyfortis »

i have a checkout with two buttons but then the user is sent from the cart to the checkout im getting the following error

Code: Select all

Notice: Undefined index: buttonClicked in /home/erggnfr/public_html/web/AW16/checkout.php on line 259

Notice: Undefined index: buttonClicked in /home/erggnfr/public_html/web/AW16/checkout.php on line 288

the code is

line 259:

Code: Select all

if (is_numeric($_POST['buttonClicked']) && ($_POST['buttonClicked']==1)) {
288:

Code: Select all

elseif (is_numeric($_POST['buttonClicked']) && ($_POST['buttonClicked']==2)){
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: Notice: Undefined index: buttonClicked in

Post by pbs »

Try this

Code: Select all

$buttonClicked = isset($_POST['buttonClicked']) ? $_POST['buttonClicked'] : 0; // you can set default value instead of 0

if ($buttonClicked == 1)
....
.....
Last edited by requinix on Fri Sep 23, 2016 6:39 am, edited 1 time in total.
Reason: courtesy typo correction
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: Notice: Undefined index: buttonClicked in

Post by jonnyfortis »

pbs wrote:Try this

Code: Select all

$buttonClicked = isset($_POST['buttonClicked']) ? $_POST['buttonClicked'] : 0; // you can set default value instead of 0

if ($buttonClicked == 1)
....
.....

where to i put this in context the the previous script?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Notice: Undefined index: buttonClicked in

Post by Celauran »

Before your is_numeric checks. This first checks if it exists and, if not, initializes it to 0. You could even cast it to an int and remove the is_numeric checks altogether, depending on your requirements.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Notice: Undefined index: buttonClicked in

Post by Christopher »

Maybe something like this would be better:

Code: Select all

if (isset($_POST['buttonClicked']) {
    switch($_POST['buttonClicked']) {
    case '1':
        // code if 1
        break;
    case '2':
        // code if 2
        break;
    default:
        // invalid value error
    }
} else {
    // not set error
}
(#10850)
Post Reply