The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   Networking (http://hintsforums.macworld.com/forumdisplay.php?f=14)
-   -   PHP Button input (http://hintsforums.macworld.com/showthread.php?t=92755)

cwtnospam 08-08-2008 04:12 PM

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:

<html>
<head>
<title>In/Out White Board</title>
</head>
<body>
<left><b>
<?php 
mysql_connect
("127.0.0.1""root""password") or die(mysql_error());
mysql_select_db("test") or die(mysql_error()) or die("A MySQL error has occurred.<br />Your Query: " $your_query "<br /> Error: (" mysql_errno() . ") " mysql_error());
$data mysql_query("SELECT * FROM Employees") or die(mysql_error());
Print 
"<table border cellpadding=3>"
while(
$info mysql_fetch_array$data )) 
{
Print 
"<tr>";
$theid $info ['id'];
$inout $info ['inn'];
if (
$inout==1) {
$ch1="CHECKED";
$ch2="";
} else {
$ch2="CHECKED";
$ch1="";
}
Print 
"<th><b>ID: </b><td>" .$theid" </td>";
Print 
"<th><b>Name: </b><td>" .$info'Name' ] . " </td>";
?>
<td><input type="radio", name=<?php echo $theid ?>, "<?php echo $ch1 ?>" value=1>
<?php 
Print "<th><b>In / Out</b><td>";
?>
<td><input type="radio", name=<?php echo $theid ?>, "<?php echo $ch2 ?>" value=0>
<?php 
Print "<br>";
if (
$r == "") {
$value=0;
} else {
$value=0;
}
Print 
$value;
mysql_query("UPDATE Employees SET inn = $value WHERE id = $theid") or die("A MySQL error has occurred.<br />Your Query: " $your_query "<br /> Error: (" mysql_errno() . ") " mysql_error());
}
?>
</b>
</body>
</html>


baf 08-09-2008 11:02 AM

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.

cwtnospam 08-09-2008 12:51 PM

Thanks.
Will the form wrap the 'while' loop or go inside it?

baf 08-09-2008 01:16 PM

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();" />

cwtnospam 08-09-2008 01:40 PM

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.

baf 08-09-2008 01:53 PM

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

cwtnospam 08-09-2008 02:41 PM

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!

baf 08-09-2008 03:01 PM

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.

cwtnospam 08-09-2008 03:18 PM

One at a time. I want the state of the buttons to always reflect the associated values in the database.

baf 08-09-2008 03:54 PM

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

cwtnospam 08-09-2008 04:23 PM

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?

baf 08-09-2008 04:28 PM

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.

cwtnospam 08-09-2008 04:37 PM

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

baf 08-09-2008 04:51 PM

I see.
Even better way:
Code:

<form method="post" action="http://127.0.0.1/~baf/index.php/some_id1" >
johnny
<input type="radio" name="state" checked="1" value="0">in</input>
<input type="radio" name="state" value="1" onclick="this.form.submit();" >out</input>
</form>
<form method="post" action="http://127.0.0.1/~baf/index.php/some_id2" >
betty
<input type="radio" name="state" checked="1" value="0">in</input>
<input type="radio" name="state" value="1" onclick="this.form.submit();" >out</input>
</form>

Then _SERVER["PATH_INFO"] will contain something like /some_id2
And of course you would generate all this with php.

cwtnospam 08-09-2008 04:59 PM

Ok, I think I see what you mean, but I'll have to wait until tomorrow when I can try it.

Thanks!

baf 08-09-2008 05:22 PM

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.

acme.mail.order 08-09-2008 10:47 PM

Quote:

Originally Posted by cwtnospam (Post 487417)
One at a time. I want the state of the buttons to always reflect the associated values in the database.

Then radio buttons are not the way to go, unless you're going to do tons of Ajax. You need javascript to submit the form, and javascript isn't always available. You can, however, have unlimited submit buttons with the server-side script acting on the submit button name/value.

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.

cwtnospam 08-10-2008 02:28 PM

Quote:

Originally Posted by acme.mail.order (Post 487465)
printf('<input type="radio" name="%s" %s value=1>, $theid, ($inout ? 'checked' : '') );

That looks good, and I'll try it when I get back later, but what's getting passed to the database, and do I have to call a separate php file? I'd like to do it all in one shot, with the link to this page being a php file. Maybe I'm just confusing myself because I can't try these things out yet.

cwtnospam 08-10-2008 07:51 PM

I'm not getting something about posting methods. I can't get the buttons or the database to change.

PHP Code:

<html>
<head>
<title>In/Out White Board</title>
</head>
<body>
<left><b>
<?php 
$mystate 
$_POST['state'];
echo 
$mystate;
if (isset(
$_POST['state'])) { // if page is submitted to itself, change the data.
$value $mystate;
echo 
"++";
echo 
$value;
echo 
"+++";
mysql_query("UPDATE Employees SET inn = $value WHERE id = $theid");// or die("A MySQL error has occurred.<br />Your Query: " . $your_query . "<br /> Error: (" . mysql_errno() . ") " . mysql_error()); 
}
mysql_connect("127.0.0.1""root""password"); //or die(mysql_error());
mysql_select_db("test"); //or die(mysql_error()) or die("A MySQL error has occurred.<br />Your Query: " . $your_query . "<br /> Error: (" . mysql_errno() . ") " . mysql_error());
$data mysql_query("SELECT * FROM Employees") or die(mysql_error());

Print 
"<table border cellpadding=3>"
while(
$info mysql_fetch_array$data )) 
{
Print 
"<th>" .$info'Name' ]."</td> <th>".$theid."</td>" 
$value $info'inn' ];
if (
$value == 1) {
$ch1 "checked";
$ch2 '';
} else {
$ch1 '';
$ch2 "checked";
}
?>
<form method="post" action="<?php echo $PHP_SELF ?>" > 
<input type="radio" name="state" value="1" "<?php echo $ch1 ?>" onclick="this.form.submit();">in</input> 
<input type="radio" name="state" value="0" "<?php echo $ch2 ?>" onclick="this.form.submit();" >out</td></input> 
</form>
<?php
Print "</td></th>";

