[SOLVED] Undefined index not sure where i'm going wrong

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

stantheman
Forum Commoner
Posts: 38
Joined: Wed May 26, 2004 8:57 am

Undefined index not sure where i'm going wrong

Post by stantheman »

I'm getting the error undefined index when this is ran

Code: Select all

<?php

$num = 0;
$num2 = 0;

if (isset($_REQUEST&#1111;'submit']))
&#123;
$q1 = "";
$q1 = $_GET&#1111;'q1'];
switch ($q1)
&#123;
case "test":
$num = 100;
break;

default:
$num = 0;
break;
&#125;

$q2="";
$q2 = $_GET&#1111;'q2'];
switch ($q2)
&#123;
case "tes":
$num2 = 1000;
break;

default:
$num2 = 0;
break;
&#125;

&#125;
print $num;
print $num2; 

?>
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

which ones undefined? submit, q1, or q2 probsbly no submit because its being tested for submit. Are you sure you dont want $_POST instead og $_GET ?
PAW Projects
Forum Commoner
Posts: 30
Joined: Tue Jun 15, 2004 7:43 am
Contact:

Post by PAW Projects »

I'm not getting an error at all when running this code (http://test.vanens.com/test.php)

Are you sure you're sending the values via GET, and not via POST?

edit: beaten
Last edited by PAW Projects on Tue Jun 15, 2004 7:44 am, edited 1 time in total.
stantheman
Forum Commoner
Posts: 38
Joined: Wed May 26, 2004 8:57 am

Post by stantheman »

I got ride of the undefined know but i'm not able to get the value from my case statement i have a check box on the page before that returns test if checked and then i want to set $num to 100 but it keeps defaulting to my default and give me zero.

if (isset($_GET['submit']))
{
$q1 = "";
$q1 = $_POST['q1'];
switch ($q1)
{
case "test":
$num = 100;
break;

default:
$num = 0;
break;
}

$q2="";
$q2 = $_GET['q2'];
switch ($q2)
{
case "tes":
$num2 = 1000;
break;

default:
$num2 = 0;
break;
}
PAW Projects
Forum Commoner
Posts: 30
Joined: Tue Jun 15, 2004 7:43 am
Contact:

Post by PAW Projects »

Notice how you're trying to read q1 from the POST array, and q2 from the GET array ?
stantheman
Forum Commoner
Posts: 38
Joined: Wed May 26, 2004 8:57 am

Post by stantheman »

I've swicthed them both from $_GET to $_POST and vice versa and still doesn't give me any value it keeps defaulting to my default
PAW Projects
Forum Commoner
Posts: 30
Joined: Tue Jun 15, 2004 7:43 am
Contact:

Post by PAW Projects »

Can I see the HTML ?
stantheman
Forum Commoner
Posts: 38
Joined: Wed May 26, 2004 8:57 am

Post by stantheman »

<table border="0" align="center">

<form action="test.php" method="post">
<tr>
<td><input type="checkbox" name="q1" value="test"></td>
<td>Great room window</td>
<td><div align="right">$1308.00</div></td>
</tr>
<tr>
<td><input type="checkbox" name="q2" value="tes"></td>
<td>Cable TV outlets in basement</td>
<td><div align="right">$115.00</div></td>
</tr>
</form>
</table>
PAW Projects
Forum Commoner
Posts: 30
Joined: Tue Jun 15, 2004 7:43 am
Contact:

Post by PAW Projects »

Aha. Okay,
First: All variables are POSTed here
Second: Checkboxes return 'on' when checked, and are not set when unchecked.

So by checking if $q1 == 'on', you solve the problem.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

i think you can simply use if(isset($_POST['q1'])) and if(isset($_POST['q2'])) - with checkboxes if they are not checked they will not be postedd.

you can use print_r($_POST) to confirm what's coming from your form.
stantheman
Forum Commoner
Posts: 38
Joined: Wed May 26, 2004 8:57 am

Post by stantheman »

Here is all my code it still only returns 0 0 for its values

<table border="0" align="center">

<form action="test.php" method="post">
<tr>
<td><input type="checkbox" name="q1"></td>
<td>Great room window</td>
<td><div align="right">$1308.00</div></td>
</tr>
<tr>
<td><input type="checkbox" name="q2"></td>
<td>Cable TV outlets in basement</td>
<td><div align="right">$115.00</div></td>
</tr>
</form>
</table>

//This is test.php

if (isset($_GET['submit']))
{
$q1 = "";
$q1 = $_POST['q1'];
switch ($q1)
{
case "on":
$num = 100;
break;

default:
$num = 0;
break;
}

$q2="";
$q2 = $_POST['q2'];
switch ($q2)
{
case "on":
$num2 = 1000;
break;

default:
$num2 = 0;
break;
}

}
print $num;
print "<br>";
print $num2;
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

throw a print_r($_POST) in there and see what it gives you
PAW Projects
Forum Commoner
Posts: 30
Joined: Tue Jun 15, 2004 7:43 am
Contact:

Post by PAW Projects »

The reason it's not showing anything, is because of this line:

Code: Select all

if (isset($_GET['submit']))
You're still trying to read $_GET['submit'], while your form is using the POST method.
stantheman
Forum Commoner
Posts: 38
Joined: Wed May 26, 2004 8:57 am

Post by stantheman »

when i change everything to POST i get this

Notice: Undefined index: q1 in c:\inetpub\wwwroot\dutchman's demo\test.php on line 11
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

did you check q1 in your form? You need to test for isset($_POST['q1']) (and q2!) with check boxes if they aren't checked then they aren't posted hence undefined index because there is no q1 in the $_POST superglobabl array
Post Reply