coldasice Posted April 29, 2011 Share Posted April 29, 2011 i want to insert only whats checked to db:) but everything get posted for now i came upwith a for loop but thats for inserting all data not only whats checked any one know a good way to do this? Quote Link to comment https://forums.phpfreaks.com/topic/235127-inserting-data-to-from-via-checkbox/ Share on other sites More sharing options...
wildteen88 Posted April 29, 2011 Share Posted April 29, 2011 What do mean by "checked". You need to post more information to your problem along with your code. Quote Link to comment https://forums.phpfreaks.com/topic/235127-inserting-data-to-from-via-checkbox/#findComment-1208384 Share on other sites More sharing options...
coldasice Posted April 29, 2011 Author Share Posted April 29, 2011 oh my bad this was the old info i upg it right way what i mean is when i check i only what whats where i check to go to the $_POST! but now everything goes.. Quote Link to comment https://forums.phpfreaks.com/topic/235127-inserting-data-to-from-via-checkbox/#findComment-1208404 Share on other sites More sharing options...
Psycho Posted April 29, 2011 Share Posted April 29, 2011 Yes, everything in the form will be passed with the POST data. You could probably implement some JavaScript to run at the time of submission to remove those values, but that's not necessary. What you need to do is implement the solution on the processing page by checking which checkboxes are checked and then only processing the associated values. If you were to show the form and the processing code we could help you further. Quote Link to comment https://forums.phpfreaks.com/topic/235127-inserting-data-to-from-via-checkbox/#findComment-1208407 Share on other sites More sharing options...
Pikachu2000 Posted April 29, 2011 Share Posted April 29, 2011 What do mean by "checked". You need to post more information to your problem along with your code. Quote Link to comment https://forums.phpfreaks.com/topic/235127-inserting-data-to-from-via-checkbox/#findComment-1208410 Share on other sites More sharing options...
coldasice Posted April 29, 2011 Author Share Posted April 29, 2011 Yes, everything in the form will be passed with the POST data. You could probably implement some JavaScript to run at the time of submission to remove those values, but that's not necessary. What you need to do is implement the solution on the processing page by checking which checkboxes are checked and then only processing the associated values. If you were to show the form and the processing code we could help you further. well thanks that helps this is my form kode and i dont have any prosess code yet since i dont know how to do it with this type of form input $this->_sql = "SELECT * FROM user"; $this->_query = mysql_query($this->_sql); echo '<div id=users> Update ur Admin status<br /> Username | Admin level | Ban | tids ban format: yyyy-mm-dd h:m:s <br />'; echo '<form method="Post">'; while ($user = mysql_fetch_object($this->_query)) { echo ''.$user->username.': '; echo '<input type="checkbox" name=check[] value='.$user->id.' />'; echo '<select name=adminlvl[]>'; // enum from mysql $this->_sql = "SHOW COLUMNS FROM user LIKE 'adminlevel'"; if ($result = mysql_query($this->_sql)) { $enum = mysql_fetch_object($result); preg_match_all("/'([\w ]*)'/",$enum->Type,$values); $this->_values = $values[1]; } // end foreach ($this->_values as $k => $v) { if ($v == 1) { if ($user->adminlevel == 1) { echo '<option selected value="'.$v.'">User'; } else { echo '<option value="'.$v.'">User'; } } if ($v == 2) { if ($user->adminlevel == 2) { echo '<option selected value="'.$v.'" >Nyhets Admin'; } else { echo '<option value="'.$v.'">Nyhets Admin'; } } if ($v == 3) { if ($user->adminlevel == 3) { echo '<option selected value="'.$v.'">Moderator'; } else { echo '<option value="'.$v.'">Moderator'; } } if ($v == 4) { if ($user->adminlevel == 4) { echo '<option selected value="'.$v.'" selected>Administrator'; } else { echo '<option value="'.$v.'">Administrator'; } } } echo '</select>'; $this->_sql = "SHOW COLUMNS FROM user LIKE 'ban'"; echo '<select name=ban[]>'; if ($result = mysql_query($this->_sql)) { $enum = mysql_fetch_object($result); preg_match_all("/'([\w ]*)'/",$enum->Type,$values); $this->_values = $values[1]; } foreach ($this->_values as $k => $v) { if ($v == 0) { if ($user->ban == 0) { echo '<option selected value="'.$v.'">UnBan'; } else echo '<option value="'.$v.'">UnBan'; } if ($v == 1) { if ($user->ban == 1) { echo '<option selected value="'.$v.'">Ban'; } else echo '<option value="'.$v.'">Ban'; } } echo '</select>'; echo '<input type="text" value="'.date("Y-m-d H:i:s").'" name=bantime[] />'; echo "<br />"; } echo '<input type="submit" name="submit" value="Save" class="submitbox">'; echo '</form>'; echo "<br />"; echo "</div>"; Quote Link to comment https://forums.phpfreaks.com/topic/235127-inserting-data-to-from-via-checkbox/#findComment-1208419 Share on other sites More sharing options...
Psycho Posted April 29, 2011 Share Posted April 29, 2011 OK, you are naming your input fields so they will be sent/received as array - that is good. But, you are going to need to provide indexes for the fields such that a checkbox and it's related fields can be associated with one another. You are already using the user ID as the value of the checkbox, so you can use that as the index for the field names. Here are the fields I see in your code and one way you could provide indexes echo '<select name="adminlvl['.$user->id.']">'; // echo '<select name="ban['.$user->id.']">'; Then in your processign code you could do something like this foreach($_POST['check'] as $userID) { //Get the values to be updated for the selected user ID $adminlvl = $_POST['$_POST['check'][$userID]; $ban = $_POST['$_POST['ban'][$userID]; //Create query to update selected record $query = "UPDATE user SET adminlvl='$adminlvl', ban='$ban' WHERE id = $userID"; } NOTE: that is only a preliminary example. I have left all validation/escaping that should be done. Quote Link to comment https://forums.phpfreaks.com/topic/235127-inserting-data-to-from-via-checkbox/#findComment-1208428 Share on other sites More sharing options...
coldasice Posted April 29, 2011 Author Share Posted April 29, 2011 i c i will try and so on Quote Link to comment https://forums.phpfreaks.com/topic/235127-inserting-data-to-from-via-checkbox/#findComment-1208442 Share on other sites More sharing options...
coldasice Posted April 30, 2011 Author Share Posted April 30, 2011 Uploaded with ImageShack.us wow this shit is working thanks!!!! thats what i call a mind block.. strange it gives error Warning: Invalid argument supplied for foreach() in C:\Program Files (x86)\VertrigoServ\xxxx\xxx\xxx\xxx\xxxx.php on line 51 foreach ($check as $userID) { $adminlvlupd = $adminlvl[$userID]; $banupd = $ban[$userID]; $bantimeupd = $bantime[$userID]; $this->_sql = "UPDATE user SET adminlevel='".$adminlvlupd."',ban='".$banupd. "',ban_time='".$bantimeupd."' WHERE id='".$userID."'"; mysql_query($this->_sql) or die(mysql_error()); echo "user upd"; } Quote Link to comment https://forums.phpfreaks.com/topic/235127-inserting-data-to-from-via-checkbox/#findComment-1208459 Share on other sites More sharing options...
coldasice Posted April 30, 2011 Author Share Posted April 30, 2011 forgot to add a check to see if its set plz merge mod when ever u read.. cuz i couldnt edit Quote Link to comment https://forums.phpfreaks.com/topic/235127-inserting-data-to-from-via-checkbox/#findComment-1208474 Share on other sites More sharing options...
coldasice Posted April 30, 2011 Author Share Posted April 30, 2011 dunno if i shuld post this in here but now im stuck another place i dont quite get... why is that the loops stop when i try to add my date function to the textbox : ( [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/235127-inserting-data-to-from-via-checkbox/#findComment-1208562 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.