Page 1 of 1

Call PHP function on click of HTML button?

Posted: Tue Jun 17, 2003 12:19 pm
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]

Posted: Tue Jun 17, 2003 12:44 pm
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.

Posted: Tue Jun 17, 2003 12:45 pm
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.