Re: Self-Process php forms or not?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Oct 5, 2009, at 7:42 PM, Manuel Lemos wrote:

Hello,

on 10/05/2009 03:02 PM Philip Thompson said the following:
I try to avoid the use of hidden form elements as much as possible,
especially for tracking whether a user has submitted a form or not...

I use name="submit" for the submit button instead, that will pass the
value of the submit button to the action script.

above all i use a template engine, smarty to take care of the
presentation for me(like deciding whether to show the form and/or a
success/failure message)

That only works if the user clicks on that submit button. If the user hits the enter key in a text input, the form is submitted but the submit
input variable is not set. That is why an hidden input is a safer
solution.

If you need the button to be *clicked*...

<form onsubmit="$('submitButton').fireEvent('click');" ...>

Or something along those lines.

That does not make much sense and is pointless. First that syntax you
mentioned probably requires JQuery or some other large Javascript
library. something like this['submitButton'].click() would emulate the
click event. Second, by the time that onsubmit is called, the event that
triggered it was already dispatched. Emulating the click on a button
would probably fire the form submission and onsubmit code would be run
again, leading to an infinite loop sucking machine CPU.

It makes perfect sense and is not pointless. Yes, it is library- specific javascript. However, it was used to show an example and make a point. I assume that most the subscribers here are able to decipher the code and determine what the intent was. And no, this will not cause an infinite loop. The onsubmit is called first and will process whatever action you specify, and then move on. *If* the submit button wasn't *clicked* and you needed it to be, then this would emulate that functionality. I'm not saying this is the best solution on how to deal with the previous question.... but it is *a* solution.

Here's some code that you can see there's no infinite loop and shows which events are called first.

------------------
<?php
session_start();
$_SESSION['timesSubmitted'] = isset ($_POST['submitted']) ?
    $_SESSION['timesSubmitted'] + 1 : 0;
?>
<html>
<head>
    <title>loop? i don&#039;t think so</title>
</head>
<body>
<h3>Times submitted: <?php echo $_SESSION['timesSubmitted']; ?></h3>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="theForm" onsubmit="document.getElementById('submitBtn').click(); return false;">
    <input type="hidden" name="submitted" value="1" />
<input type="text" name="text" value="Focus on me and hit Enter" / ><br/> <input type="button" name="submitBtn" id="submitBtn" value="Click me to Submit" onclick="alert('I was clicked'); document.theForm.submit ();" />
</form>

<script type="text/javascript">document.theForm.text.focus();</script>
</body>
</html>
------------------

The above code works as expected in Safari 4.0.3, FF3.5.3 and IE8.

Cheers,
~Philip

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux