Jump to content

[SOLVED] Random Number of Forms on one Page


skyer2000

Recommended Posts

I'm having some problems figuring out the best way to handle this. What I have is a mysql query pulling out a certain number of user created records. This is then listed onto a page.

 

What I can't figure out is a way to make it so that each item has a form associated with it, that will post to the same page. Most preferably using a javascript link like

<a href="javascript:document.hey.submit();">Hey</a>

 

I know that the "hey" form can have a dynamically created number next to it, but how is that going to fix the fact that the code of checking the form submission is only

if (isset($_POST['submit']))

 

Any ideas? Solutions?

Link to comment
Share on other sites

I think you should look to use type='hidden' and you can pass in a variable to the name (as I have beneath with $c).

 

Make sense?

 

	<tr>
		<td class='dataleft'><input type="hidden" id='cart_image<? echo "_".$c; ?>' name='cart_image<? echo "_".$c; ?>' value="<? echo $image; ?>"><? echo $image; ?></td>
		<td class='dataleft'><input type="hidden" id='cart_pkg<? echo "_".$c; ?>' name='cart_pkg<? echo "_".$c; ?>' value="<? echo $row['packaging_desc']; ?>"><? echo $row['packaging_desc']; ?></td>
		<td class='dataleft'><input type="hidden" id='cart_msg<? echo "_".$c; ?>' name='cart_msg<? echo "_".$c; ?>' value="<? echo $row['packaging_message']; ?>"><? echo $row['packaging_message']; ?></td>
		<td class='datacenter'><input type="hidden" id='cart_qty<? echo "_".$c; ?>' name='cart_qty<? echo "_".$c; ?>' value="<? echo $row['packaging_quantity']; ?>"><? echo $row['packaging_quantity']; ?></td>
		<td class='datacenter'><input type="hidden" id='cart_cello<? echo "_".$c; ?>' name='cart_cello<? echo "_".$c; ?>' value="<? echo $row['packaging_cello']; ?>"><? echo $row['packaging_cello']; ?></td>
		<td class='datacenter'>£<input type="hidden" id='cart_prc<? echo "_".$c; ?>' name='cart_prc<? echo "_".$c; ?>' value="<? echo $row['packaging_price']; ?>"><? echo number_format($row['packaging_price'],2,'.',''); ?></td>
		<td class='datacenter'><input name='qty<? echo "_".$c; ?>' type="text" id='qty<? echo "_".$c; ?>' size="3"></td>


	</tr>

Link to comment
Share on other sites

Do you want a table structure like this, with one submit/click per row?:

 

item a1  item b1  item c1 SUBMIT

item a2  item b2  item c2 SUBMIT

item a3  item b3  item c3 SUBMIT

 

and you want to be able to refer to the item for the row you submitted?

 

Or, completely differently, do you have a structure like:

 

a1  b1  c1

a2  b2  c2

a3  b3  c3

 

and you want to know which item was clicked?

Link to comment
Share on other sites

OK, so the code I posted should work.

 

Here's the whole code for the form:

 

<form action="/cart.php" method="post" id="mycart">
<table width="100%">
<tr>
	<td width="23px"></td>
	<td width="163px">
		<a href="/greetingscards.php" ><img src="images/viewother.gif" alt="Cards Un Limited" width="163" height="26" border="0" /></a>

	</td>
	<td width="163px"></td>
	<td width="163px">
		<input type="image" src="images/addtocart.gif"  id="submit5" name="submit5">
	</td>
	<td width="23px"></td>
</tr>
</table>


</div>
</div>


<div id="packbox">

  <table width="535px" id='table1'>
	<tr>
		<th id='table1'>Design</th>			
		<th id='table1'>Packaging</th>
		<th id='table1'>Message</th>
		<th id='table1'>Qty</th>
		<th id='table1'>Cello</th>
		<th id='table1'>Price</th>
		<th id='table1'>Quantity</th>

	</tr>		


	<?	
	$c=0;
	//$c=$_SESSION['count'];
	while ($row=mysql_fetch_assoc($result3)) { ?>
	<tr>
		<td class='dataleft'><input type="hidden" id='cart_image<? echo "_".$c; ?>' name='cart_image<? echo "_".$c; ?>' value="<? echo $image; ?>"><? echo $image; ?></td>
		<td class='dataleft'><input type="hidden" id='cart_pkg<? echo "_".$c; ?>' name='cart_pkg<? echo "_".$c; ?>' value="<? echo $row['packaging_desc']; ?>"><? echo $row['packaging_desc']; ?></td>
		<td class='dataleft'><input type="hidden" id='cart_msg<? echo "_".$c; ?>' name='cart_msg<? echo "_".$c; ?>' value="<? echo $row['packaging_message']; ?>"><? echo $row['packaging_message']; ?></td>
		<td class='datacenter'><input type="hidden" id='cart_qty<? echo "_".$c; ?>' name='cart_qty<? echo "_".$c; ?>' value="<? echo $row['packaging_quantity']; ?>"><? echo $row['packaging_quantity']; ?></td>
		<td class='datacenter'><input type="hidden" id='cart_cello<? echo "_".$c; ?>' name='cart_cello<? echo "_".$c; ?>' value="<? echo $row['packaging_cello']; ?>"><? echo $row['packaging_cello']; ?></td>
		<td class='datacenter'>£<input type="hidden" id='cart_prc<? echo "_".$c; ?>' name='cart_prc<? echo "_".$c; ?>' value="<? echo $row['packaging_price']; ?>"><? echo number_format($row['packaging_price'],2,'.',''); ?></td>
		<td class='datacenter'><input name='qty<? echo "_".$c; ?>' type="text" id='qty<? echo "_".$c; ?>' size="3"></td>


	</tr>
	<? $c++;
	$_SESSION['bulkcount']=$c;
	} ?>
	</form>

 

