Bl4ckMaj1k Posted May 13, 2011 Share Posted May 13, 2011 Good afternoon all!! I have a very specific issue that, in my opinion, is quite complicated. In words, here is what I wish to achieve. I would like a page which displays users from the database. Each individual user has their own Div hidden below their name. Within this div, there is a form. The form is full of radio buttons. Once the user's name is clicked and form submitted, I wish to write the form data to the database for the specific user that was selected. See below example for better understanding: Dale Gibbs Chris Hansen Steve Jobs If I click Chris Hansen, the following happens Dale Gibbs ============== Chris Hansen HIDDEN DIV FORM CONTENT HIDDEN DIV FORM CONTENT HIDDEN DIV FORM CONTENT HIDDEN DIV FORM CONTENT HIDDEN DIV FORM CONTENT Submit Button ============== Steve Jobbs So as of now, the display is correct. I am seeing exactly what I want to see from my database. Also, my div IDs work just fine as well as the Javascript toggleSlidebox function. The only issue is for some reason, whenever I submit the form (no matter which user I select from my list), I can only write to the last inputted ID. So for example, if the last ID entered into the database was 6, then thats the only ID that will be returned when my form is submitted and the only place data will be written to, even if I select a user with ID 2. Please see below code for more information <?php $staff_display_query = "SELECT staff_info.id, staff_info.fname, staff_info.lname FROM staff_info, staff_projects WHERE staff_info.id = staff_projects.staff_id AND staff_projects.proj_id = '$c_project_id'"; $staff_display_sql = mysql_query($staff_display_query) or die (mysql_error()); while ($row = mysql_fetch_array($staff_display_sql)) { $current_staff_id = $row['id']; $staff_fname = $row['fname']; $staff_lname = $row['lname']; $list_staff .= ' ' . $current_staff_id . '<br /> <a href="#" onclick="return false" onmousedown="javascript:toggleSlideBox(' . $current_staff_id . ');">' . $staff_fname . ' ' . $staff_lname . '</a> <div class="hiddenDiv" id="' . $current_staff_id . '" style="border:#FFF 2px solid; width:553px;"> <!--TASK 1--> <div id="task_1_permissions" class="task_permissions"> <input name="permissions_1" type="radio" value="1"/> <input name="permissions_1" type="radio" value="2" /> <input name="permissions_1" type="radio" value="3" /> <input name="permissions_1" type="radio" value="0" /> </div> <!--TASK 2--> <div id="task_2_permissions" class="task_permissions"> <input name="permissions_2" type="radio" value="1"/> <input name="permissions_2" type="radio" value="2" /> <input name="permissions_2" type="radio" value="3" /> <input name="permissions_2" type="radio" value="0" /> </div> <!--TASK 3--> <div id="task_3_permissions" class="task_permissions"> <input name="permissions_3" type="radio" value="1"/> <input name="permissions_3" type="radio" value="2" /> <input name="permissions_3" type="radio" value="3" /> <input name="permissions_3" type="radio" value="0" /> </div> <!--TASK 4--> <div id="task_4_permissions" class="task_permissions"> <input name="permissions_4" type="radio" value="1"/> <input name="permissions_4" type="radio" value="2" /> <input name="permissions_4" type="radio" value="3" /> <input name="permissions_4" type="radio" value="0" /> </div> <input name="submit_user_permissions" type="submit" value="Submit Permissions" /> </div> </div> <br /><br /> '; } if (isset($_POST['submit_user_permissions'])) { $permissions_1 = $_POST['permissions_1']; $permissions_2 = $_POST['permissions_2']; $permissions_3 = $_POST['permissions_3']; $permissions_4 = $_POST['permissions_4']; $query = "UPDATE staff_projects SET task_1='$permissions_1', task_2='$permissions_2', task_3='$permissions_3', task_4='$permissions_4' WHERE proj_id='$c_project_id' AND staff_id='$current_staff_id'"; $sql = mysql_query($query) or die (mysql_error()); echo 'Permissions set successfully.<br />'; } After this PHP I have my standard HTML. Below is the javascript function I use for the slide box: <script src="js/jquery-1.5.js" type="text/javascript"></script> <script language="javascript" type="text/javascript"> function toggleSlideBox(x) { if ($('#'+x).is(":hidden")) { $(".hiddenDiv").slideUp(200); $('#'+x).slideDown(300); } else { $('#'+x).slideUp(300); } } </script> Javascript works just fine by the way. Below is the form I have in the HTML of the code. <form action="" method="post" enctype="multipart/form-data"> <?php echo "$list_staff"; ?> </form> This is of course wrapped around body tags and all the other necessary HTML. The view of the form is working right, the functions within the form are also working correctly. I just cant seem to separate the variables for each individual user. If I am in Chris Hansen's div, it should be his ID number that is being referenced for the database, not the ID number of the last person entered into the system. Any ideas? As always, thanks in advance guys!!! Bl4ck Maj1k Quote Link to comment https://forums.phpfreaks.com/topic/236343-help-separation-of-ids-pulled-from-database-and-stored-into-form/ Share on other sites More sharing options...
spiderwell Posted May 13, 2011 Share Posted May 13, 2011 looking it seems you have one big form encompassing all the divs and users. is that the reason? dont you need a seperate userid for each user, by making each div its own form? Quote Link to comment https://forums.phpfreaks.com/topic/236343-help-separation-of-ids-pulled-from-database-and-stored-into-form/#findComment-1215162 Share on other sites More sharing options...
Bl4ckMaj1k Posted May 15, 2011 Author Share Posted May 15, 2011 EDIT: Just tested and it still doesnt work. Unfortunately, i believe I will need to do some type of foreach statement. I say unfortunately because I have no idea how that works. Anyone wanna help me out in changing this code in such a way that will deliver me each individual user id depending on which div I have selected? Hmmm....I don't know how I would go about doing that...would I just place the form tags in the PHP function that calls all the divs? I thought about doing this but I still don't understand how that would allow me to separate the individual User IDs. In any case, I will test this and let you know if it works. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/236343-help-separation-of-ids-pulled-from-database-and-stored-into-form/#findComment-1215827 Share on other sites More sharing options...
spiderwell Posted May 16, 2011 Share Posted May 16, 2011 at the moment, the reason why you are updating the last user, is because when you post the form, you also do the select statement still, and the last user in the data has his id passed to $current_staff_id and thats the id that is passed to the update sql statement. what you need to do is put the form tags where i told you, add a hidden field to the form that is the userid for that row in the recordset. then when the from is posted, inside the update if statement, grab the hidden id from the form $_POST array, and pass that one to the update sql statement instead. that way you will seperate out the ids Quote Link to comment https://forums.phpfreaks.com/topic/236343-help-separation-of-ids-pulled-from-database-and-stored-into-form/#findComment-1216134 Share on other sites More sharing options...
Bl4ckMaj1k Posted May 16, 2011 Author Share Posted May 16, 2011 Where exactly in the code are you saying I need to place the form and the hidden form? Sorry but I just don't understand where exactly in the code you are saying the changes need to be made. Can you give me an example? Quote Link to comment https://forums.phpfreaks.com/topic/236343-help-separation-of-ids-pulled-from-database-and-stored-into-form/#findComment-1216135 Share on other sites More sharing options...
Bl4ckMaj1k Posted May 16, 2011 Author Share Posted May 16, 2011 I don't mean to nag but can someone please help me with this issue? I really can't seem to understand storing each of these IDs as a separate entity. I keep pulling the last ID that was stored in the database. I think my new confusion is how to work the hidden form function so that would grab the variables for me and separate each form or ID. Quote Link to comment https://forums.phpfreaks.com/topic/236343-help-separation-of-ids-pulled-from-database-and-stored-into-form/#findComment-1216209 Share on other sites More sharing options...
jcbones Posted May 16, 2011 Share Posted May 16, 2011 He means like this: <?php $staff_display_query = "SELECT staff_info.id, staff_info.fname, staff_info.lname FROM staff_info, staff_projects WHERE staff_info.id = staff_projects.staff_id AND staff_projects.proj_id = '$c_project_id'"; $staff_display_sql = mysql_query($staff_display_query) or die (mysql_error()); while ($row = mysql_fetch_array($staff_display_sql)) { $current_staff_id = $row['id']; $staff_fname = $row['fname']; $staff_lname = $row['lname']; $list_staff .= ' ' . $current_staff_id . '<br /> <a href="#" onclick="return false" onmousedown="javascript:toggleSlideBox(' . $current_staff_id . ');">' . $staff_fname . ' ' . $staff_lname . '</a> <div class="hiddenDiv" id="' . $current_staff_id . '" style="border:#FFF 2px solid; width:553px;"> <form action="" method="post"> <input type="hidden" name="staff_id" value="' . $current_staff_id . '" /> <!--TASK 1--> <div id="task_1_permissions" class="task_permissions"> <input name="permissions_1" type="radio" value="1"/> <input name="permissions_1" type="radio" value="2" /> <input name="permissions_1" type="radio" value="3" /> <input name="permissions_1" type="radio" value="0" /> </div> <!--TASK 2--> <div id="task_2_permissions" class="task_permissions"> <input name="permissions_2" type="radio" value="1"/> <input name="permissions_2" type="radio" value="2" /> <input name="permissions_2" type="radio" value="3" /> <input name="permissions_2" type="radio" value="0" /> </div> <!--TASK 3--> <div id="task_3_permissions" class="task_permissions"> <input name="permissions_3" type="radio" value="1"/> <input name="permissions_3" type="radio" value="2" /> <input name="permissions_3" type="radio" value="3" /> <input name="permissions_3" type="radio" value="0" /> </div> <!--TASK 4--> <div id="task_4_permissions" class="task_permissions"> <input name="permissions_4" type="radio" value="1"/> <input name="permissions_4" type="radio" value="2" /> <input name="permissions_4" type="radio" value="3" /> <input name="permissions_4" type="radio" value="0" /> </div> <input name="submit_user_permissions" type="submit" value="Submit Permissions" /> </form> </div> </div> <br /><br /> '; } if (isset($_POST['submit_user_permissions'])) { $permissions_1 = $_POST['permissions_1']; $permissions_2 = $_POST['permissions_2']; $permissions_3 = $_POST['permissions_3']; $permissions_4 = $_POST['permissions_4']; $current_staff_id = (isset($_POST['staff_id'])) ? $_POST['staff_id'] : NULL; $query = "UPDATE staff_projects SET task_1='$permissions_1', task_2='$permissions_2', task_3='$permissions_3', task_4='$permissions_4' WHERE proj_id='$c_project_id' AND staff_id='$current_staff_id'"; $sql = mysql_query($query) or die (mysql_error()); echo 'Permissions set successfully.<br />'; } Quote Link to comment https://forums.phpfreaks.com/topic/236343-help-separation-of-ids-pulled-from-database-and-stored-into-form/#findComment-1216225 Share on other sites More sharing options...
Bl4ckMaj1k Posted May 16, 2011 Author Share Posted May 16, 2011 Got it, works perfectly...thanks!!! One question. Do you mind explaining the following line to me. I can honestly say I have never seen this done before: $current_staff_id = (isset($_POST['staff_id'])) ? $_POST['staff_id'] : NULL; I understand you are saying that the current_staff_id variable will be equal to whatever variable is stored in the hidden div. But what is the ? $_POST['staff_id'] : NULL; ?? Can you explain this line? Quote Link to comment https://forums.phpfreaks.com/topic/236343-help-separation-of-ids-pulled-from-database-and-stored-into-form/#findComment-1216234 Share on other sites More sharing options...
jcbones Posted May 16, 2011 Share Posted May 16, 2011 In laymens terms, it is a short way to write an if/else statement. $var = (condition) ? true : false; So it means "if staff_id is in the post array, the assign it to $current_staff_id, else define $current_staff_id as NULL". Ternary Operator Quote Link to comment https://forums.phpfreaks.com/topic/236343-help-separation-of-ids-pulled-from-database-and-stored-into-form/#findComment-1216237 Share on other sites More sharing options...
spiderwell Posted May 16, 2011 Share Posted May 16, 2011 **edit haha beat me to it, i was to long winded and slow typing ** its a special kind of if statement, a very short boolean one (true/false) breaking it down it checks if the form posted has the field staff_id isset($_POST['staff_id']) that is wrapped in a ternary comparison operator which is the bit you don't understand. it basically works like this (if true) ? 'return this'; 'else return this' so in this instance it is saying if the $_POST['staff_id'] exists, return its value (the return is passed to $current_staff_id), otherwise return nothing. isset($_POST['staff_id'])) ? $_POST['staff_id'] : NULL; and the result is passed to the variable $current_staff_id = (isset($_POST['staff_id'])) ? $_POST['staff_id'] : NULL; Quote Link to comment https://forums.phpfreaks.com/topic/236343-help-separation-of-ids-pulled-from-database-and-stored-into-form/#findComment-1216241 Share on other sites More sharing options...
Bl4ckMaj1k Posted May 16, 2011 Author Share Posted May 16, 2011 You guys are nothing short of GENIUSES!!!!!! Man I love this place. Thanks again boyz and girlz....another one for the books! SOLVED!!!! Quote Link to comment https://forums.phpfreaks.com/topic/236343-help-separation-of-ids-pulled-from-database-and-stored-into-form/#findComment-1216242 Share on other sites More sharing options...
spiderwell Posted May 17, 2011 Share Posted May 17, 2011 You guys are nothing short of GENIUSES!!!!!! Man I love this place. Thanks again boyz and girlz....another one for the books! SOLVED!!!! think you mean geniui Quote Link to comment https://forums.phpfreaks.com/topic/236343-help-separation-of-ids-pulled-from-database-and-stored-into-form/#findComment-1216442 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.