hello, i have just begining learn php, in this form every things looks ok, however a error message that i can't solve it. please give some help because i cant move on before solve this problem. thanks

HTML FORM CODE

<html>
<head>
<title>simple HTML FORM</title>
</head>
<body>
<form method="post" action="listing9.2.php">
<p><strong> Name: </strong><br>
<input type="text" name="user" /></p>
<p><strong>Address: </strong><br>
<textarea name="address" rows="5" cols="40"></textarea>

<p><strong>Select the name of the products:</strong><br>
<select name="products[]" multiple>
<option value="product 1">product 1</option>
<option value="product 2">product 2</option>
<option value="product 3">product 3</option>
<option value="product 4">product 4</option>
</select>
<br>
<br>
  <p><input type="submit" value="send"></p>
</form>
</body>
</html>

PHP CODE

<html>
<head>
<title>READING IMPUTS FROM THE FORM </title>
</head>
<body>
<?php
    echo "<p><b>YOUR NAME IS:  </b>$_POST[user]</p>";
    echo "<p><b>YOUR ADDRESS IS:  </b>$_POST[address]</p>";
    echo "<p><br>YOUR PRODUCT CHOICES ARE:<br></p>";
if(!empty($_POST[products])){
    echo "<ul>";
    foreach($_POST[products] as $value) {
        echo "<li>$value";
    }
    echo "</ul>";
}
?>
</body>
</html>

MESSAGE ERROR

YOUR NAME IS: MR. JHON

YOUR ADDRESS IS: 54, HIGH ROAD, London - uk

YOUR PRODUCT CHOICES ARE:

Notice: Use of undefined constant products - assumed 'products' in C:\wamp\www\phptest\listing9.2.php on line 10

Notice: Use of undefined constant products - assumed 'products' in C:\wamp\www\phptest\listing9.2.php on line 12

product 1
product 2
product 4

it works but with the error message, please help me to solve it, many thanks

Recommended Answers

All 4 Replies

<select name="products[]" multiple>

use

<select name="products" multiple>

its not an array, until its posted

Your php code is incorrect and needs quotes inside the array. So the correction is the following:

<html>
<head>
<title>READING IMPUTS FROM THE FORM </title>
</head>
<body>
<?php
echo "<p><b>YOUR NAME IS: </b>".$_POST['user']."</p>";
echo "<p><b>YOUR ADDRESS IS: </b>".$_POST['address']."</p>";
echo "<p><br>YOUR PRODUCT CHOICES ARE:<br></p>";
if(!empty($_POST['products'])){
echo "<ul>";
foreach($_POST['products'] as $value) {
echo "<li>$value";
}
echo "</ul>";
}
?>
</body>
</html>

And please use code tags as it makes the code easier to read.

MANY THANKS FOR YOUR IDEA, i have replaced single quotes to double quotes and its working now, i really didnt know that, well, many thanks for anwer my questions, helped lots,

to keep the generated html valid
echo "<li>$value</li>";[code=php]echo "<li>$value</li>";

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.