?>
</b>
</body>
</html>

Edit: I'm getting closer though. ;)

cwtnospam 08-11-2008 09:41 AM

I've got it working, but there are two minor problems.

One is that I'd like the table to format better. I'm getting this:

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:

<html>
<head>
<title>In/Out White Board</title>
</head>
<body>
<left><b>
<?php
$mystate 
$_POST['state'];
$length=strlen($mystate);
if (!
$length=="0"){
$theid substr($mystate,1,1);
$value substr($mystate,0,1);
mysql_connect("127.0.0.1""root""password") or die(mysql_error());
mysql_select_db("test") or die(mysql_error()) or die("A MySQL error has occurred.<br />Your Query: " $your_query "<br /> Error: (" mysql_errno() . ") " mysql_error());
mysql_query("UPDATE Employees SET inn = $value WHERE id = $theid") or die("A MySQL error has occurred.<br />Your Query: " $your_query "<br /> Error: (" mysql_errno() . ") " mysql_error()); 
} else {
mysql_connect("127.0.0.1""root""password") or die(mysql_error());
mysql_select_db("test") or die(mysql_error()) or die("A MySQL error has occurred.<br />Your Query: " $your_query "<br /> Error: (" mysql_errno() . ") " mysql_error());
}
$data mysql_query("SELECT * FROM Employees") or die(mysql_error());
Print 
"<table border cellpadding=3>"
while(
$info mysql_fetch_array$data )) 
{
$value $info'inn' ];
$theid $info'id' ];
Print 
"<th>".$info'Name' ]."</td> <th></td>" 
if (
$value == 1) {
$ch1 "checked";
$ch2 '';
} else {
$ch1 '';
$ch2 "checked";
}
$v0 "0".$theid;
$v1 ="1".$theid;
?>
<form method="post" action="inout.php" > 
<input type="radio" name="state" value="<?php echo $v1 ?>" "<?php echo $ch1 ?>" onclick="this.form.submit();">in</input> 
<input type="radio" name="state" value="<?php echo $v0 ?>" "<?php echo $ch2 ?>" onclick="this.form.submit();" >out</td></input>
</form>
<?php
Print "<br>";

?>
</b>
</body>
</html>


cwtnospam 08-11-2008 10:16 AM

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.

cwtnospam 08-11-2008 10:50 AM

Ok, this seems to work with FF, Camino & Safari. It even works with IE on Windows:

PHP Code:

<body>
<left><b>
<?php
$mystate 
$_POST['state'];
if (!
$mystate==""){
$theid substr($mystate,1);
$value substr($mystate,0,1);
mysql_connect("127.0.0.1""root""password") or die(mysql_error());
mysql_select_db("test") or die(mysql_error()) or die("A MySQL error has occurred.<br />Your Query: " $your_query "<br /> Error: (" mysql_errno() . ") " mysql_error());
mysql_query("UPDATE Employees SET inn = $value WHERE id = $theid") or die("A MySQL error has occurred.<br />Your Query: " $your_query "<br /> Error: (" mysql_errno() . ") " mysql_error()); 
} else {
mysql_connect("127.0.0.1""root""byteme") or die(mysql_error());
mysql_select_db("test") or die(mysql_error()) or die("A MySQL error has occurred.<br />Your Query: " $your_query "<br /> Error: (" mysql_errno() . ") " mysql_error());
}
$data mysql_query("SELECT * FROM Employees") or die(mysql_error());
?>
<table border cellpadding=3>
<?php
while($info mysql_fetch_array$data )) 
{
$value $info'inn' ];
$theid $info'id' ];
$v0 "0".$theid;
$v1 ="1".$theid;
Print 
'<th><align="left">'.$info'Name' ]."</td> <th></td>" 
if (
$value == 1) {

?>
<form method="post" action="inout.php" > 
<input type="radio" name="state" value="<?php echo $v1 ?>" CHECKED="CHECKED" onclick="this.form.submit();">in</input> 
<input type="radio" name="state" value="<?php echo $v0 ?>" onclick="this.form.submit();" >out</td><tr></input>
</form>
<?php

} else {

?>
<form method="post" action="inout.php" > 
<input type="radio" name="state" value="<?php echo $v1 ?>" onclick="this.form.submit();">in</input> 
<input type="radio" name="state" value="<?php echo $v0 ?>" CHECKED="CHECKED" onclick="this.form.submit();" >out</td><tr></input>
</form>
<?php
}

?>
</b>
</body>


baf 08-11-2008 12:23 PM

Possibly worth looking at frames? Then you could have several "sub pages".

cwtnospam 08-11-2008 02:37 PM

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.