Jump to content

PHP Help


mmcg_33

Recommended Posts

I've just done a tutorial from a book (PHP and MySQL for Dynamic Websites - Larry Ullman).

 

I'm pulling my hair out at the moment as I'm sure I've followed it to the letter, but can't get it to echo variables from an array of selection buttons.

 

here's the code from the html page

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>

<form action="handle_multi_array.php" method="post">
<fieldset><legend>Enter your information in the form below:</legend>
    	<p>Name: <input type="text" name="name" size="20" maxlength="40"/></p>
        <p>Interests:
        	<input type="checkbox" name="interests[]" value="Music" /> Music
            <input type="checkbox" name="interests[]" value="Movies" /> Movies
            <input type="checkbox" name="interests[]" value="Books" /> Books
            <input type="checkbox" name="interests[]" value="Skiing" /> Skiing
            <input type="checkbox" name="interests[]" value="Napping" /> Napping
        </p>   	
    </fieldset>
    <input type="submit" name="submit" value="Submit my information" />
</form>

</body>
</html>

 

and here's the php handler code...

 

<!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=UTF-8" />
<title>Untitled Document</title>
</head>

<body>

<?php

//VALIDATE THE NAME FIELD
if(!empty($_POST['name'])){
$name = stripslashes($_POST['name']);
}
else{
$name = NULL;
echo '<font color="red">You didn\'t enter your name!</font>';
}

//VALIDATE THE CHECKBOXES
if(isset($_POST['interests'])){
$interests = TRUE;
}
else{
$interests = NULL;
echo '<p><font color="red">You didn\t select any interests!</font></p>';
}

//CHECK SOMETHING WAS ENTERED FOR BOTH VARIABLES
if($name && $interests){
echo "Thank you $name, you entered your interests as:<ul> ";

foreach($_POST['$interests'] as $value) {
	echo "<li>$value</li>\n";
}

echo '</ul>';
}
else{
echo '<p><font color="red">Go back and fill out the form again</font></p>';
}

?>


</body>
</html>

 

it's this bit I can't get to work -

 

foreach($_POST['$interests'] as $value) {

echo "<li>$value</li>\n";

}

 

thanks

 

Link to comment
Share on other sites

That wouldn't help. Try:

 

foreach($_POST['interests'] as $value) {
	echo "<li>$value</li>\n";
}

 

You shouldn't have had the $sign in there.

 

interpim: FYI: text inside single quotes isn't parsed. If you want a variable to be echoed , you must place it inside double quotes, or no quotes at all. With that code, the text $value would literally be displayed.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.