How to submit a multiple forms using the 'PHP_SELF' command?

Experts,

I have two form named 'find_report' & 'list_view'.  Both of these forms are using
action="<?php echo $_SERVER['PHP_SELF']; ?>" to submit the data supplied by the user.  Therefore the processing of the given information happens on the same page.

How can I differentiate between which form has been submitted?  I have a feeling that I could use the HTML tags 'name' or 'id' in order to tell the difference between which form has been submitted but, I have not ever done it before in PHP.

Can someone offer some advice on how to do this?  The code that I am using to process the data given by the user for the form called 'find_report' is below.
<?php
// THIS IS THE CODE THAT PROCESSES THE INFORMATION REQUESTED BY THE USER
// IT RETURNS THE REQUESTED REPORT TO THE BROWSER IN THIS FORMAT: find.php-ReportName-Month-Year
 
// GET THE INFORMATION PASSED BY THE FORM
$Name = $_POST['Name'];
$Month = $_POST['Month'];
$Year = $_POST['Year'];
 
// IF ALL FORM INFORMATION IS ENTERED, GO TO THE PAGE WITH THE FORM
if($Name != '' & $Month != '' & $Year != ''){
header ('location: find.php?name='.$Name.'&month='.$Month.'&year='.$Year.'');
}
?>

Open in new window

evibesmusicAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michael701Commented:
You didn't post much code, but I'll assume you have submit buttons on both forms

<input type='submit' name='submit' value='Find Report' />

and the other form

<input type='submit' name='submit' value='List View' />

-----------------

your php script can check the value returned

if ($_POST['submit']=='Find Report')
{
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
As usual, Michael701 has a good solution.  But you might not have a submit button for each form, and if that were the case you can use a hidden form field to identify the forms.

Make sure your forms do not have any overlap in the HTML.

Also, beware of name "collisions" in the form fields.  You might experiment with this script to see how the form fields arrive in the $_POST array.

Best regards, ~Ray
<?php // RAY_twoforms.php
 
// ANYTHING POSTED YET?
if (!empty($_POST))
{
    echo "YOU ENTERED '" . $_POST["form_field"] . "' IN FORM NUMBER " . $_POST["form_identifier"] . "\n";
    echo "<pre>POST: ";
    var_dump($_POST);
    echo "</pre>\n";
}
?>
<form method="post" action="<?=$PHP_SELF?>">
ENTER YOUR NAME
<input type="hidden" name="form_identifier" value="1" />
<input type="text"   name="form_field" />
</form>
 
<form method="post" action="<?=$PHP_SELF?>">
ENTER YOUR NAME
<input type="hidden" name="form_identifier" value="2" />
<input type="text"   name="form_field" />
</form>

Open in new window

0
evibesmusicAuthor Commented:
@Michael701:

Thank you for your assistance.  Your suggestion worked perfectly.

@Ray_Paseur:

Thank you very much for your suggestion on watching to make sure there are no overlaps in the HTML.  It turned out that there was so when I implemented Michael701's solution, I came across this issue.

Kudos to you both.  I'm gonna award points to you both.  Have a great day and a good weekend!

EVM

0
evibesmusicAuthor Commented:
You both are awesome!  Thanks so much!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.