Call PHP function on click of HTML button?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

Call PHP function on click of HTML button?

Post by slipstream »

Hi all,

I want to alter my php file so when the user clicks the button, it calls my php function. Here is the code.

<?
echo 'Username <input type="text" name="username" size="14" maxlength="14"><BR>';
echo 'Password <input type="password" name="password" size="14" maxlength="14">';

echo '<input type="submit" value="Submit">'; <--Here is the button

function checkPass(){ <--How do I call this function?
if($username=="forge" && $password=="order2002") {
include ("selectDocket.php");
}
else {
echo "You have not entered a valid username/password. Remember, they are case sensitive.";
}
}
?> [b][/b]
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You have to do a check to see if the form has been submitted. If you added a hidden field to your form:

Code: Select all

&lt;input type="hidden" name="action" value="post" /&gt;
Then you can test for that variable when the page is submitted and run your function:

Code: Select all

if (isset($_POST&#1111;'action'] &amp;&amp; $_POST&#1111;'action'] == 'post') {
    checkPass();
} else {
    // show login form
}
you'll need to amend the function to something like that below in order to be able to test those posted variables:

Code: Select all

function checkPass() {
    $username = (!empty($_POST['username'])) ? $_POST['username'] : '';
    $password = (!empty($_POST['password'])) ? $_POST['password'] : '';

    if ($username == 'forge' && $password == 'order2002') { 
        include 'selectDocket.php'; 
    } else { 
        echo '<p>You have not entered a valid username/password Remember, they are case sensitive.</p>'; 
    } 
}
Mac

P.S. Please remember to put your PHP code into tags.
Galahad
Forum Contributor
Posts: 111
Joined: Fri Jun 14, 2002 5:50 pm

Post by Galahad »

PHP is all server side. As a result, it is not possible to have a button on the clients machine call a function on the server without generating a new page. Perhaps if you had a page "authenticate.php" that you send your form data to. It can just does what checkPass() does, but redirect either back to the login if it fails, or onto another page which is basically selectDocket.php.
Post Reply