Jax2 Posted April 9, 2010 Share Posted April 9, 2010 Hi guys, I'm trying to sort this out ... I have a table with location_id and company_name and I'd like to be able to select as many of these combos as I can (I.e., location_id 1 = such and such company) ... I tried making a test form but it's not working correctly. Even if I choose 1 location, it is returning all 4 as having been chosen, as well as a mysterious 5th selection, when there's only 4 to choose from... I'd appreciate it if someone would be able point out my error(s)? Here's the test code: $sql="SELECT * FROM host_locations"; $result=mysql_query($sql, $db); $total=mysql_num_rows($result); while ($row=mysql_fetch_array($result)) { $id[]=$row['location_id']; $name[]=$row['company_name']; } $i = 0; echo "<form method='POST' action='testcheckboxes.php'>"; while ($i<$total) { echo "<input type='checkbox' name=location value=".$id[$i].">".$name[$i]."<br>"; $i++; } echo "<input type='submit' name='doSubmit' value='Go!'>"; echo "</form>"; ?> <br><br> <div align="center">Testing checkmarks</div> <?php $id[]=$_POST['id']; $name[]=$_POST['name']; foreach($id as $location) { echo "You chose: Location ID:".$location."<br>"; } ?> The first part shows up correctly at least: I get 4 checkboxes, all unchecked, with the company name beside them ... submit button below. Link to comment https://forums.phpfreaks.com/topic/198166-another-question-check-box-arrays-and-forms/ Share on other sites More sharing options...
the182guy Posted April 9, 2010 Share Posted April 9, 2010 Use a checkbox array, instead of having the checkboxes called location which is not an array. Try this // notice the new name location[] echo "<input type='checkbox' name="location[]" value=".$id[$i].">".$name[$i]."<br>"; Then to see which were checked you can just use if($_SERVER['REQUEST_METHOD'] == 'POST') // was the form posted { // yes the form was posted, loop through any checked checkboxes foreach($_POST['location'] as $value) { echo 'location ticked: ' . $value; } } Link to comment https://forums.phpfreaks.com/topic/198166-another-question-check-box-arrays-and-forms/#findComment-1039759 Share on other sites More sharing options...
Jax2 Posted April 9, 2010 Author Share Posted April 9, 2010 That worked perfectly ... Question though, I am going to be adding the array to the database (not only displaying it) which shouldn't be an issue, however, is it pretty similar method to find out what is in the database under locations_selected and to show a checked checkbox for each one they've chosen? My client would like the check boxes to represent which location the customer has chosen for such and such, so when you look at the edit_info form, you'll see as many checkboxes as there are locations, and the ones they've already chosen should be checked ... know what I mean? Link to comment https://forums.phpfreaks.com/topic/198166-another-question-check-box-arrays-and-forms/#findComment-1039770 Share on other sites More sharing options...
the182guy Posted April 9, 2010 Share Posted April 9, 2010 To show a checkbox already checked when the page loads you can do it like.. // notice the 'checked' echo "<input type='checkbox' name="location[]" value=".$id[$i]." [b]checked [/b]>".$name[$i]."<br>"; So you would need to retrieve the checked locations from that database, then when displaying the checkbox inputs, compare the $id[$i] with the checked locations from the database, if found a match then insert 'checked' into the tag to auto select it. Link to comment https://forums.phpfreaks.com/topic/198166-another-question-check-box-arrays-and-forms/#findComment-1039773 Share on other sites More sharing options...
Jax2 Posted April 9, 2010 Author Share Posted April 9, 2010 Okay, this is where I'm running into a problem now. On the test page, everything showed up as perfect, but when I try and update the database with the new values for the check boxes (2, 8, 9, or 10, or any combination of), I am getting a blank variable. Using your method, I did the following: $contract_number = clean($_POST['contract_number']); // misc stuff not checkbox $ad_cost = clean($_POST['ad_cost']); // $monthly_fee = clean($_POST['monthly_fee']); // $additional_info = clean($_POST['additional_info']); // foreach($_POST['location'] as $value) { $advertising_locations.= $value; } Which I thought would add each number (again, 2, 8,9 or 10 or combo of) to $advertising_locations and insert it into the DB ... apparently not. Please forgive me, arrays are NOT my strong point here. Link to comment https://forums.phpfreaks.com/topic/198166-another-question-check-box-arrays-and-forms/#findComment-1039777 Share on other sites More sharing options...
the182guy Posted April 9, 2010 Share Posted April 9, 2010 What variable is blank? What are you doing with $advertising_locations after the code you posted? The contents of that variable will look like: 28910 if all are checked Link to comment https://forums.phpfreaks.com/topic/198166-another-question-check-box-arrays-and-forms/#findComment-1039780 Share on other sites More sharing options...
Jax2 Posted April 9, 2010 Author Share Posted April 9, 2010 You were right, I was trying to add locations, not advertising_locations ... so I now have the following in my database: 9102 9, 10, 2 ... However, it's not going to be able to be read like that, is it? I mean, that is one number, not an array of numbers or will it be recognized as 9,10,2? So again, I need to figure out how to show those locations and auto-checkmark the correct checkboxes, for which I'll go back to your other post, if it can use the data as it currently is ... Thanks for the help so far 182, you're doing me a huge service here... Link to comment https://forums.phpfreaks.com/topic/198166-another-question-check-box-arrays-and-forms/#findComment-1039784 Share on other sites More sharing options...
the182guy Posted April 9, 2010 Share Posted April 9, 2010 I would store them in a seperate table with three fields: id (PK), location_id, submission_id. submission_id would be the PK of the table that stores the form data (contact_number, ad_cost) etc. So what I would do is insert the form into the main table. Then get the mysql_insert_id(), then loop through the checked locations and insert each as a new row into the new table with the location, and submission_id (found from mysql_insert_id). You could also just store all of the checked locations in one string seperating them by commas, and they would just go in one field. E.g. '9, 10, 2' Link to comment https://forums.phpfreaks.com/topic/198166-another-question-check-box-arrays-and-forms/#findComment-1039827 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.