![]() |
PHP Button input
Do I need to use forms to get clicked buttons? This thread got me thinking that it should be easy enough to create a web page to create a whiteboard, so I set up a mysql database and wrote the code below. As you can see, I'm having trouble getting the clicked buttons so that I can write back to the database.
If I use forms, will I be able to list all items in the database with buttons simultaneously? PHP Code:
|
Yes you need a form to be able to use radiobuttons,buttons and some more things.
You can still build them from your database. A form is just a wrapper which tells how and where the query gets sent. |
Thanks.
Will the form wrap the 'while' loop or go inside it? |
If you want to be able to change several in one go then it has to be outside the while.
If you just want one at a time to be changed then it could go inside the loop, but then you would have to make it look like this: name1 radio1 radio2 submit_butt name2 radio1 radio2 submit_butt ... if the form goes around the while then you'll only need one submit button. Or if you want them to update directly on radio_click and you know that their browser runs javascript (not tested) Code:
<input type="radio" name="myradio" value="1" onclick="this.form.submit();" /> |
Thanks.
I want to display an unknown quantity of items from a mysql table with two buttons (on/off) for each, so I guess I'll need the javascript too. |
Well that depends.
You could have this page layout: submit button name1 radio_in radio_out name2 radio_in radio_out name3 radio_in radio_out ... submit button that could be form submit many_radios submit end_form or if it would span several pages you could put one submit per page. if that javascript thing works you could have this structure: form many_radios end_form and either get back to the same page with updated values or send them a new page that just states Hi xyz you're now In_or_Out ... |
All I really want to do is update the database depending on what button is selected. I'm able to select the appropriate button based on the database, now I want to go the other way!
|
Well you still haven't stated if you want to bo able to change one at a time. Or several at a go. If I know that I could give some more ideas.
|
One at a time. I want the state of the buttons to always reflect the associated values in the database.
|
Ok then I would suggest one form per radio button
I think that an empty action goes back to the same page but I'm not sure. <form method="post" action="http://127.0.0.1/~baf/index.php" > <input type="radio" name="in" checked="1" value="0" /> <input type="radio" name="out" value="1" onclick="this.form.submit();" /> <input type="hidden" name="employee" value="some_kind_of_id" /> </form> Your script must then check: $_POST['employee'] will contain the id that was set and $_POST[in] and/or $_POST[out] will then be set. use the one thats set and has value 1 |
The problem with that is that I don't know how many radio buttons there will be. What I was hoping to do is put something like this in the while loop:
<form method="post" action="http://127.0.0.1/~baf/index.php" > <input type="radio" name="<?php echo $theid.'in' ?>" checked="1" value="0" /> <input type="radio" name="<?php echo $theid.'out' ?>" value="1" onclick="this.form.submit();" /> <input type="hidden" name="employee" value="some_kind_of_id" /> </form> Unfortunately, I'm not at my computer and I can't install mysql on this one, so I won't be able to test anything until tomorrow. Edit: Oh, and don't the names need to be the same so that clicking 'in' will uncheck 'out' and vice versa? Also, shouldn't both contain a submit so that the database will be updated on every change? |
Yes but let let radio buttons be statically named and change only value="some_kind_of_id"
You can have many buttons with the same name so long as they are in different forms. And let only the "new state" button have onclick=... then you wont get the other one. |
Ok, I see what you mean about the names, but I'm not sure about the onclick. I think I do want to get both because any click changes the state:
Check: In -- changes the 'inn' value to 1 Check: Out -- changes the 'inn' value to 0 |
I see.
Even better way: Code:
<form method="post" action="http://127.0.0.1/~baf/index.php/some_id1" >And of course you would generate all this with php. |
Ok, I think I see what you mean, but I'll have to wait until tomorrow when I can try it.
Thanks! |
And a small debugging tip.
make a php-page like this. ( this is my index.php ) <?php phpinfo() ?> And point your script to that then you will see all form variables and such. That is what I did. Test by generating a html page by hand whose action= points to that page. |
Quote:
Also, the UPDATE query should go at the beginning of the script, not the end. Otherwise the page content will not reflect the current database state - it will be one step behind. And read about the printf/sprintf function - makes things like querys and complex escaped html much easier to follow: Print "<th><b>Name: </b><td>" .$info[ 'Name' ] . " </td>"; turns into printf("<th><b>Name: </b><td>%s</td>\n",$info['Name']); combined with the ternary operator (PHP manual -> Control Structures) saves you calculating the value separately printf('<input type="radio" name="%s" %s value=1>', $theid, ($inout ? 'checked' : '') ); Doesn't look much different now, but imagine something with 6 different attributes taking different argument styles. And note that there are no commas in html attribute lists. |
Quote:
|
I'm not getting something about posting methods. I can't get the buttons or the database to change.
PHP Code:
|
I've got it working, but there are two minor problems.
Name1 In button Out button Name2 In button Out button Name3 In button Out button... I'd prefer to have: Name1 In button Out button Name2 In button Out button Name3 In button Out button Edit: Never mind! <tr> The second is that the entire page must refresh to accomplish what I'm doing. I don't think there's a way around this, but if there is I'd like to know! PHP Code:
|
2 Attachment(s)
Arrgh! I thought I had it working, but it doesn't work with Safari. :mad:
Safari won't check the buttons. Any ideas? See images from Firefox and Safari below. |
Ok, this seems to work with FF, Camino & Safari. It even works with IE on Windows:
PHP Code:
|
Possibly worth looking at frames? Then you could have several "sub pages".
|
Maybe. On the other hand, I want it to easily refresh the list since some other computer may have updated the information.
|
| All times are GMT -5. The time now is 09:41 PM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.