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
Post
by stantheman » Tue Jun 15, 2004 7:37 am
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;
?>
magicrobotmonkey
Forum Regular
Posts: 888 Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA
Post
by magicrobotmonkey » Tue Jun 15, 2004 7:40 am
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 » Tue Jun 15, 2004 7:43 am
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 » Tue Jun 15, 2004 7:44 am
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 » Tue Jun 15, 2004 7:46 am
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 » Tue Jun 15, 2004 7:49 am
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
stantheman
Forum Commoner
Posts: 38 Joined: Wed May 26, 2004 8:57 am
Post
by stantheman » Tue Jun 15, 2004 7:52 am
<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 » Tue Jun 15, 2004 7:57 am
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 » Tue Jun 15, 2004 7:58 am
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 » Tue Jun 15, 2004 8:02 am
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 » Tue Jun 15, 2004 8:11 am
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 » Tue Jun 15, 2004 8:12 am
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.
stantheman
Forum Commoner
Posts: 38 Joined: Wed May 26, 2004 8:57 am
Post
by stantheman » Tue Jun 15, 2004 8:16 am
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 » Tue Jun 15, 2004 8:21 am
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