Jump to content

Double $_POST or $_POST inside a function


CarmenH

Recommended Posts

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

 

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.)

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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);
}
}
?>

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.