CarmenH Posted June 21, 2011 Share Posted June 21, 2011 I am trying to setup a drop down menu that stores values to a file. The drop down menu only appears after another form has been submitted, so the functions that create the drop down as well as the function that populates the data only operate/appear when a post has been submitted. I am then trying to post the data from the drop down into the text file. I can clarify more if needed, but the code i think speaks for itself better. <code> function AssignReps ($depart_id, $shift_id, $task_id){ echo "<p>Please Assign Reps to ".$task_id."</p>"; $realID=SelectEmps($depart_id, $shift_id) ; $friendlyname= DisplayEmpNames($realID); echo "<form method=\"post\" action=\"\" name=\"testform\">"; $menu= createDropdown($realID, "name", $friendlyname); print_r($menu); echo "<input name=\"assign\" type=\"submit\" value=\"Continue\" />"; echo "</form>"; if($_POST['assign']){ $submission= isset($_POST['name']) ? trim($_POST['name']) : die('Missing entry: Rep Selection'); echo $submission; $myFile = $task_id."file.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); fwrite($fh, $submission); fclose($fh); } } //Application if($_POST) //setting varibles from form input { $shift_id= isset($_POST['shift']) ? trim($_POST['shift']) : die('Missing entry: Shift Selection'); $depart_id= isset($_POST['department']) ? trim($_POST['department']) : die('Missing entry: Department Selection'); $task_id= isset($_POST['task']) ? trim($_POST['task']) : die('Missing entry: Task Selection'); AssignReps ($depart_id, $shift_id, $task_id); } </code> I need help with the double $_POST. The first one operates normally but the second one, inside the AssignReps function fails due to the data not meeting the previous $_POSTS required values. Any ideas? Thanks much Quote Link to comment Share on other sites More sharing options...
WebStyles Posted June 21, 2011 Share Posted June 21, 2011 I don't really get what you want. do you want them both to run, or just one or the other... if only one of them should run, based on the existence of $_POST['assign'], change last if statement to something like: if(isset($_POST) && !isset($_POST['assign'])) ... hang on.. Ignore that... I just had a better look at your code... My bad. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted June 21, 2011 Share Posted June 21, 2011 the one inside the function probably fails because $_POST['assign'] is not set. You have a condition specifically for that. Could you please explain EXACTLY what you want to happen ? (also, consider changing fopen(), fwrite(), fclose() for file_put_contents(). it will do the same and is a better practice.) Quote Link to comment Share on other sites More sharing options...
CarmenH Posted June 22, 2011 Author Share Posted June 22, 2011 I will look more into file_put_contents. I honestly didn't know that was an available function. Thanks! This is what I want to happen. User picks out the shift, department and task from drop down menus- and clicks continue (bottom _POST that contains the AssignReps function). These values are then passed to the AssignReps function and the matching reps for shift and department are displayed in another drop down box. The user will then select the reps in the drop down box. Upon post- these will be written to a file. (_POST inside the AssignReps function.) This is a read only database, so I am not able to create or modify tables in anyway, hence writing to the file. Thanks for reviewing. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted June 22, 2011 Share Posted June 22, 2011 try something like this (untested) I removed the second $_POST from inside the function, <?php function AssignReps ($depart_id, $shift_id, $task_id){ echo '<p>Please Assign Reps to '.$task_id.'</p>'; $realID=SelectEmps($depart_id, $shift_id) ; $friendlyname= DisplayEmpNames($realID); echo '<form method="post" action="" name="testform">'; $menu= createDropdown($realID, "name", $friendlyname); print_r($menu); echo '<input name="assign" type="submit" value="Continue" />'; echo '</form>'; } if(isset($_POST)){ if(isset($_POST['assign'])){ $submission= isset($_POST['name']) ? trim($_POST['name']) : die('Missing entry: Rep Selection'); echo $submission; $myFile = $task_id."file.txt"; file_put_contents($myFile,$submission); }else{ $shift_id= isset($_POST['shift']) ? trim($_POST['shift']) : die('Missing entry: Shift Selection'); $depart_id= isset($_POST['department']) ? trim($_POST['department']) : die('Missing entry: Department Selection'); $task_id= isset($_POST['task']) ? trim($_POST['task']) : die('Missing entry: Task Selection'); AssignReps ($depart_id, $shift_id, $task_id); } } ?> Quote Link to comment Share on other sites More sharing options...
CarmenH Posted June 23, 2011 Author Share Posted June 23, 2011 nearly worked!! Replaced the first if(isset(($_POST)){ if(isset($_POST['assign'])){ with if($_POST){ if(isset($_POST['assign'])){ and it works beautifully. Thank you! 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.