Page 1 of 1

can any 1 find my error.

Posted: Thu Aug 06, 2009 1:30 pm
by bunty82
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hello,
After reading this script you will understand. that i am a NEW NEWBIE to php.
:D .Actually i was runing my mind on a html script in which user enter his mood value into the form and php do the answering.
but its not working.
please help.

Code: Select all

<html>
<head>
<title>Untitled Document</title>
</head>
 
<body>
 
<?php
 
 
if (isset($_POSTGET[$mood])){
 
switch ($mood)
{
case "happy":
print "i am happy";
break ;
 
case "sad":
print "i am sad" ;
break ;
default:
print "nethier happy nor sad";
}
}
?>  
  
<form action="" method="POST"/>
How is your mood?:
<br>
<select>
<option id="mood" value="happy">happy</option><br />
<option id="mood" value="sad">Sad</option><br />
<option id="mood" value="default">default</option><br/>
</select>
<br/>
<input type="submit" name="submit">
</body>
</html>

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: can any 1 find my error.

Posted: Thu Aug 06, 2009 1:43 pm
by jayshields
No such thing as $_POSTGET.
$_POSTGET[$mood] undefined.
No name attribute set for select element.

Re: can any 1 find my error.

Posted: Thu Aug 06, 2009 2:01 pm
by bunty82
i forgot to remove "GET" from there
please give me what u want to say in written i dont know which attribute ur talking baout.

Re: can any 1 find my error.

Posted: Thu Aug 06, 2009 2:14 pm
by pickle
1) You need to define the "name" attribute for the <select>. Otherwise it won't show up in $_POST. Setting the ID of the <option> does nothing in this instance.
2) $_POST[$mood] won't give you anything, because the $mood variable hasn't been defined. What you want is $_POST['mood']
3) It's spelled "neither" ;)

Re: can any 1 find my error.

Posted: Fri Aug 07, 2009 12:12 am
by agitator
Hi,

Kindly find below the corrected code (though there would be many other ways to achieve this):

<html>
<head>
<title>Untitled Document</title>
</head>

<body>

<?php


if (isset($_REQUEST["mood"])){

switch ($_REQUEST["mood"])
{
case "happy":
print "i am happy";
break ;

case "sad":
print "i am sad" ;
break ;
default:
print "nethier happy nor sad";
}
}
?>

<form action="" method="POST"/>
How is your mood?:
<br>
<select name="mood">
<option id="mood" value="happy">happy</option><br />
<option id="mood" value="sad">Sad</option><br />
<option id="mood" value="default">default</option><br/>
</select>
<br/>
<input type="submit" name="submit">
</body>
</html>

Re: can any 1 find my error.

Posted: Fri Aug 07, 2009 1:21 am
by bunty82
thanks for the help guys.
Now to tell you guys

$_REQUEST will read any method whether its post or get.
and
to get the values from the table i had to give ID is that true am i right.
then also i had to define the select name right...

Re: can any 1 find my error.

Posted: Fri Aug 07, 2009 2:10 am
by cpetercarter
You are right about $_REQUEST.

In a form, the important thing is that everything you want to send should have a 'name'. The 'name' is passed to $_POST. So, an input element with the name 'myname' will be passed as $_POST['myname']. You do not need to give form elements an id unless you need an id for css styling or for JavaScript.

Re: can any 1 find my error.

Posted: Fri Aug 07, 2009 1:05 pm
by bunty82
cpetercarter
you are right with the id thing
i tried running it without the ID it worked.
thanks man