This is a discussion on "Undefined index problem" within the PHP Forum section. This forum, and the thread "Undefined index problem are both part of the Program Your Website category.
|
|
|
|
|
![]() |
||
Undefined index problem
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
|||
|
Undefined index problem
Hi, i'm really stuck and would really apreciate some help...
I've made a simple page that will redirect a user to their desired page if they agree to a terms and conditions.... the thing is, if a user doesn't click the agree box, the page comes up but certain elements are removed from the page... the background and text formatting being the main bits.... I've viewed source on the page that loads once a user clicks submit without checking the box and it gives out the following error at the top of the page.... <b>Notice</b>: Undefined index: terms_ok in <b>C:\Program Files\EasyPHP 2.0b1\www\PopCap\game_redirect.php</b> on line <b>16</b><br /> If you do a refresh on this page that hasn't loaded correctly it does load the page correctly..... Please please please help me with this guys, i'll buy whoever helps a beer! Thanks, Andrew
|
|
|
|
|||
|
Re: Undefined index problem
As far as I understand it, if the user dosen't check that box, no value will be sent to the next page.
therefore you should say:
|
|
|||
|
Re: Undefined index problem
also, shouldn't this:
|
|
|||
|
Re: Undefined index problem
I tried using an else and it still isn't working, i don't think that's the problem.
My original code.... The script isn't run unless somebody presses the submit button, if they do and haven't ticked the box then valid is set to 0 and the terms_error variable is set and displayed when the page is reloaded. If the box is ticked then it is redirecting with the header function. Using an else is pretty much the same thing as i've already been doing i think..... It's really odd because when i refresh the page the background elements return and it is displayed correctly.
|
|
|||
|
Re: Undefined index problem
hmm ok, but wont this statement:
also if the user doesn't check the box, $_POST['terms_ok'] will equal "" wont it?? |
|
|||
|
Re: Undefined index problem
I've changed the code a little. You say that if a user doesn't tick the box then the $_POST['terms_ok'] variable will containt no data, all that will happen then is the $terms_error variable gets set and displayed on the following page. I don't see how the contents of the $_POST_['terms_ok'] variable stops certain elements of the page from loading but will appear if you do a refresh....
<?php //Pref Setup $terms_error = ''; $selected_game = $_GET['game']; $file_extension = '.php'; $go_where = $selected_game.$file_extension; $do_we_agree = 'yes'; //What do we do once the user hits submit? if (isset($_POST['submitted'])) { $valid = 1; $terms = $_POST['terms_ok']; if ( $terms!==$do_we_agree) { $valid = 0; $terms_error = 'You must select \'Yes\' to continue.'; } else { // If user agrees send them to their game header('Location:' .$go_where); exit; } } ?> |
|
|||
|
Re: Undefined index problem
Very strange, when i put the files on my server rather than on my desktop machine it loads and works no problem....
:-( |
|
|||
|
Re: Undefined index problem
lol, so long as it works I guess..
When the user ticks the box, the $terms_ok will equal whatever you put in the value field for the checkbox in the form. It won't equal yes or no.. |
|
|||
|
Re: Undefined index problem
sry if u know this already, but doesn't equal is !=
equals is == !== is valid but it means something else, i forget what. you could also say if (!$terms) means if $terms is not set, ie if they didn't check the box. |
|
|||
|
Re: Undefined index problem
Quote:
I've set a variable at the top of the code that also equals yes.... the final bit of code i put up is saying that if $_POST['terms_ok'] doesn't equal $do_we_agree then set the $valid to 0, if it does match then $valid equals 1 so redirect..... I didn't know that !== is different to != so i'll look into it.... Thanks. |
|
|||
|
Re: Undefined index problem
Quote:
anyways, glad it's working!! |
|
|||
|
Re: Undefined index problem
Ahhh, well in the code i posted above it will be set to "agree", i changed this and set the value to yes which is why i was saying it will be set to yes.... Oops! ;D
|
|
||||
|
Re: Undefined index problem
I think you have it sorted out here but must point out that !== & != although essentially doing a match comparison are quite different.
When PHP compares two values using != or ==, it will do variable type conversion. So, the integer 2 and the string '2' will match becuase PHP will convert the string to an integer based on its value. Similarly when checking say if($var) it is comparing againts a boolean value and whatever the variable contains will be converted to a boolean so an integer of 0 will translate to fale. When you use === or !== PHP doesn't do this type conversion for you so in the example above 2 will not equal '2' becuase they are different variable types and 0 wil not equal false for the same reason. This is an important difference when working with forms as all values are strings so you have to be careful you aren't using === to compare againts integers or booleans from a $_REQUEST value. Hope that makes sense..
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
|
![]() |
| Tags |
| form, php, undefined index |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Undefined Index error with DB | Sphinx111 | PHP Forum | 3 | Apr 21st, 2008 20:37 |
| design & code problem - movie index strategy and loading movies | betso | Web Page Design | 2 | Sep 11th, 2006 20:35 |
| Undefined index using $GET_ | LostProphet | PHP Forum | 7 | Aug 22nd, 2006 11:15 |
| Index Server search results problem | starrbuck | Classic ASP | 0 | Feb 22nd, 2006 17:20 |