Closed Thread Icon

Preserved Topic: PHP, forms and a 'reoccuring' problem Pages that link to <a href="https://ozoneasylum.com/backlink?for=12110" title="Pages that link to Preserved Topic: PHP, forms and a &amp;#039;reoccuring&amp;#039; problem" rel="nofollow" >Preserved Topic: PHP, forms and a &#039;reoccuring&#039; problem\

 
Author Thread
mobrul
Bipolar (III) Inmate

From:
Insane since: Aug 2000

posted posted 03-14-2002 19:22

I have this form that allows someone to update a database.
I use the
<form method='post' action="<?php echo $PHP_SELF ?>">
method of posting the form.
I use a code like this:
<?php
if ($submit) {
[do sql stuff here]
}
?>
in the page to check if the submit button has been pressed and, if it has, to process the appropriate sql statements...
All this works wonderfully.

The only problem I now have comes after the user enters an entry. If the user refreshes the page, the sql statement is repeated, leading to row after row after row of the same damn entry.
Is there a way to stop this??

Pardon my ignorance, please. I'm VERY new to this PHP language...tried looking at www.php.net, but couldn't find anything that looked helpful.

Thank you
mobrul


mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 03-14-2002 19:51

Simply redirect browser to some other location after you finish with your SQL stuff...


GRUMBLE
Paranoid (IV) Mad Scientist

From: Omicron Persei 8
Insane since: Oct 2000

posted posted 03-14-2002 19:52

if i understood you correctly, i have to say, that this is a feature (or bug) of the browser, and i cant think of a way to avoid this.

maybe someone else?
what about headers?

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 03-14-2002 21:14

That's not a bug. By refreshing the page, the script runs again, thus creating your problem.

Like Max said - redirect.

In fact, this reminds me of a humorous event. I had a similar problem for a content management system, but mine was caused by a META refresh tag. If the user left the page on a certain page, and walked away, the page would refresh every 10 minutes. And this particular page would enter data into a MySQL database. Two days later...... you get the idea.



[This message has been edited by Pugzly (edited 03-14-2002).]

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 03-14-2002 23:09

Yeah what I like to do with my forms that submit to a db I usually just use one page that calls itself which includes different HTML pages depending on what it has done.

something like so:

if ($action == "submit") {
validate submitted data, do SQL check that it worked. If anything fails then I set a variable called $error to report what errors have occured. If everything goes well then
include('/confirmation page.inc');
} else {
Include the HTML form (that's where I'll print that $errors var);
}


The action variable is either hidden in the form or usually just the name of my submit button.

make any sense here?



.:[ The Tao of Steve ]:.
Be Desireless
Be Excellent
Be Gone
...................................

mobrul
Bipolar (III) Inmate

From:
Insane since: Aug 2000

posted posted 03-15-2002 01:38

Absolutely that makes sense...unfortunately.
I had a feeling someone was going to come back here and call this a 'feature'. :P
Well, I really don't want to redirect...there are lots of things one can choose to do from this one page:
-add some stuff
-edit some stuff
-change one particular field (same as edit, really, but this one field will be used 90% of the time so it gets its own special place)
-delete some stuff.

Someone might want to add something, then edit something else, then delete something else...

So, I don't want to redirect...but I guess I'll have to. Hmmm...

Thanks for your help. I really appreciate it. I'm diving into this PHP thing for the first time so you can probably expect me here a little more often in the near future.

mobrul

Emperor
Maniac (V) Mad Scientist with Finglongers

From: Cell 53, East Wing
Insane since: Jul 2001

posted posted 03-15-2002 02:12

mobrul: There is no need to redirect if you do what bitdamaged suggests. Just off the top of my head (i.e. very crudely) try something like this (pos. some problem in there but I think it should work - if it doesn't someone will jump in and point out my error!!):

if ($edit_entry) {
Do edit things to DB
THANKS FOR EDITING ENTRY
LINK back to $PHP_SELF to perform other actions
}
elseif ($section == "edit") {
BLAH FORMS TO EDIT
<input name="edit_entry" value="Edit" />
}
elseif ($add_entry) {
Do add things to DB
THANKS FOR ADDING ENTRY
LINK back to $PHP_SELF to perform other actions
}
elseif ($section == "add") {
BLAH FORMS TO EDIT
<input name="add_entry" value="Add" />
}
else {
LINKS:
$PHP_SELF?section=add
$PHP_SELF?section=edit
}

or some other variation (you could have the options to do more tasks after the thanks for example).

Emps

mobrul
Bipolar (III) Inmate

From:
Insane since: Aug 2000

posted posted 03-15-2002 14:42

Hey, that's pretty smooth, Emps...why didn't I think of that?
I was trying to handle all the different options by displaying or not displaying the appropriate layer/form via Javascript...and used the php only to write sql statements based on which 'submit' button the user clicked.
Your way seems a lot more intuitive...and much better suited for my particular situation. Damn, Emps, you're good. You too bitdamaged! All of you, great!
Thanks for the help.
mobrul

« BackwardsOnwards »

Show Forum Drop Down Menu