Page 1 of 1

submitting multi keys from table rows

Posted: Tue Sep 02, 2014 5:58 am
by bowlesj
Hi, I am doing my first website after beginning my studies of HTML/PHP/Javascript/CSS/MySql in June 2014. The adminisrative stuff is done and tested (Specifically: registration, login, user forgot password processing, profile update). Okay so now the fun stuff (65 more pages of duplicating my MS-Access prototype database into this new WebSite technology). I think I know how to do this next challenge but thought I should run it by the PHP/Javascript experts.

The objective:
Produce a table of MySql data on the page. Each row is a record on the database and has the key hidden (I know how to do all this so far). The user can checkbox the rows they want to have added to their personal database, and after they have checked all the rows they want, they click submit and the record keys are added to their personal selection.

My theory as to how to do this:
I know how to select one key and go off to get another page. However to collect the keys for final submission is the new part. I am thinking I will need a hidden form at the top with a text field and I will append each key the user checks into that hidden text field and when they click submit the next whatever.php file will parse out the keys from this text box and add these to the user records. Assuming I am on the correct track so far I am thinking each row needs a button with an "onclick" event which calls a javascript function. Okay so I need to figure out how to get the record key of that row to the function much like the example I have from a book of getting the record key of the row into the hyperlink address. I remember now. The PHP code used a loop to build each table row and there was a variable called $RowsKey that was put in a hidden field and I could put this same $RowKey in the javascript function call. Once I get this far I know how to use the Javascript to append the key to the text box with a comma to separate the keys.

What what do you think? Is this the way to do it?

Thanks,
John

Re: submitting multi keys from table rows

Posted: Tue Sep 02, 2014 6:38 am
by Celauran
You don't need Javascript for this at all. Put the table inside a form and have the checkboxes values use an array. Rather than

Code: Select all

<input name="foo" type="checkbox" value="123">
as you would normally do, foo becomes an array

Code: Select all

<input name="foo[]" type="checkbox" value="123">
On form submission, you can now iterate over $_POST['foo'] to get the selected IDs and add those to your pivot table.

Re: submitting multi keys from table rows

Posted: Tue Sep 02, 2014 6:54 am
by bowlesj
Thanks Celauran, thats great. I am pretty sure I understand how to do what you have suggested.

I need to review the array syntax (link below - W3schools is my favorite source of info). I noticed you don't have an index within the [] brackets. I will figure it out.
http://www.w3schools.com/php/func_array.asp

John

Re: submitting multi keys from table rows

Posted: Wed Sep 03, 2014 10:37 am
by bowlesj
I got it to work as proven by the proper display of the key value from selected rows.
Studying the http://www.w3schools.com/php/php_arrays.asp page did not help much.
I got the answer from this Google search "php obtain $_Post array checkbox"
Here is the successful test for anyone reading this.

Code: Select all

   if (isset($_POST['foo'])) {
      foreach($_POST['foo'] as $value){
         echo "$value  <br>";
      }
   }
The example found with the google search is a bit more elaborate than I needed.
http://stackoverflow.com/questions/1065 ... -from-post

Thanks again.

Re: submitting multi keys from table rows

Posted: Wed Sep 03, 2014 10:40 am
by Celauran
bowlesj wrote:Studying the http://www.w3schools.com/php/php_arrays.asp page did not help much.
Terrible, terrible resource. Forget it even exists. It will leave you mired in worst practices.

Re: submitting multi keys from table rows

Posted: Wed Sep 03, 2014 10:42 am
by Celauran
bowlesj wrote:

Code: Select all

   if (isset($_POST['foo'])) {
      foreach($_POST['foo'] as $value){
         echo "$value  <br>";
      }
   }
Note also, if you aren't already aware, that you can also iterate over key/value pairs like so

Code: Select all

foreach ($array_name as $key => $value) {
    // stuff
}

Re: submitting multi keys from table rows

Posted: Wed Sep 03, 2014 10:54 am
by bowlesj
Thanks Celauran.

Re: submitting multi keys from table rows

Posted: Wed Sep 03, 2014 11:14 am
by bowlesj
I find it interesting that it only loads the array if I check the checkbox as true and leave it checked as true (unchecking it seems to clear the array). I was thinking that I would have to test the value of the "record key" coming back to figure out if the checkbox was checked or not. So that is a bit of good news.