JavaScript to PHP to MYSQL - Checkbox

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ripcurlksm
Forum Commoner
Posts: 34
Joined: Sun Aug 08, 2004 9:17 pm

JavaScript to PHP to MYSQL - Checkbox

Post by ripcurlksm »

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

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?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

You're definitely on the right track there - well, maybe minus the javascript.
  • HTML forms submit data to the server.
    PHP has the ability to use this information.
    PHP also has the ability to query MySQL databases.
So to recap: that's 3 technologies at play (without javascript) and they're definitely worth learning. That being said, what kind of time-frame were you hoping for here?
ripcurlksm
Forum Commoner
Posts: 34
Joined: Sun Aug 08, 2004 9:17 pm

Almost there..

Post by ripcurlksm »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


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: Select all

<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++;
}
?>
Here is the page that handles receiving the data and writing to the database.

Code: Select all

<?
$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); 

?>

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


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

make sure $_POST['reportbox'] exists in the post data and is an array.

isset()/array_key_exists() + is_array()
Post Reply