Submit Button Click

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
ms.Jemi
Forum Newbie
Posts: 6
Joined: Thu Mar 03, 2011 1:50 am

Submit Button Click

Post by ms.Jemi »

Hi everyone!
I have a problem in submit button.
I have dynamic input fields and submit button.
Each input Fields has a submit button.

So, when I click the first button, I want to get the value of its corresponding input field.
Can you guys, help me out?...please...
Here is the sample code for generating input fields and submit buttons

<form name="form1" method="post" action="practice1.php">
<?php
for($x=0;$x<3;$x++){
echo '<input name="txtVal[]" type="text">';
echo '<input name="sub[]" value="Add" type="Submit">';
echo "<br>";
}

?>
Guys..please I really need ur ideas....Thank You
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Submit Button Click

Post by social_experiment »

You'd probably have to generate a complete form for each button. You can only have 1 submit button per form because you only have 1 action per form.

Code: Select all

<?php
for($x=0;$x<3;$x++){
echo '<form name="form ' . $x .'" method="post" action="practice1.php" >';
echo '<input name="txtVal[]" type="text">';
echo '<input name="sub[]" value="Add" type="Submit">';
echo '</form>
echo "<br>";
}
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
ms.Jemi
Forum Newbie
Posts: 6
Joined: Thu Mar 03, 2011 1:50 am

Re: Submit Button Click

Post by ms.Jemi »

thank you for the reply.....

I cant get the value
echo $_POST["txtVal"];
is this the only in the other page?
Iswald
Forum Newbie
Posts: 1
Joined: Thu Mar 03, 2011 3:43 pm

Re: Submit Button Click

Post by Iswald »

ms.Jemi wrote:Hi everyone!

Code: Select all

<form name="form1" method="post" action="practice1.php">
<?php
for($x=0;$x<3;$x++){
echo '<input name="txtVal[]" type="text">';
echo '<input name="sub[]" value="Add" type="Submit">';
echo "<br>";
}
?>
One way to do this, is to give all of your submit buttons the same name:

Code: Select all

echo '<input name="math" value="Add" type="Submit">';
echo '<input name="math" value="Subtract" type="Submit">';
In your form processing code, do a switch based on the value of the submit button's name:

Code: Select all

switch ($_POST['math']) {
  case "Add":
   // do some adding
    break;
  case "Subtract":
   // do some subracting
    break;
  default:
  // whatever happens when things break
    break;
}
I'd thought I'd misplaced the link I got this idea from (needing to do the same thing), but here it is: http://www.techrepublic.com/article/han ... hp/5242116

Unless I'm misunderstanding the question, which I might be.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Submit Button Click

Post by social_experiment »

My bad. I thought html conventions only allowed for 1 submit button per form. Here is another URL you can look at
Multiple+submit+buttons+in+one+form
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply