/    Sign up×
Community /Pin to ProfileBookmark

Checkbox – PHP/JS together?

I am displaying a table of data on a web page, populated by a mySQL DB. The first column is a checkbox which signifies the field Done – 0 is not done, 1 is done. I want the user to be able to toggle the checkbox without hitting a submit button, and automatically update the db. So, I believe that I need an onClick in the checkbox definition that will execute a JS script. But how then do I force a reload so I can update the DB?

<input name=”checkbox” type=”checkbox” onclick=”changeProp(‘checkbox’,”,’checked’,”,’INPUT/CHECKBOX’)” value=”checkbox” <?php if ($row_rsList[‘isDone’]) {echo “checked=”checked””;} ?> />

I tried something like this and then added window.location.reload() to the JS code, but it doesn’t work.

So what is the best way to do this seemingly simple task – click on the checkbox and immediately update the db?

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@SpectreReturnsDec 14.2005 — Go look up AJAX or SAJAX.
Copy linkTweet thisAlerts:
@winged1Dec 14.2005 — onChange="document.checkbox.submit()"
Copy linkTweet thisAlerts:
@glenngauthorDec 14.2005 — onChange="document.checkbox.submit()"[/QUOTE]

This too does not seem to work. Here is my code:

<input name="checkbox" type="checkbox" value="checkbox" onChange="document.checkbox.submit()" <?php if ($row_rsList['isDone']) {echo "checked="checked"";} ?> />

I want to click the checkbox, change its value and then reload the page so my PHP script can update the db.

The code above toggles the value on the screen but does not reload the page.
Copy linkTweet thisAlerts:
@bokehDec 14.2005 — I don't know what this is: [BI]onChange="document.checkbox.submit()"[/I] but I would use something like: [code=php]onchange="document.getElementById(this.form).submit();"[/code]By the way that wont work if the form has an element [I]name="submit"[/I] and the form needs an [I]ID[/I].
Copy linkTweet thisAlerts:
@glenngauthorDec 14.2005 — I guess this is not as easy as I thought! None of these suggestions seem to work. In each case I click the checkbox, it toggles the checkbox on the screen and then says "error in page" in the lower corner of the browser. The form does not reload. ???

There is no JS in the page and if I take out the onClick, there is no error message.
Copy linkTweet thisAlerts:
@kelly23Dec 14.2005 — What does the error message say? Did you add an id to the form as bokeh suggested? Is there a page online that can be viewed?
Copy linkTweet thisAlerts:
@bokehDec 14.2005 — Post the [I]changeProp()[/I] function and the [I]form[/I]. Post them within PHP code tags so they are easy to read. Also post any error message you recieve. If the error contains a line number mark your code on that line so we know to which line the erro refers.
Copy linkTweet thisAlerts:
@winged1Dec 14.2005 — wrap your input/Onclick in a form. POST the data and serve a new page, or do a javascript http request to maintain the page.
Copy linkTweet thisAlerts:
@glenngauthorDec 14.2005 — The code for the page is over 500 lines, so I have taken most of it out and left just the following to demonstrate the problem. Note that I have an onChange in the checkbox line. I have tried both options mentioned in this thread, and also used an onClick, but nothing forces the reload.

[code=php]<?php require_once('Connections/connTasks.php'); ?>
<?php

$query_rsList =
"select tasklist.isDone
, tasklist.userid
, tasklist.taskid
, tasklist.taskitem
, tasklist.taskpriority
, tasklist.taskduedate
, tasklist.tasknotes
from tasklist " ;

mysql_select_db($database_connTasks, $connTasks);
$rsList = mysql_query($query_rsList, $connTasks) or die(mysql_error());
$row_rsList = mysql_fetch_assoc($rsList);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>To Do List</title>
<link rel="stylesheet" type="text/css" media="screen" href="todo.css" />
<link rel="stylesheet" type="text/css" media="print" href="print.css" />
</head>



<div id="Content2">
<table id="itemtable" cellspacing="0" >
<?php do { ?>
<form class="form1" name="form1" method="post" action="" >

<tr class="firstrow">
<td class="colDone">
<input name="checkbox" type="checkbox" onchange="document.getElementById(this.form).submit();"
value="checkbox" <?php if ($row_rsList['isDone']) {echo "checked="checked"";} ?> /> </td>
<td class="colItem" nowrap ><?php echo $row_rsList['taskitem']; ?></td>
<td class='pcol' ><?php echo $row_rsList['taskpriority']; ?></td>
<td class='coldate' ><?php echo $row_rsList['taskduedate'] == NULL ? "None" :date("m-d-Y",strtotime($row_rsList['taskduedate'])); ?></td>
<td class="pButton"><a href="edittask.php?id=<?php echo $row_rsList['taskid']; ?> " title="Edit" ><img src="images/btnEdit.gif"/> </a></td>

</tr>

</form>
<?php } while ($row_rsList = mysql_fetch_assoc($rsList)); ?>
</table>
<p>&nbsp;</p>
</div>


</body>
</html>
<?php

mysql_free_result($rsList);


?>[/code]
Copy linkTweet thisAlerts:
@glenngauthorDec 15.2005 — Does the silence mean I have done something in my code that is incredibly stupid, or that the code looks ok?
Copy linkTweet thisAlerts:
@kelly23Dec 15.2005 — As mentioned above, your form needs an id.
Copy linkTweet thisAlerts:
@kelly23Dec 15.2005 — You might try this also: (just a suggestion)

onchange="document.getElementById(this.form).submit(); [b]window.location.reload(true)[/b]"
Copy linkTweet thisAlerts:
@hiwindzMar 29.2010 — onclick="document.getElementById(this.form.id).submit()"
×

Success!

Help @glenng spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...