Jump to content

Help separation of IDs pulled from database and stored into form


Bl4ckMaj1k

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 />';

}


Link to comment
Share on other sites

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?

Link to comment
Share on other sites

**edit haha beat me to it, i was to long winded and slow typing  :P **

 

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;

Link to comment
Share on other sites

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.