Page 1 of 2
Undefined index not sure where i'm going wrong
Posted: Tue Jun 15, 2004 7:37 am
by stantheman
I'm getting the error undefined index when this is ran
Code: Select all
<?php
$num = 0;
$num2 = 0;
if (isset($_REQUESTї'submit']))
{
$q1 = "";
$q1 = $_GETї'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;
}
}
print $num;
print $num2;
?>
Posted: Tue Jun 15, 2004 7:40 am
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 ?
Posted: Tue Jun 15, 2004 7:43 am
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
Posted: Tue Jun 15, 2004 7:44 am
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;
}
Posted: Tue Jun 15, 2004 7:46 am
by PAW Projects
Notice how you're trying to read q1 from the POST array, and q2 from the GET array ?
Posted: Tue Jun 15, 2004 7:49 am
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
Posted: Tue Jun 15, 2004 7:51 am
by PAW Projects
Can I see the HTML ?
Posted: Tue Jun 15, 2004 7:52 am
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>
Posted: Tue Jun 15, 2004 7:57 am
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.
Posted: Tue Jun 15, 2004 7:58 am
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.
Posted: Tue Jun 15, 2004 8:02 am
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;
Posted: Tue Jun 15, 2004 8:11 am
by magicrobotmonkey
throw a print_r($_POST) in there and see what it gives you
Posted: Tue Jun 15, 2004 8:12 am
by PAW Projects
The reason it's not showing anything, is because of this line:
You're still trying to read
$_GET['submit'], while your form is using the POST method.
Posted: Tue Jun 15, 2004 8:16 am
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
Posted: Tue Jun 15, 2004 8:21 am
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