Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

How to check if a type=submit & type= button is clicked by user?

This is main .php page

<form action="edit.php" method="post" enctype="multipart/form-data">
<?php for ($i=1; $i<=6; $i++){ 

echo $_SESSION['number'.$i]=$i;
?>



<input type="submit" name= "submit<?php echo $i;?>" value="Edit" >
<input type="button" name= "detail<?php echo $i;?>" value="Details" >
<?php } ?>

This is edit.php page

for ($y=1; $y<=6; $y++){
if (isset($_POST['submit'.$y])) {
$_SESSION['result'] = $_SESSION['number'.$y];
}
}
echo $_SESSION['result'] ;
?>

from the above code, which mean that submit1, detail1 have value of '1' ; submit2, detail2 have value of '2' and respectively. when i press the 3rd submit button in edit.php i having value of '3'.

how bout if i press detail3 i wan to have move the $_SESSION[result] to detail.php. But i have no idea how to bring the value to detail.php page when detail3 button clicked?

Please advice

2 Answers

personally I would not use different pages to do the processing but post to the same page that way you can work out which submit button is being clicked.The only other way to do it in your case would be to use jquey ajax and a button's onlick event to post to the appropriate page.

** change you input type like this <input type="submit" name= "detail<?php echo $i;?>" value="Details" > <?php if($_SERVER["REQUEST_METHOD"]=="POST") { for ($y=1; $y<=6; $y++) { if (isset($_POST['submit'.$y])) { $_SESSION['result'] = $_SESSION['number'.$y]; } if (isset($_POST['detail'.$y])) { $_SESSION['result'] = $_SESSION['number'.$y];
} } echo $_SESSION['result'] ;

}

?> <form action=" " method="post" enctype="multipart/form-data"> <?php for ($i=1; $i<=6; $i++){?>

<? echo $_SESSION['number'.$i]=$i; ?>

<input type="submit" name= "submit<?php echo $i;?>" value="Edit" > <input type="submit" name= "detail<?php echo $i;?>" value="Details" > <?php } ?>

Thanks Andreas, it really helpful