/    Sign up×
Community /Pin to ProfileBookmark

Identify submitted button

To Genius out there,

I know there were few thread on this topic in this forum, but mine is slightly twisted ?.

I have this script where it gets records from database and displays in a table format with each row has its own submit button.

sample code:

[code=php]
$num=mysql_num_rows($sqlresult);
for ($j=1,$j<=$num;$j++){
bla bla bla….
print “<tr><td><input type=”submit” name=”submit$j” value=”update”></td></tr>”;
}
[/code]

this code will only show records of 6 rows max (average of 4 rows). At anyone time there will be 4 – 6 submit buttons.

My question is how to identify which submit button is clicked? because the code behind each submit button is to update the record of that particular row instead of updating all the 4-6 rows displayed where all of the records has its own ID field in the table

I would appreciate if anyone could point me in the right direction to solve this or if there is any other method of doing this.

Thanks in advance

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@ryanxcomauthorJan 26.2012 — Or perhaps if I were to do the above with one submit button, then how do I update the database only on the records that were modified?
Copy linkTweet thisAlerts:
@OctoberWindJan 26.2012 — I would assign an array of radio buttons, from which your process script can determine which set of data to manipulate.
Copy linkTweet thisAlerts:
@WolfShadeJan 26.2012 — I've had something similar to this while on my last contract. Basically, I gave each text field a hidden field that contains the exact same value (same name, too, except I appended "_orig" to the end of the name of the hidden field.) When the single submit button is clicked, the original value is compared to the text field value; if they match, you don't need to process that record; if they don't match, that record needs to be updated. I would build the SQL statement as it looped through and put it into a string, then queried the string.
Copy linkTweet thisAlerts:
@OctoberWindJan 26.2012 — I've had something similar to this while on my last contract. Basically, I gave each text field a hidden field that contains the exact same value (same name, too, except I appended "_orig" to the end of the name of the hidden field.) When the single submit button is clicked, the original value is compared to the text field value; if they match, you don't need to process that record; if they don't match, that record needs to be updated. I would build the SQL statement as it looped through and put it into a string, then queried the string.[/QUOTE]

That sounds like a lot of unnecessary work, to create and populate an additional input for every field. It sounds like the OP just needs to update one of n number of rows. What happens if two or more rows are modified, even if they are not supposed to be updated (user error)?


OP, what is the data tat is being returned from the db, and what. Of it is bein change that needs to be updated? Text? Drop downs?
Copy linkTweet thisAlerts:
@WolfShadeJan 26.2012 — If the form is being created on the fly from records retrieved by a query, it's very little "extra work". The page that processes the data and creates the query is also dynamically generated by looping through the form elements.
Copy linkTweet thisAlerts:
@007JulienJan 26.2012 — Inserting the ID in the name of the submit button will perhaps be a solution ?
Copy linkTweet thisAlerts:
@ssystemsJan 26.2012 — My first approach would be to just let client side do asynchronous post for each button. However, this is a matter of debate as to how many users got their js turned on. Which would give you another option on to separate each row to different fields. This would require little extra work on the html rendering though. You can also, as you already considered, is to have a single update button. With this you have to do comparison on the existing data versus whatever was changed which I don't really like. You also have an option of setting the id of each row's submit button then appending the id on each column of the row. Either way you're still submitting the entire form back and forth. So asynchronous post and handling is my best option.
Copy linkTweet thisAlerts:
@NogDogJan 26.2012 — You could use array notation for the submit button name, e.g. [b]name="submit[$j]"[/b]. Then to see which one was clicked:
[code=php]
reset($_POST['submit']);
$itemNbr = key($_POST['submit']);
echo "You clicked item # $itemNbr";
[/code]
×

Success!

Help @ryanxcom spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...