ChenXiu Posted July 2, 2021 Share Posted July 2, 2021 My PHP code for mySQL query result: echo '<input type="text" value="' . $row [ "dollar_amount" ] . '" onInput = "submit();" >'; This yields a neat HTML column of retrieved prices on my PHP page that looks kinda like this <input type="text" value="19.99" onInput="submit();"> <input type="text" value="7.55" onInput="submit();"> <input type="text" value="10.00" onInput="submit();"> <input type="text" value="4.99" onInput="submit();"> <input type="text" value="12.75" onInput="submit();"> Desired Effect: I want to change the "10.00" amount to "20.00" simply by typing and entering "20.00" where it says "10.00" and have the form submit and update that specific mySQL record. Question: What's the best way to capture that specific post variable? Problems I've Encountered: 1. The page has several inputs and submit buttons, thus all kinds of different $_POST variables exist. 2.) I could add "name = $unique_order_number" to each input field, but when I submit the form, I still have to have PHP loop through everything. 3.) I could add "name" and have it match "value" (e.g. <input name="$row['dollar_amount']" value="$row['dollar_amount']") It seems the only way to do this is to have PHP loop through EVERY post variable every time anything is posted and this seems inefficient. Example: (to make this inefficient example work, I would have to append "updatedollar" to the name, like "updatedollar".$row[dollar_amount'] ) foreach($_POST as $var = $val) { // loop through all the posts on the page if(substr($var,0,12) == 'updatedollar') { // make sure it's an "updatedollar" post and not one of the other posts on the page. if(substr($var,12) != $val) { // get rid of "updatedollar" part and see if it's the value I wanted changed // do a mysql update query and update the table } } } So, there has to be a better way? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/313029-submitting-just-on-from-column-of-inputs/ Share on other sites More sharing options...
mac_gyver Posted July 2, 2021 Share Posted July 2, 2021 (edited) these initial values must be related to something (an id?), that you would then use when updating the correct row(s) in the database table. you would use an array name for the form field, with the array index being the id. you would then loop over that array name in the post data, getting the index (id) and the submitted value to use in the update query. note: onInput is probably not a good choice to use to submit the data, since every keypress that alters what's in the field will cause a form submission. for your example of changing a 10.00 to 20.00, you can move the cursor to the '1' position, but deleting it will cause your submit function to be executed, then typing the 2 will cause the submit function to be executed again. Edited July 2, 2021 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/313029-submitting-just-on-from-column-of-inputs/#findComment-1587753 Share on other sites More sharing options...
ChenXiu Posted July 2, 2021 Author Share Posted July 2, 2021 (edited) 1 hour ago, mac_gyver said: these initial values must be related to something (an id?) Absolutely, after I posted the question I realized that was the key piece of the puzzle ( <input type="text" name="2021-07-02ABC" value="44.99"> ). I have heard of what you are suggesting... but I've never done that before :-) This would probably be the HTML for PHP to echo out, right? echo '<input type="text" name="orderid['.$row['order_id'].'" value="'.$row['dollar_amount'].'">'; ... and then using foreach($_POST["orderid"] as $var => $val) would narrow down and limit the loop to just the "order_id" posts. Now I have to figure out how to capture the exact <input> that I changed, and insert that into mySQL. Edited July 2, 2021 by ChenXiu Quote Link to comment https://forums.phpfreaks.com/topic/313029-submitting-just-on-from-column-of-inputs/#findComment-1587756 Share on other sites More sharing options...
ChenXiu Posted July 3, 2021 Author Share Posted July 3, 2021 I can't make it work. I'm trying to capture the $_POST value of the specific input that I've changed. But they all get posted (they all isset = true). Quote Link to comment https://forums.phpfreaks.com/topic/313029-submitting-just-on-from-column-of-inputs/#findComment-1587782 Share on other sites More sharing options...
ChenXiu Posted July 3, 2021 Author Share Posted July 3, 2021 Nevermind. I figured it out. Quote Link to comment https://forums.phpfreaks.com/topic/313029-submitting-just-on-from-column-of-inputs/#findComment-1587783 Share on other sites More sharing options...
Barand Posted July 3, 2021 Share Posted July 3, 2021 For the sake of having a solution to this thread, put each in its own form. form input /form form input /form ... Quote Link to comment https://forums.phpfreaks.com/topic/313029-submitting-just-on-from-column-of-inputs/#findComment-1587789 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.