can i use submit button inside a form to access a function in other php file?because i have 2 buttons on same page but want them to do different function..one to query database other to update table..

Recommended Answers

All 2 Replies

Yes, you can use multiple submit buttons in your forms:

submit buttons: When activated, a submit button submits a form. A form may contain more than one submit button.

and:

If a form contains more than one submit button, only the activated submit button is successful.

source: http://www.w3.org/TR/html401/interact/forms.html

to access a function in other php file?

Use the action attribute of the form to point a switcher file, there you can redirect or just use include() to perform the pre-selected operation.

Here is an example:

<?php

    $print = '';
    $action = '';

    if($_POST)
    {
        $print = $_POST;

        if(array_key_exists('query', $_POST))
        {
            // do query
            $action = 'query';
        }

        if(array_key_exists('edit', $_POST))
        {
            // edit action
            $action = 'edit';
        }
    }

?>
<!DOCTYPE html>
<html>
<head>
    <title>Yet Another Form</title>
</head>
<body>

    <form method="post" action="">
        <input type="text" name="msg" />
        <input type="submit" name="query" value="search" />
        <input type="submit" name="edit" value="edit" />
    </form>

    <pre><code>
        <?php
            print_r($print);
        ?>
    </code></pre>

    <p>
        <?php
            echo $action;
        ?>
    </p>

</body>
</html>

Bye!

Member Avatar for iamthwee

You can either use two forms or you can use jquery to single out your submit functions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.