Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onClick Javascript equivalent for Php? 2

Status
Not open for further replies.

AcidReignX

Programmer
Feb 28, 2005
32
US
Hi,
I've been searching for a better method of having my form events handled when I press a form button, but it has been of relatively little success. All my searches turn up a javascript solution (onClick) or something that doesn't help me. I was under the impression that something like:

$showform = "<form action=\"$PHP_SELF\">Name: <input type=\"text\" name=\"fusername\"><br>";
$showform .= "Password: <input type=\"text\" name=\"fpassword\"><br>";
$showform .= "<input type=\"button\" name=\"fsubmit\" value=\"Submit\"></form>";

echo $showform;

if($fsubmit) { *CODE FOLLOWS* }

would work, but it doesn't - or, if it does, something is drasticaly wrong with my current setup. Is there an alternative if statement that would ideally work like the "if($fsubmut)" that I have posted where I can just have it check if the button has been pressed?

Or if not, is there a way I can have a particular script run (such as having the form action not change pages, but run a specific function)?
 
Try:
Code:
if($_GET['fsubmit']) { *CODE* }
This looks like a register globals problem.
 
You want to do something like this:
Code:
<?
if (!isset($_POST['fsubmit'])) {// submit button not pressed, display form
    $tmp = array();
    $tmp[] = '<form action="' . $_SERVER['PHP_SELF'] .'" method = "POST">';
    $tmp[] = 'Name: <input type="text" name="fusername">';
    $tmp[] = 'Password: <input type="password" name="fpassword">';
    $tmp[] = '<input type="submit" name="fsubmit" value="Submit">';
    $tmp[] = '</form>';
    echo implode("<br>\n",$tmp)."<br>\n"; }
else { // submit button pressed
    // Process the inputs from your form using $_POST['fusername'] and $_POST['fpassword']
    // }
?>

Just a few comments:

[li]You were assuming that register_globals was on. The default is now off[/li]
[li]You don't have to either display the form or process it. You could check to see if the submit is pressed before displaying the form and check for errors and redisplay the form indicating any errors.[/li]


Ken
 
Ahh.. a register globals problem. Hrumph. Now I feel stupid.

Thanks for the input, guys

Vragabond, I wasn't able to get that method to work for my particular setup, although it *did* work when I changed the input type from 'button' to 'submit' which I suspect you probably assumed was the case anyways because it's the more logical approach to things.

Ken, that worked excellently :D Thanks much.
 
One other thing to keep in mind ...

If the user submits the form by pressing the enter key, then the submit value wil not be sent. I encounter this a lot with log-in forms. Users, like myself, enter the username, tab to the password field, enter the password and then hit the Enter key - never actually clicking the submit button.

So when I look for the $submit key in the Post array - it doesn't exist.

You could create a hidden field instead and look for that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top