Undefined index problem

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.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Aug 30th, 2007, 09:52
Junior Member
Join Date: May 2006
Location: London
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
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

PHP: Select all

<?php 
 
//Pref Setup 
$terms_error ''
$selected_game $_GET['game']; 
$file_extension '.php'
$go_where $selected_game.$file_extension
 
$do_we_agree 'no'
 
//What do we do once the user hits submit? 
if (isset($_POST['submitted'])) { 
    
$valid 1
 
if (
$_POST['terms_ok']=$do_we_agree) { 
      
$valid 0
      
$terms_error 'Please try again.'
    } 
 
// If user agrees send them to their game 
            
if ( $valid == ) { 
                 
header('Location:' .$go_where); 
                exit; 
            } 

?>
HTML: Select all
<!-- Elements Begin --> 
<?php include "common_elements.html"; ?> 
 
<!-- Terms & Conditions Begin -->        
<div class="game_holder">Terms & Conditions<br><br> 
 
<!-- Terms Checkbox Begin --> 
<form enctype="multipart/form-data" method="post" action=""> 
 
<!-- Terms Text Begin --> 
<iframe width="450" height="332" src="terms.html"></iframe> 
<!-- Terms Text End --> 
 
<br> 
 
<span style="color:#666666";>I Agree to the Terms & Conditions:</span> 
 
<input type="checkbox" name="terms_ok" value="agree"></input> 
 
<span style="font-size:8pt; color:red;"><?php echo $terms_error; ?></span> 
<br> 
 
<input type="submit" name="submitted" value="Lets Go!" ></input> 
 
 
</form> 
<!-- Terms Checkbox End --> 
 
</div> 
<!-- Terms & Conditions End --> 
 
 
<!-- Ads Begin -->    
<!-- Sky --> 
   <div class="skyscraper">Sky Ad</div> 
 
<!-- Leaderboard --> 
<div class="banner">Banner Ad<div> 
<!-- Ads End -->    
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Aug 30th, 2007, 10:16
SuperMember

SuperMember
Join Date: Apr 2007
Location: Sydney
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
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:

Code: Select all
 
if (checkboxname != "") // if it has been checked
{
    do this
}
else
{
     do this // it will only do this if the box has not  been checked
}
hope this helps.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Aug 30th, 2007, 10:17
SuperMember

SuperMember
Join Date: Apr 2007
Location: Sydney
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Undefined index problem

also, shouldn't this:

Code: Select all
 
if ($_POST['terms_ok']=$do_we_agree)
be:

Code: Select all
 
if ($_POST['terms_ok']==$do_we_agree)
could be wrong of course..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Aug 30th, 2007, 10:32
Junior Member
Join Date: May 2006
Location: London
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
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.

PHP: Select all

<?php 
 
//Pref Setup 
$terms_error ''
$selected_game $_GET['game']; 
$file_extension '.php'
$go_where $selected_game.$file_extension
 
$do_we_agree 'no'
 
//What do we do once the user hits submit? 
if (isset($_POST['submitted'])) { 
    
$valid 1
 
if (
$_POST['terms_ok']=$do_we_agree) { 
      
$valid 0
      
$terms_error 'Please try again.'
    } 
 
// If user agrees send them to their game 
            
if ( $valid == ) { 
                 
header('Location:' .$go_where); 
                exit; 
            } 
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Aug 30th, 2007, 10:38
SuperMember

SuperMember
Join Date: Apr 2007
Location: Sydney
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Undefined index problem

hmm ok, but wont this statement:

Code: Select all
 
($_POST['terms_ok']=$do_we_agree)
always be true because of the = instead of ==

also if the user doesn't check the box, $_POST['terms_ok'] will equal ""

wont it??
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Aug 30th, 2007, 10:46
Junior Member
Join Date: May 2006
Location: London
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
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;
}
}
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Aug 30th, 2007, 10:50
Junior Member
Join Date: May 2006
Location: London
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
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....

:-(
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Aug 30th, 2007, 10:56
SuperMember

SuperMember
Join Date: Apr 2007
Location: Sydney
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
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..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old Aug 30th, 2007, 11:01
SuperMember

SuperMember
Join Date: Apr 2007
Location: Sydney
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10  
Old Aug 30th, 2007, 11:12
Junior Member
Join Date: May 2006
Location: London
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Undefined index problem

Quote:
Originally Posted by nate2099 View Post
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..
if the box is ticked the $_POST['terms_ok'] will equal 'yes' because that is what i set its value to.

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #11  
Old Aug 30th, 2007, 12:20
SuperMember

SuperMember
Join Date: Apr 2007
Location: Sydney
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Undefined index problem

Quote:
if the box is ticked the $_POST['terms_ok'] will equal 'yes' because that is what i set its value to.
not according to the html in the file above...

anyways, glad it's working!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #12  
Old Aug 31st, 2007, 10:12
Junior Member
Join Date: May 2006
Location: London
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #13  
Old Sep 2nd, 2007, 12:19
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
form, php, undefined index

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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


All times are GMT. The time now is 06:52.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42