Jump to content

Submitting just on <input> from column of inputs


ChenXiu
 Share

Recommended Posts

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.

Link to comment
Share on other sites

Posted (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 by mac_gyver
Link to comment
Share on other sites

Posted (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 by ChenXiu
Link to comment
Share on other sites

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.

 Share

×
×
  • 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.