/    Sign up×
Community /Pin to ProfileBookmark

index undefined?

this is my code:

[B]<?php

[COLOR=Red]$choice = $_POST[“choice”];[/COLOR]
echo $choice;

?>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml“>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″ />
<title>Untitled Document</title>

</head>

<body>
<form name=”form1″ method=”post”>
<input type=”radio” name=”choice” value=”green” checked=”checked”>
<input type=”radio” name=”choice” value=”red”>
<input type=”radio” name=”choice” value=”blue”>
<input type=”submit” name=”Submit” value=”Submit”>
</form>

</body>
</html>[/B]

at runtime there is an error “notice: index undefined on c:..… on line 3″ line 3 is the RED-colored line above, couldn’t figure it out why?
plz help

to post a comment
PHP

22 Comments(s)

Copy linkTweet thisAlerts:
@SheldonMar 09.2006 — single quotes
[code=php]$var = $_POST['var'];
[/code]


Notice how i use [ php ] code [/ php] tags with my code, you make sure you do the same, Every time
Copy linkTweet thisAlerts:
@bokehMar 09.2006 — The error is because [I]$_POST["choice"][/I] does not exist. Try:

[code=php]$choice = (isset($_POST["choice"])) ? $_POST["choice"] : NULL ;[/code]
Copy linkTweet thisAlerts:
@ssffccauthorMar 09.2006 — thx bokeh.

the code is functionally working, i.e. when I click on button, it shows expected result as different radio button is clicked. however, the "notice......." exists.

what do you mean by "$_POST["choice"] does not exist"?
Copy linkTweet thisAlerts:
@bokehMar 09.2006 — The first time you run the script there is no POST array set. Do the following.[code=php]<?php

$choice = (isset($_POST["choice"])) ? $_POST["choice"] : NULL ;
echo $choice;

?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Untitled Document</title>

</head>

<body>
<form name="form1" method="post">
<input type="radio" name="choice" value="green" checked="checked">
<input type="radio" name="choice" value="red">
<input type="radio" name="choice" value="blue">
<input type="submit" name="Submit" value="Submit">
</form>


</body>
</html>[/code]
Copy linkTweet thisAlerts:
@ssffccauthorMar 09.2006 — thx bokeh very very much, it works fine now~

I also tried to put it into 2 different files (without changing the code), works ok too. i.e.

firstly, the form code into aaa.html,

second: the php code in aaa.php:

I am a php beginer, could u please suggest which way is better?
Copy linkTweet thisAlerts:
@bokehMar 09.2006 — Why would you bother putting it in separate files? If you want to divide it the easiest way would be an if else statement.

Also I can see this is just a simple exercise but try to be more descriptive with your file naming.
Copy linkTweet thisAlerts:
@SpectreReturnsMar 10.2006 — single quotes
[code=php]$var = $_POST['var'];
[/code]


Notice how i use [ php ] code [/ php] tags with my code, you make sure you do the same, Every time[/QUOTE]

Quote style only matters when you need escapes or variable parsing.
Copy linkTweet thisAlerts:
@ssffccauthorMar 10.2006 — so do I have to use "isset" everytime when I get a value for a variable from user input?
Copy linkTweet thisAlerts:
@bokehMar 10.2006 — so do I have to use "isset" everytime when I get a value for a variable from user input?[/QUOTE]That depends on your level of error reporting, but it's a good idea anyway.
Copy linkTweet thisAlerts:
@ssffccauthorMar 10.2006 — since
[CODE]$choice = isset($_POST["choice"]) ? $_POST['choice'] : ''; [/CODE]
is another form of "if" condition.

is it possible to rewrite it to something like this?
[CODE]if ()
{}
else
{}[/CODE]
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYMar 10.2006 — yes you can
[code=php]
<?php
if(isset($_POST["choice"])){
$choice = $_POST["choice"];
}else{
$choice = "";
}
?>
[/code]
Copy linkTweet thisAlerts:
@bokehMar 10.2006 — yes you can
[code=php]
<?php
if(isset($_POST["choice"])){
$choice = $_POST["choice"];
}else{
$choice = "";
}
?>
[/code]
[/QUOTE]
Except that personally I'd go for a NULL value. By doing it your way you are giving the variable a type.
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYMar 10.2006 — i was merely writing the full if/else-statement of the first statement in post #11
Copy linkTweet thisAlerts:
@bokehMar 10.2006 — i was merely writing the full if/else-statement of the first statement in post #11[/QUOTE]And I was merely hoping to start an argument.
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYMar 11.2006 — Ok, let's solve that in the backyard, bare-handed!
Copy linkTweet thisAlerts:
@bokehMar 11.2006 — Ok, let's solve that in the backyard, bare-handed![/QUOTE]Don't you mean bare knuckles?
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYMar 11.2006 — yeah, whatever! :p
Copy linkTweet thisAlerts:
@ssffccauthorMar 12.2006 — thx all of you very much for helping me to understand this concept, however, I prefer to use NULL as it somehow free the memory (maybe?)
Copy linkTweet thisAlerts:
@bokehMar 12.2006 — I prefer to use NULL as it somehow free the memory[/QUOTE]Really? Where did you read that?
Copy linkTweet thisAlerts:
@SpectreReturnsMar 13.2006 — It doesn't. PHP's null is just equal to "". And bokeh, you damn troll!
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYMar 13.2006 — hahahaha
Copy linkTweet thisAlerts:
@bokehMar 13.2006 — PHP's null is just equal to "". [/QUOTE]Not quite. NULL means the variable has no attributes or value whereas "" sets the variable to be a string.
[code=php]if(NULL === "")
{
echo 'Spectre is right';
}
else
{
echo 'Spectre is wrong';
}[/code]
×

Success!

Help @ssffcc spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...