Jump to content

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 dont see how javascript comes into this, so is this basically a permission kind of thing for your users? and are the permissions only: 200, 322, and 505.
btw, please paste some codes for us to get an idea of what you are doing.
Ted
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:
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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