simplyrichard Posted October 27, 2008 Share Posted October 27, 2008 I'm having a litte trouble trying to think out the best process to collect a large number of items checkmarked. I will later have to insert a database row depending on each item selected here is an example of what I am trying to do. List of open positions (Select all positions that you would like to apply for) Office Manager House Keeping HR Administrator Director of IT Lets say the user selects three of the four above. I call the $_POST['jobs_selected']; and now I have an array if I am not mistaken. How do I use the information within $_POST['jobs_selected']; I will need to insert each item selected into a different table. The user can select many jobs to apply to at once which could be different departments, I want to insert the application in each of the department's tables I have created. Therefore I need to know each job ID that the user selected. How would I do this? Thanks for your help. Richard Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted October 27, 2008 Share Posted October 27, 2008 Assuming that your HTML is correct where: name="jobs_selected[]" <-- Don't forget the square brackets foreach($_POST['jobs_selected'] as $job){ // Do a db query // Do anything else you want // Each value now is referred to by using $job echo 'Job Selected: '.$job.'<br />'; } Quote Link to comment Share on other sites More sharing options...
simplyrichard Posted October 27, 2008 Author Share Posted October 27, 2008 Great thank you! Now how do I auot assign each of them to their very own variable? Quote Link to comment Share on other sites More sharing options...
simplyrichard Posted October 27, 2008 Author Share Posted October 27, 2008 Here is an example of what I am trying to do which is not work... setcookie("jobs_selected", $_POST['apply'], time()+3600); foreach($_COOKIE["jobs_selected"]; as $key=>$value) { // and print out the values echo $key." ".$value.' <br />'; } Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted October 27, 2008 Share Posted October 27, 2008 It may be because of the semi-colon in your foreach statement. Quote Link to comment Share on other sites More sharing options...
simplyrichard Posted October 27, 2008 Author Share Posted October 27, 2008 I noticed that after I posted it. That didnt work either. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted October 27, 2008 Share Posted October 27, 2008 after the setCookie command, the cookies are not available until the next page load.... that aside, i don't see the need for cookies here. once the info is submitted you want to store them in the tables right? do you have an INSERT commands written yet? how are the tables setup? Quote Link to comment Share on other sites More sharing options...
simplyrichard Posted October 27, 2008 Author Share Posted October 27, 2008 I'm not ready to store them into a table. I want to store them until I need them. For me to use them I have to make sure an applicant has filled out an application and of course is logged in. I thought a cookie would work well because I have no information about the user until after they have registered or logged in. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted October 27, 2008 Share Posted October 27, 2008 well, setCookie can work, but you can't access the cookie until the page has been reloaded. ...i would recommend storing it into the SESSION Quote Link to comment Share on other sites More sharing options...
simplyrichard Posted October 27, 2008 Author Share Posted October 27, 2008 Like this? $_SESSION['jobs'] = $_POST['apply']; foreach($_SESSION['jobs'] as $key => $value){ // Do a db query // Do anything else you want // Each value now is referred to by using $job echo $key." ".$value'<br />'; } Quote Link to comment Share on other sites More sharing options...
Andy-H Posted October 27, 2008 Share Posted October 27, 2008 session_start(); foreach($_POST['jobs_selected'] as $job){ $_SESSION['jobs_selected'][] = $job; } if you wish to see how the info is stored print_r($_SESSION['jobs_selected']); I think that should work ok. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted October 27, 2008 Share Posted October 27, 2008 Like this? $_SESSION['jobs'] = $_POST['apply']; foreach($_SESSION['jobs'] as $key => $value){ // Do a db query // Do anything else you want // Each value now is referred to by using $job echo $key." ".$value'<br />'; } yes, that should work, just make sure you do a session_start() at the beginning of the script. again, this assumes you don't want to use the data right away. Quote Link to comment Share on other sites More sharing options...
simplyrichard Posted October 27, 2008 Author Share Posted October 27, 2008 Thanks a million! I have it working now! You guys Rock! Richard Quote Link to comment 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.