php form with two action

Dear Expert,

I want to have one form with with action button:
when click button1, it process with page1.php
when click button2, it process with page2.php

<form action="" method="POST">
<Input type="text" name=text>
<input type="submit" name=page1>
<input type="submit" name=page2>
</form>

Open in new window

arkam chouAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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

Julian HansenCommented:
You can't do that - a form only has one action however what you can do is


<?php
if (isset($_GET['page1')) {
    require_once('page1.php');
}
else if (isset($_GET['page2'])) {
    require_once('page2.php');
}
else {
    die("Unsupported request");
}

Open in new window

Save that as form_handler.php and point your form to this

<form action="form_handler.php" method="POST">
<Input type="text" name=text>
<input type="submit" name=page1>
<input type="submit" name=page2>
</form>

Open in new window

arkam chouAuthor Commented:
Dear julianH,

can I use "form_handler.php" in same page with html form?

ex: click button1, it will process code in same page
     click button2, it will direct to "page2.php"

Thanks,
Rada
arkam chouAuthor Commented:
Hello are my code:

<form action="" method="POST">
<?php
$connection = mysqli_connect("localhost", "root", "", "database") or die("message");
$query = "select brandID,brandNameEN from tblbrand order by brandNameEN";
$result = mysqli_query($connection, $query);
    if (mysqli_num_rows($result) > 0) {
        echo "<select name=brand id=brand>";
        while ($row = mysqli_fetch_assoc($result)) {
            echo "<option value=" . $row[brandID] . " ".($val==$row[brandID]?"selected":"").">";
            echo $row["brandNameEN"];
            echo "</option>";
        }
        echo "</select>";
    } else {
        echo "Null";
    }
?>
<input type="submit" name="submit1" value="View"/>
<input type="submit" name="ToExcel" value="ToExcel"/>
 <?php
            if (isset($_POST['submit1'])) {
                $year = $_POST['year'];
                $month = $_POST['month'];

                $connection = mysqli_connect("localhost", "root", "", "database") or die("message");
                $query3 = "select year, month, brandNameEN, adNameEN, spendName, price, numberofSpend from tbldataentry inner join tblbrand on tbldataentry.brandID=tblbrand.brandID "
                        . "inner join tblad on tbldataentry.adID=tblad.adID inner join tblspendtype on tbldataentry.spendid=tblspendtype.spendid where year='$year' and month='$month'";
                $result3 = mysqli_query($connection, $query3);
                if (mysqli_num_rows($result3) > 0) {
                    echo "<br><br>";
                    echo "<table class='table-fill'>";
                    echo "<thead><tr><th class='text-left'>Year</th><th class='text-left'>Month</th><th class='text-left'>Brand Name</th><th class='text-left'>AD</th><th class='text-left'>Type of Spend</th><th class='text-left'>Price</th><th class='text-left'>Number of Spend</th><tr></thead>";
                    echo "<tbody class='table-hover'>";
                    while ($row = mysqli_fetch_assoc($result3)) {
                        echo "<tr><td class='text-left'>" . $row["year"] . "</td>";
                        echo "<td class='text-left'>" . $row["month"] . "</td>";
                        echo "<td class='text-left'>" . $row["brandNameEN"] . "</td>";
                        echo "<td class='text-left'>" . $row["adNameEN"] . "</td>";
                        echo "<td class='text-left'>" . $row["spendName"] . "</td>";
                        echo "<td class='text-right'>" . $row["price"] . "</td>";
                        echo "<td class='text-right'>" . $row["numberofSpend"] . "</td>";
                        echo "</td>";
                    }
                    echo "<tbody>";
                    echo "</table>";
                                }
            }
            if (isset($_POST['ToExcel'])) {
                require_once 'excel.php';
            }
             ?>
</form>

Open in new window

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Julian HansenCommented:
can I use "form_handler.php" in same page with html form?
I don't understand what you are asking.

When you submit a form it is sumbitted to wherever you are pointing the action attribute (or to the page that generated the form).

Those are the only options and it can be only one.

If you want to have different outcomes based on different buttons pressed you have to do that on the server. You don't have to do it exactly the way I have done above - but you will need some logic that looks at the variables posted and based on those include whatever code is required to handle the request.

Just note a correction in my earlier post - your form is POST''ing to the handler - in my code I used $_GET - I should have used $_POST.
Ray PaseurCommented:
You may want to redesign the application.  Use two separate forms.  HTML forms have a one-to-one relationship with action scripts.

If you're new to PHP web development and you would like to find your way to some good learning resources, this article can help.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
arkam chouAuthor Commented:
Dear julianH,

I want my form have two action
click button1: is get data from mysql and display on webpage
click button2: is generatedata to excel

below is the page code for button2
<?php
/** Include PHPExcel */
require_once dirname(__FILE__) . '\Classes\PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle($name);
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;

Open in new window


here is the link of PHPExcel for download: http://phpexcel.codeplex.com/
Julian HansenCommented:
I want my form have two action

I understand the requirement. The above posts outline what your options are.

The latest code snippet for button 2 does not appear to use any of the form variables - can you not just create a link styled as a button to point to that page?
Tony ChristopherLead ProgrammerCommented:
You can do this using JavaScript. What you need to do is add an ID to your form and buttons and then use jQuery to detect the buttons being pressed, change the form action and then submit the form. Try this:

<form action="" method="POST" id="myForm">
    <Input type="text" name=text>
    <input type="button" name="page1" id="btn1>
    <input type="button" name="page2" id="btn2">
</form>

<script>
$('#btn1').click(function() {
    $('#myForm').prop('action', 'page1.php').submit();
});
$('#btn2').click(function() {
    $('#myForm').prop('action', 'page2.php').submit();
});
</script>

Open in new window

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
arkam chouAuthor Commented:
Thanks you all, now it work with solution of expert Tony Christopher.
arkam chouAuthor Commented:
oh, sorry all for this misunderstanding. How can I change to A?
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.