...

View Full Version : How to use one FORM against different entries



eshban
09-07-2005, 08:43 PM
HI,

I have a php page in which i have different products. I retrieve these products from database. Mean a loop runs and one by one it will retrieve all the products from the database.

Then i add a simple button "ADD to cart".

this button will repeat will displayed at every product.

Means if there are 6 products, then it will show 6 buttons of "Add to CART".

I want that if user click on first "add to cart" button, then it will post the values of only first product to database. If if user click on second "add to cart" button, then it will post the values of only second product to database.


But add to cart button just send the values of only first prodcut.

Kindly help me in this regard.


Here is my sample code.

PHP:
$query = mysql_query("Select * from newproductinfo where pid=$pageid",$db) or die ("Error");
$rowcount=0; //count the iterations of loop to control the table structure

while ($result = mysql_fetch_array($query))
{
$pid=$result['pid'];
$ppid=$result['productid'];
print "<br> $ppid";

$title=$result['title'];
$pname=$result['productname'];
$desc=$result['description'];
$size=$result['size'];
$image=$result['image'];
$price = $result['price'];
$a = $result['image'];

if($rowcount==2){

echo"</tr>";
echo"<tr>";
echo"<td width='50%' height='399' align='left' valign='top'>";
$rowcount=1;
}

else{

echo "<td width='50%' height='399' align='left' valign='top'>";
$rowcount = $rowcount+1;
}

////////////// Displaying Image and other things///////////////////////////////////////

echo"<center><img src='images/".$a."'height='180' width='200'></center>";
echo "<br><br>$ppid<br> $title<br> $pname<br> $desc<br> $size<br> $price<br><br>";

echo"<form action='test.php?btn=$ppid' method='post'>";
echo"<center><input type='submit' name='SUBMIT' value='Add to Cart'></center>";


echo"</td>";
echo"</form>";
} //while Loop Ends

I don't want to use seperate forms for each product . I just want to use one form against each product

kindly correct this code, so that it will only send the information of that product, which i clicked.

with this code it will only sent the first value of $ppid to other page.


bye

NancyJ
09-07-2005, 11:01 PM
you already posted almost *exactly* the same thing.
http://www.codingforums.com/showthread.php?t=67655

to only use 1 form move your <form> and </form> tags outside the while loop. (you will have to remove the ?$ppid from the form action.

for the seocnd part of your question, there is no way to only send the information from 1 product using 1 form. A form can only submit all of itself. You also have nothing in the form except a submit button, so you're not actually submitting anything. (except the button itself)

You seem to want to pass the product id through the querystring, in which case you dont need a form at all. Just a link with test.php?btn=$ppid as the url (you can style the link to look like a button, or use a graphic).

missing-score
09-08-2005, 01:26 AM
Actually, if you give the button a name and value, differnet submit buttons can lead to different outcomes... This way you only need one big form, and submit buttons could look like:



<input type="submit" name="ppid" id="ppid" value="<?php echo $ppid; ?>" />


... or you get the general idea? You see, when submit buttons are pressed, they also submit their name and value, so I think this could accomplish what you are looking for.

NancyJ
09-08-2005, 08:06 AM
Actually, if you give the button a name and value, differnet submit buttons can lead to different outcomes... This way you only need one big form, and submit buttons could look like:



<input type="submit" name="ppid" id="ppid" value="<?php echo $ppid; ?>" />


... or you get the general idea? You see, when submit buttons are pressed, they also submit their name and value, so I think this could accomplish what you are looking for.
Then the button text would read the product id rather than 'Add to Cart' ;)

missing-score
09-08-2005, 12:03 PM
Oh yeah :S

I know I have done something along the lines of this before though, but it was long long ago..

Still, stilly mistake on my part :rolleyes:

NancyJ
09-08-2005, 12:27 PM
I've pondered that solution to a problem many times, then had the spark of rememberance lol.

One way of doing it is to include the ppid in with the button name

to the button might be called "add|24" then at the processing stage you could do a

foreach($_POST as $key => $val){
if(strpos( $key, "add")){
$arrData = split("|", $key);
$ppid = $arrData[1];
}
}


buts thats a lot of work when you could just use a link and pass the ppid through $_GET
and use


$ppid = $_GET['ppid'];


There really is no reason to use a form if you're only passing 1 piece of data.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum