hi guys, i have working on this simple cart script,it goes like when selecting the product in combobox it will display the details of the
product into the textbox, i can display the details but my problem is when i select another product the previous
selected product was replaced.. what should i do to keep the selected first,second.. and so on product in the form? thanks

my script so far...

<html>
<head>
</head>

<body>

<form action="insert.php" method="post">

<?php
mysql_connect("localhost","root","");
mysql_select_db("Inventory") or die(mysql_error());


$sql="SELECT * FROM product order by id";
$query=mysql_query($sql);

echo "<select name='select' size='5'>";

while($row = mysql_fetch_array($query))

{
if ($_POST['select'] == $row['id']){
$pquan=$row['pquan'];
$sel=$row['sellprice'];
$pcode = $row['pcode'];
}
$selected = '';

   if (isset($_POST['select']) && strcmp($row['id'], $_POST['select']) == 0) 
    {
        
       $selected = ' selected="selected"';
    
    } 

printf ('<option value="%s"%s>%s</option>'. PHP_EOL, $row['pname'], $selected, $row['pname']);

}

echo "</optgroup>";

?>

</select>

<?php

 if ($_POST['submit'])
 {
  
$i = $_POST['count'];
       for ($x=0;$x<=$i;$x++)

 {
       
    
?>

<br>
<input type="text" name="textbox<?php echo $x; ?>" value="<?php echo htmlspecialchars(@$_POST['select']); ?>" size=5/>
<input type="text" name="textbox<?php echo $x; ?>" value="<?php echo $pcode ?>" size=5/>
<input type="text" name="textbox<?php echo $x; ?>" value="<?php echo $pquan ?>" size=5/>
<input type="text" name="textbox<?php echo $x; ?>" value="<?php echo $sel ?>" size=5/>
<input type="text" name="textbox<?php echo $x; ?>" size=5/>

<input type="submit" name="delete" value="Delete" /> 

<?php
    }
    $i++;
?>

<input type="hidden" name="count" value="<?php echo $i; ?>" />

<?php

 }
  else 
{

?>

<input type="hidden" name="count" value="0" />

<?php
  } 
  $i--
?>



<br>

<input type="submit" name="submit" value ="Add Text Box" />

</form>
</body>
</html>

Recommended Answers

All 8 Replies

I'm not positive that this is the problem but it looks like the most likely culprit

if (isset($_POST['select']) && strcmp($row['id'], $_POST['select']) == 0) 
{

   $selected = ' selected="selected"';

}

I may be way off track here or I may be on the ball.

what should i do? should i change that lines? please help im so confused

what should i do? should i change that lines? please help im so confused

I'm still a novice at PHP. I don't know if that line is the culprit but it would be the one I'd suspect. What I would do is comment it out temporarily and test the script. Don't be afraid of commenting out code if you are on a testing server since you can easily uncomment it. I wouldn't comment out code if I didn't know what the outcome was going to be and the code was live online.
**********
I am referring to the line

$selected

that is within your if statement.

ok thanks tiger86, i just need to know what should i do im very confused.. anyone 's help much more appriciated.. thanks

ok thanks tiger86, i just need to know what should i do im very confused.. anyone 's help much more appriciated.. thanks

Karin may I suggest making it a multiple list, checkbox list, or a radio group instead of a drop down menu?

yes.maybe your right it should be more easier with check list or radio group but i started to make it in combobox and in my project im required to use combobox and textbox, is there any way to do it with that?

use LIST or Array of your products so you can manage the previous product information with next one.

can you show me how to start with array i'am a newbie in php i tried to search for more tutorials from the net but im still confused in using array.. can you guide me please..!!?

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.