Jump to content

JavaScript to PHP to MYSQL - Checkbox


ripcurlksm

Recommended Posts

I have a login system for users to get reports.... On the backend, I have an admin function which allows the admin to set privileges for each user, so that the user only gets the reports they purchased. In the admin section, a series of checkboxes appear for each report. I need to gather this data and write it to the database. [B]In short, I need is to get a JavaScript function that takes all the reports that have a checked box, and then write them into the permissions table of the database.[/B]

How do I pass the values of each checked box from JavaScript, then pass it to PHP to write an SQL statement for each entry, for example here is what I would need to transfer from checkbox's with the value of 200, 322, and 505 for user with the user_id of 1.

PERMISSIONS TABLE
------------------------
user_id  |    report_id
------------------------
  1        |  200
  1        |  322
  1        |  505


JavaScript to PHP to MYSQL... any suggestions?
Link to comment
https://forums.phpfreaks.com/topic/33868-javascript-to-php-to-mysql-checkbox/
Share on other sites

I am a little confused how are you using Javascript?

Your form checkboxes are html elements, your form if its being submitted is being passed to PHP, and from PHP you can send the form data to MySQL. So where is the Javascript coming into this? It doesn't make sense.
I am trying a set of different code that I am close to getting running but I keep getting an error with the following:

[QUOTE]Warning: Invalid argument supplied for foreach() in /home/content/l/s/i/lsintelligence/html/emt/admin/editsubscriber3.php on line 75
insert into user_reports (user_id, report_id) values[/QUOTE]


[B]Here is the code that prints the form checkbox's:[/B]
[CODE]
<form name='form1' method='post' action='editsubscriber3.php'>
<?
dbConnect();
// This gets all of the reports so that the admin can set permissions for individual reports
$sql = "SELECT * FROM emt_report ORDER BY date_year DESC, date_month DESC, company ASC"; 
$result = mysql_query($sql) or user_error("mysql error " . mysql_error());
$rank = 1;

while($row = mysql_fetch_assoc($result))
{
    $id = $row['id'];


  print("<input name='reportbox[]' type='checkbox' value='$id'>");

  $rank++;
}
?>
[/CODE]

[B]Here is the page that handles receiving the data and writing to the database.[/B]
[CODE]
<?
$user = $_GET['user'];

// --------THIS LINE BELOW IS THROWING THE ERROR --------
$buf = array();
foreach ($_POST['reportbox'] as $reportbox) {
    $buf[] = sprintf('(%d, %d)', $userid, $id);
}

echo dbConnect();
echo $sql = "insert into user_reports (user_id, report_id) values " . implode(",\n ", $buf);

?>
[/CODE]


What am I doing wrong? I kept testing and modifying but I cant get this error to stop... :sick:

Archived

This topic is now archived and is closed to further replies.

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