SitePoint Sponsor |
|
User Tag List
Results 101 to 125 of 219
Thread: PHP Quiz
-
Apr 14, 2003, 21:31 #101
- Join Date
- Jul 2002
- Location
- Dallas, TX
- Posts
- 349
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by seanf
Anyway, I really can't think of a "trivial" question, so how bout something like this :
Say i'm using a multi-purpose form. Meaning, if it were a user sign-up script, the same file would not only show the form, but would also submit to itself, and accept the data and process it (as well as redisplaying the form in the case of errors). Now, without using a header or other redirect to another page (like a "thank you", or "success" page), how could you use this same script (one file only) to keep the form from being processed if the user hits refresh?
Now I know that there is probably more than one way to do this, but I could only think of one, so let's here what others say about it. BTW, the way I use (or am about to use) keeps any form from being reprocessed (ie, duplicate data being entered into a database) when the user presses the refresh button (like all users do).
-
Apr 15, 2003, 03:11 #102
- Join Date
- Feb 2002
- Posts
- 625
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by sojomy
Well, i guess using a session variable which you set to the appropriate status would do the trick. For example, you could set it to "notPosted", whenever an error occurs, and you only set it to posted once you've inserted the input into your DB. Once placed into the database you could set another session variable which holds the time when the entriy was inserted. How to move on from here should be fairly obvious
-
Apr 15, 2003, 03:26 #103
- Join Date
- Nov 2001
- Location
- UK
- Posts
- 466
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
wouldn't it be best to set session variables equal to the value of the form fields and test against the posted values - thus allowing people to change one input slightly (eg if their email addr doessn't validate through a typo) and submit again without being blocked/ having to open a new browser session?
one mo and I'll try some code.
-
Apr 15, 2003, 03:32 #104
- Join Date
- Feb 2002
- Posts
- 625
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by pootergeist
-
Apr 15, 2003, 04:02 #105
- Join Date
- Jun 2001
- Location
- Before These Crowded Streets
- Posts
- 9,446
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Ok, so my guess is that datune got it, right?
-
Apr 15, 2003, 08:25 #106
- Join Date
- Nov 2001
- Location
- Atlanta, GA, USA
- Posts
- 5,011
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Sketch
datune, give us a question!Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?
-
Apr 15, 2003, 10:54 #107
- Join Date
- Jul 2002
- Location
- Dallas, TX
- Posts
- 349
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Sketch
I just use one session variable called $_SESSION['FormProcessed']. When the page first loads, I check to see if $_POST['Action'] is set, and if it is, is it set to "AddUser" or "SendMessage" or whatever I set the hidden action field to in the form. If the $_POST['Action'] isn't set, or isn't set to whatever I tell it to look for, I set $_SESSION['FormProcessed'] to 0 (Zero). I then display the form for the user to fill out, with the HIDDEN field named Action that has the value of whatever I want, like "AddUser" or "SendMessage"
If the $_POST['Action'] Variable is set to what I am looking for, then I know that the form was submitted. The first thing I do is check to see if $_SESSION['FormProcessed'] is 1 or 0. If it is a 1, then I know that the form has been processed, so I skip the form processing, if it is 0, then I continue with the form processing. First in the processing is to check all of the submitted data. If it fails any of the error checks, I redisplay the form with the user data, along with the appropriate error message. If it passes all of the error checks, I process the data (send the message, add a user to the database, or do whatever), and then I set the $_SESSION['FormProcessed'] variable to 1 (One), so that I won't do the form processing again, even if the user presses refresh. If there were no errors, then I continue, otherwise, I display an error message and halt. Then, if I get this far, whether I did processing this time or not, I show the "Form has been processed" message.
This might be a little hard to understand the flow without code, so here's a small demo...
PHP Code:// Form to add a new user, only a username
<? session_start(); ?>
<HTML><BODY>
<?
if (!isset($_POST['Action']) $_POST['Action'] = '';
if (!isset($_SESSION['FormProcessed']) $_SESSION['FormProcessed'] = 0;
$_POST['UserName'] = (isset($_POST['UserName']) ? $_POST['UserName'] : '';
$_POST['Password'] = (isset($_POST['Password']) ? $_POST['UserName'] : '';
$FormErrors = '';
if ($_POST['Action'] == 'AddUser') {
do {
if ($_SESSION['FormProcessed'] == 0) {
if ($_POST['UserName']=='') $FormErrors .= 'Please provide a username<BR>';
if ($_POST['Password']=='') $FormErrors .= 'Please provide a Password<BR>';
if ($FormErrors != '') break;
// insert data into the database here
// if it fails, show an error and halt
$_SESSION['FormProcessed'] = 1;
}
echo 'New user was added successfully';
} while(0);
}
if ($FormErrors != '') echo $FormErrors . '<P>';
?>
<FORM ACTION="<? echo $_SERVER['PHP_SELF']; ?>" METHOD="POST">
<INPUT TYPE=HIDDEN NAME="Action" VALUE="AddUser">
Username :
<INPUT TYPE=TEXT NAME="UserName" VALUE="<? echo htmlspecialchars(MyStripSlashes($_POST['UserName'])); ?>">
<BR>Password :
<INPUT TYPE=TEXT NAME="Password" VALUE="<? echo htmlspecialchars(MyStripSlashes($_POST['Password'])); ?>">
<BR><INPUT TYPE=SUBMIT VALUE="Add User">
</FORM>
</HTML></BODY>
-
Apr 15, 2003, 11:11 #108
- Join Date
- Feb 2002
- Posts
- 625
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I will try to come up with a question until tommorow...i can't just think of something right now.....
I hope this is ok for everyone
-
Apr 15, 2003, 11:43 #109
- Join Date
- Feb 2002
- Posts
- 625
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Alright, i got something (a lot quicker as expected, found these questions on the net...), you have to answer all three questions to ask the next one (if that's ok for everybody, if not just use one question [img]images/smilies/wink.gif[/img] )
1.) What's the name of the very first known PHP Virus ?- PHP.Virus
- dEsTR0Y
- PHP.Serious
- PHP.Pirus
- PHP2
- MySQL Database
- Perl
- Apache Server
- PHP2
- PHP
- PHP/FI 2.0
- FI
Have fun [img]images/smilies/wink.gif[/img]
-
Apr 15, 2003, 11:43 #110
- Join Date
- Jan 2003
- Location
- Calgary, Canada
- Posts
- 2,063
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
It's so hard to come up with good questions....
Who walks the stairs without a care
It shoots so high in the sky.
Bounce up and down just like a clown.
Everyone knows its Slinky.
-
Apr 15, 2003, 11:56 #111
- Join Date
- Jun 2001
- Location
- Before These Crowded Streets
- Posts
- 9,446
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
1. 2
2. Perl
3. Fi
-
Apr 15, 2003, 11:59 #112
- Join Date
- Jun 2001
- Location
- Before These Crowded Streets
- Posts
- 9,446
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I got 'em all wrong
-
Apr 15, 2003, 12:02 #113
4, 4, 3?
SeanHarry Potter
-- You lived inside my world so softly
-- Protected only by the kindness of your nature
-
Apr 15, 2003, 12:03 #114
- Join Date
- Apr 2001
- Location
- Canada
- Posts
- 5,458
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
1) 1
2) 4
3) 1Mike
It's not who I am underneath, but what I do that defines me.
-
Apr 15, 2003, 12:06 #115
- Join Date
- Feb 2002
- Posts
- 625
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
seanf, you're right. Guess it's your turn
Ps: it is indeed not easy to come up with good questions...
-
Apr 15, 2003, 12:29 #116
I'll let Aaron post his question, I've done a few
SeanHarry Potter
-- You lived inside my world so softly
-- Protected only by the kindness of your nature
-
Apr 15, 2003, 15:46 #117
- Join Date
- Aug 2001
- Location
- Livonia, MI, USA
- Posts
- 513
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I've got a question idea. I'll post it when someone gives me the ok (or if I manage to get one of these answered correctly before anyone else does).
-
Apr 16, 2003, 04:18 #118
- Join Date
- Jun 2001
- Location
- Before These Crowded Streets
- Posts
- 9,446
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Ah cool....
What are the 2 kinds of RegX formats used by PHP?
-
Apr 16, 2003, 04:23 #119
Perl Compatible Regular Expressions (PCRE) and POSIX extended - I know what they are, but I hate using them!
SeanHarry Potter
-- You lived inside my world so softly
-- Protected only by the kindness of your nature
-
Apr 16, 2003, 04:25 #120
- Join Date
- Sep 2001
- Location
- Singapore
- Posts
- 5,269
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by seanf
*Runs*
-
Apr 16, 2003, 04:26 #121
Not fast enough Joel!
Your question cyngon?
SeanHarry Potter
-- You lived inside my world so softly
-- Protected only by the kindness of your nature
-
Apr 16, 2003, 04:50 #122
- Join Date
- Jun 2001
- Location
- Before These Crowded Streets
- Posts
- 9,446
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Yep, told you it wasn't hard.
-
Apr 16, 2003, 20:26 #123
- Join Date
- Feb 2002
- Posts
- 625
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Since no questions are being posted, im gonna post one. (i hope nobody minds).
This is (i think at least), a rather tough question.
Lets say you have a multidimensional array, like soPHP Code:
$array[1] = array ('one' => 1, 'two' => 2, 'three' => 3);
$array[2] = array ('one' => 4, 'two' => 5, 'three' => 9);
$array[3] = array ('one' => 16, 'two' => 23, 'three' => 37);
$array[4] = array ('one' => 13, 'two' => 25, 'three' => 33);
$array[5] = array ('one' => 13, 'two' => 24, 'three' => 32);
$array[6] = array ('one' => 11, 'two' => 34, 'three' => 98);
$array[7] = array ('one' => 19, 'two' => 32, 'three' => 23);
$array[8] = array ('one' => 16, 'two' => 22, 'three' => 31);
So for example, array[4] and array [5] are not valid, because one is 13 on both arrays.
How do you do it ?
Have fun thinking images/smilies/wink.gifLast edited by datune; Apr 16, 2003 at 20:40.
-
Apr 16, 2003, 20:28 #124
- Join Date
- Jun 2001
- Location
- Before These Crowded Streets
- Posts
- 9,446
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
forgive my ignorance, but doesn't it already contain unique arrays? I must be missing the question.
-
Apr 16, 2003, 20:40 #125
- Join Date
- Feb 2002
- Posts
- 625
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Updated it, sorry, my mistake for not making myself all too clear...
Bookmarks