I need help with form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vigour
Forum Newbie
Posts: 18
Joined: Thu Sep 29, 2005 2:04 am

I need help with form

Post by vigour »

I need a dynamically built form that have several rows that looks the same.

1 - Each of the rows should have a buy button that takes me to another php document.

2 - Each row should have a textfield with quantity.

When I click on the buy botton I want to read the value in the textfield for the row I'm clicking on.

I have no problems building the form, but I can't read anything from the textfield so I think I'm doing this completely wrong.

Can someone please post some code for me and explain how to do this?
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

Code: Select all

$result = mysql_query("SELECT * FROM table"); // add limitations if you want

echo '<table width="100%" align="center"><form method="post" action="pagetopostto">';
echo '<tr>
             <td width="33%">
              What to buy:
              </td>
              <td width="33%">
              How many to buy:
              </td>
              <td width="33%">
              SUBMIT
              </td>
           </tr>';
while($row = mysql_fetch_assoc($result)) { // goes through the results
echo '<tr>
              <td width="33%">
              ' . $row['name'] . '<input type="hidden" name="whattobuy" value="' . $row['name'] . '" />
              </td>
              <td width="33%">
              <input type="text" name="quantity" />
              </td>
              <td width="33%">
              <input type="submit" name="submit" value="Buy" />
              </td>
         </td>';
}
echo '</table></form>';
something like that?
ruchit
Forum Commoner
Posts: 53
Joined: Mon Sep 26, 2005 6:03 am

Post by ruchit »

its always better to post your own code.. then ask for corrections rather than script requests...
i guess its one of your lucky days

Code: Select all

$counter=1;
$maxRows=5;
echo '<table width="100%">';
while($counter <= $maxRows){
  echo "<form name=\"frm{$counter}\" action=\"secondfile.php\" method=\"post\">";
  echo '<tr><td><input type="text" name="text" value=""></td>
                   <td><input type="submit" name="submit" value="Buy"></td></tr>';
  echo '</form>';
}
echo '</table>';
vigour
Forum Newbie
Posts: 18
Joined: Thu Sep 29, 2005 2:04 am

Post by vigour »

Yes this works, thanks. Is it possible to have a .jpg picture instead of the standard submit button?
mickd wrote:

Code: Select all

$result = mysql_query("SELECT * FROM table"); // add limitations if you want

echo '<table width="100%" align="center"><form method="post" action="pagetopostto">';
echo '<tr>
             <td width="33%">
              What to buy:
              </td>
              <td width="33%">
              How many to buy:
              </td>
              <td width="33%">
              SUBMIT
              </td>
           </tr>';
while($row = mysql_fetch_assoc($result)) { // goes through the results
echo '<tr>
              <td width="33%">
              ' . $row['name'] . '<input type="hidden" name="whattobuy" value="' . $row['name'] . '" />
              </td>
              <td width="33%">
              <input type="text" name="quantity" />
              </td>
              <td width="33%">
              <input type="submit" name="submit" value="Buy" />
              </td>
         </td>';
}
echo '</table></form>';
something like that?
vigour
Forum Newbie
Posts: 18
Joined: Thu Sep 29, 2005 2:04 am

Post by vigour »

Yes it's my lucky day :) Thanks. And try to increase the counter next time :)

Is it possible to use a .jpg picture instead of the standard submit button?
ruchit wrote:its always better to post your own code.. then ask for corrections rather than script requests...
i guess its one of your lucky days

Code: Select all

$counter=1;
$maxRows=5;
echo '<table width="100%">';
while($counter <= $maxRows){
  echo "<form name="frm{$counter}" action="secondfile.php" method="post">";
  echo '<tr><td><input type="text" name="text" value=""></td>
                   <td><input type="submit" name="submit" value="Buy"></td></tr>';
  echo '</form>';
}
echo '</table>';
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

yes it is. use

Code: Select all

<input type="image" name="imagename" src="imagepath" alt="ifpiccantloadwhattext">
btw when using that and your refering to it in the posted page in PHP, you gotta add _x or _y at the end for example

Code: Select all

if(isset($_POST['imagename_x'])) {
echo 'this will work';
}

if(isset($_POST['imagename'])) {
echo 'this WONT work';
}
its because when the form is sent it sends imagename.x and imagename.y as the x and y length. then since you cant have . in the name, it gets auto changed to _.
vigour
Forum Newbie
Posts: 18
Joined: Thu Sep 29, 2005 2:04 am

Post by vigour »

Thanks, that works. How do I know what row I clicked on?
mickd wrote:yes it is. use

Code: Select all

<input type="image" name="imagename" src="imagepath" alt="ifpiccantloadwhattext">
btw when using that and your refering to it in the posted page in PHP, you gotta add _x or _y at the end for example

Code: Select all

if(isset($_POST['imagename_x'])) {
echo 'this will work';
}

if(isset($_POST['imagename'])) {
echo 'this WONT work';
}
its because when the form is sent it sends imagename.x and imagename.y as the x and y length. then since you cant have . in the name, it gets auto changed to _.
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

Why do that if you could just set a background-image using CSS to the submit button?
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

replace

Code: Select all

<input type="submit" name="submit" value="Buy" />
with

Code: Select all

<input type="image" name="imagename" src="imagepath" alt="ifpiccantloadwhattext" />
use the hidden field to tell the script when its posted to know what to buy.

could use style to add the background image to the submit too if you want.

add

Code: Select all

style="background-image: path/to/file"
into the input tag

Code: Select all

<input type="submit" name="submit" value="Buy" style="background-image: path/to/file.jpg" />
Post Reply