So,

 

1) you have a while....loop

2) you increment $c each time the loop goes

3) So you end up with arrays of data e.g. POST['cart_pkg_1'], value1, POST['cart_pkg_2'], value 2 etc.

4) Then in the next page, once submitted (in this case to cart.php), you scroll through values of $c again to filter out the data from POST['cart_pkg_1'] etc

 

The only thing I haven't worked out is whether you need to know which submit button was pressed - presumably you don't need to know if you have gathered all of the data as the example above does.

 

In the example above, there is only one submit button, so you wouldn't know which button was pressed. If you did for some reason need this, you could include a submit on each row, and name it using $c again, so each submit button had a different name/id.

 

Hope this helps!

Emma

Link to comment
Share on other sites

I'm going to try to make this more generic before i head to bed... this is written very roughly, but you get my drift

 

you display the information presumably by using a while loop.

 

while (there are rows) {

echo "<tr><td>"value from DB"</td></tr>

}

 

So you want to be able to do something with the info once they click, so you add a form

 

<form action=test.php method=post

 

while (there are rows) {

echo "<tr><td><input type='hidden' id =field1_".echo $count." value=".echo ValueFromDB."></td></tr>

$count++

}

<input type=submit id=submit name=submit>

 

</form>

 

Sorry if i've misunderstood what you're after. But since I've been working on something similar all day...

 

Emma

Link to comment
Share on other sites

uh yeah, if you mean like multi dynamic forms, a pretty easy concept, I never knew if there was an official way to approaching them, but this is what I always did.

 

 

 

// if you want a very random amount of forms, $i < rand(10,99999); .. lol

<?PHP
if(isset($_POST['submit'])){

for($i = 0;$i< count($_POST);$i++){
if(isset($_POST["guhh".$i])){

// handle it this since its set.

}

}
} else {

echo "<form method='post'>";
for($i = 0; $i<10; $i++){
echo "<input type='text' value='blabla' name='guhh".$i."' /> Option";

}
echo "<input type='submit' name='submit' value='Submit' />";
echo "</form>";

}
?>

Link to comment
Share on other sites

As I read my question, I wasn't very clear with what I'm doing. Here is an example of the code->

 

<?
	 if (isset($_POST['updatestatus']))
   {
      $item= $_POST['item'];
  
  $query = "query stuff'";

      $result = mysql_query($query);
  }
?>


<form action="webpage.php" enctype="multipart/form-data" method="post">
<input type="hidden" name="item1" value="1" />
<input type="submit" name="updatestatus" value="Update Status for Item 1" />
</form>

<form action="webpage.php" enctype="multipart/form-data" method="post">
<input type="hidden" name="item2" value="1" />
<input type="submit" name="updatestatus" value="Update Status for Item 2" />
</form>

<form action="webpage.php" enctype="multipart/form-data" method="post">
<input type="hidden" name="item3" value="1" />
<input type="submit" name="updatestatus" value="Update Status for Item 3" />
</form>

<form action="webpage.php" enctype="multipart/form-data" method="post">
<input type="hidden" name="item4" value="1" />
<input type="submit" name="updatestatus" value="Update Status for Item 4" />
</form>

 

I left the name "updatestatus" the same for all of them, because by changing it, I have no idea how to write the code that checks to see if the form was submitted. There can be anywhere from 1-20 forms just like this on the page.

Link to comment
Share on other sites

I don't actually get what you want to do... But maybe this helps a bit:

 

<?php
$number = $_POST['num'];
$item   = $_POST['item' . $number];
?>

 

But I don't see much sense in it all... If you don't want that there are equal names in your code, you can also use a foreach on $_POST...

 

<?php
foreach($_POST as $key => $post) {

}
?>

 

Or if you want to check if there is anything posted at all you can use: if ($_SERVER['REQUEST_METHOD'] == 'POST') {}

 

Hope this is usefull

 

Grtz

Link to comment
Share on other sites

i forget to copy rest of code. sorry

<?
	 if (isset($_POST['updatestatus']))
   {
      $item= $_POST['item'];
      $num = $_POST['num'];
  echo "You clik on form No $num.<br />\n";
  echo "Item_$num value is $item. <br />\n";
  $query = "query stuff'";

      $result = mysql_query($query);
  }
?>


<form action="webpage.php" enctype="multipart/form-data" method="post">
<input type="hidden" name="item" value="1" />
<input type="hidden" name="num" value="1" />
<input type="submit" name="updatestatus" value="Update Status for Item 1" />
</form>

<form action="webpage.php" enctype="multipart/form-data" method="post">
<input type="hidden" name="item" value="1" />
<input type="hidden" name="num" value="2" />
<input type="submit" name="updatestatus" value="Update Status for Item 2" />
</form>

<form action="webpage.php" enctype="multipart/form-data" method="post">
<input type="hidden" name="item" value="1" />
<input type="hidden" name="num" value="3" />
<input type="submit" name="updatestatus" value="Update Status for Item 3" />
</form>

<form action="webpage.php" enctype="multipart/form-data" method="post">
<input type="hidden" name="item" value="1" />
<input type="hidden" name="num" value="4" />
<input type="submit" name="updatestatus" value="Update Status for Item 4" />
</form>

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.