Link to home
Start Free TrialLog in
Avatar of Adam
AdamFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How can I carry out two action through one form (with one button) in php?

Hello,

I want to send a form with two actions. Those actions are to send two different values to another (the same) page. As individual actions these send but it's when I try to combine thw two actions into one form that I have problems.  

Something like this -

<form action="<?php echo 'addfav.php?id='. $teacher['id'] ;?>" 
<form action="<?php echo 'addfav.php?ids='. $_SESSION['session_ids'];?>"  method="post">

Open in new window


However,  from a bit of googling, I understand that a form can only send one action. From a bit of a reading online I made a number of failed attempts along these lines using input type 'hidden':

<form id="form" name="form" method="post" action="addfav.php">
<input type="button" class="btn btn-info btn-sm" />

<input type="hidden" name="teacherid" id="teacherid" value="<?php echo $teacher['id'] ;?>">
<input type="hidden" name="studentid" id="studentid" value="<?php echo $_SESSION['session_ids'];?> ">

</form>	

Open in new window



And then tried to pick up the posted values in addfav.php with the following

<?php if ((isset($_POST['teacherid']) && $_POST['teacherid'] != "") && (isset($_POST['studentid']) && $_POST['studentid'] != "")){

$studentid = $_POST['studentid']; 
$teacherid = $_POST['teacherid']; 

}	
?>

Open in new window



This doesn't work - Nothing happens when I press the button.

Is it correct that a form can't sent two actions, and if so, is using  input type 'hidden' the correct method moving forward? I'm trying to stick to using php and avoiding javascript if possible.  Any help, advice greatly appreciated as always.

Many thanks for reading.

Adam
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Adam

ASKER

Many thanks Chris!  Really happy that I was closer than I suspected, and it was a simple solution.

Have a great evening.