Form won't stop submitting

jonofat
jonofat used Ask the Experts™
on
It appears that this form keeps submitting or something? The form doesn't redirect anywhere after submitting. I must just stay on the same page but it seems like it just keeps going. How do I stop it?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
do you have an action on the form? if not it will submit back to the same page and then it will not go anywhere.
hi,
maybe you have
<body onload='document.yourform.submit();'>

Open in new window

on the page?
if there is no action then the form submits to the same page. But, still it should not keep on submitting itself again and again unless triggered.

Please show the code that would provide some more insight into the problem.
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

can we see the page ?? or html source code from your form page and result page
Most Valuable Expert 2011
Top Expert 2016

Commented:
Please post the URL of the page that is giving you trouble.  Please post the script code that is giving you trouble.  Thanks, ~Ray

Author

Commented:
There is an action on the form that I can't remove as I need it. All I did was make it post to another page. Unless anyone can give me a reason why this could be a problem, I am happy to leave it like that and I get the result I want. FYI though, code is..

<form action="<?php echo $editFormAction; ?>" id="form1" name="form1" method="POST">
          <table width="65%" border="0" cellspacing="2" cellpadding="2">
            <tr>
              <td width="9%">&nbsp;</td>
              <td bgcolor="#D71820"><span class="headingtext"><strong>FORGOTTEN PASSWORD
                
                
                    <input name="token" type="hidden" id="token" value="<?php echo md5(mt_rand()); ?>" />
                    <input name="customerid" type="hidden" id="customerid" value="<?php echo $row_Recordset1['customerid']; ?>" />
              </strong></span></td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td class="headingtext">If that email address is registered with us, you should receive an email shortly. </td>
              </tr>
          </table>
          <input type="hidden" name="MM_update" value="form1" />
        </form>

Open in new window

Most Valuable Expert 2011
Top Expert 2016

Commented:
Look at "view source" of this page.  My guess is that the $editFormAction variable is undefined and you will see something like this:

<form action="" id="form1" name="form1" method="POST">

If the action= attribute of the form tag is empty the script will GET or POST to its own URL.
The page more than likely is POSTing to itself.  The script behind it is probably checking for $_POST, and doing a redirect back to itself in an attempt to clear out those POST variables (sometimes done so the user can refresh the page without getting the message about having to re-POST data).

If all that is true, the page is POSTing to itself but $_POST still has data, so it continues to refresh (each time causing another POST).

Make certain that the $editFormAction variable is defined, as Ray suggested.  In the code, you can add (in the HTML, NOT the PHP part) something like "?POST=y" so it refreshes to a different URL.

Author

Commented:
Sorry for the delay in responding...

I have decided to make it go to another page so it isn't really an issue anymore. The issue is that the form is meant to submit on it's own and then redirect. The form submission is meant to update a record in the database but it doesn't. So, the form appears to be submitting and it redirects, but it isn't actually submitting?

<form action="<?php echo $editFormAction; ?>" id="form1" name="form1" method="POST">
          <table width="65%" border="0" align="center" cellpadding="2" cellspacing="2">
            <tr>
              <td width="9%">&nbsp;</td>
              <td bgcolor="#D71820"><span class="headingtext">FORGOTTEN PASSWORD<strong>
<input name="token" type="hidden" id="token" value="<?php echo md5(mt_rand()); ?>" />
                    <input name="customerid" type="hidden" id="customerid" value="<?php echo $row_Recordset1['customerid']; ?>" />
              </strong></span></td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td class="headingtext">If that email address is registered with us, you should receive an email shortly. </td>
              </tr>
          </table>
          <input type="hidden" name="MM_update" value="form1" />
        </form>
        <script>
document.form1.submit();
</script>

Open in new window

Most Valuable Expert 2011
Top Expert 2016
Commented:
the form is meant to submit on it's own and then redirect

There is no technically competent design pattern that I know of that would do something like that.  When a form is submitted, it makes an HTTP request to a server with the GET or POST data.  The server responds, but the form script on the client machine has finished its work when the HTTP request was made.  So the "redirect" occurred at the time the form was submitted, not after the form was submitted.

If you know all the information to fill in the form using hidden form tags, it doesn't make any sense to put this information into a form.  Just use the variables directly.  You have $row_Recordset1['customerid'] set in a PHP variable, right?  Then just make a query to find the password and send the email.  The part about submitting a form is unnecessary and complicates the process without adding any value.

Author

Commented:
Okay, but if I don't submit a form, how do I update the records in the database then? The purpose of this isn't just to send an email. I need to update a record in the database and send an email.
Most Valuable Expert 2011
Top Expert 2016

Commented:
If I have not recommended this book before, it is my omission.  I apologize and I am glad to recommend it now.  From this question I can tell that you are floundering a little in some of the basics of how web applications work, and this book will really help you get some of the foundation you need.   It is very readable with great examples and has been a part of my professional library since its first edition, several years ago.
http://www.sitepoint.com/books/phpmysql4/

Submitting a form has nothing to do with updating a data base.  You may find these elements of programming together quite often, but they are not intrinsically linked in any way.  Since you have got a variable that identifies a row in the data base like $row_Recordset1['customerid'] you can use this variable in an update query, something like this code snippet (substituting your specific information for anything that starts like my_).

Anyway, this question has been going on for quite some time now.  Earlier I had asked for the URL and the script and since you did not post either of these, except for a little HTML, I will sign off on the question now.  I urge you to buy that book and work through the examples and exercises.  It will not make you a pro, but it will be enjoyable and you will come out of the learning process well ahead.

Best of luck going forward, ~Ray
$c = mysql_real_escape_string($row_Recordset1['customerid']);
$q = "UPDATE my_table SET my_column='$my_value' WHERE my_customerid='$c' LIMIT 1;
$r = mysql_query($q) or die( mysql_error() );
mail ($my_email, $my_subject, $my_message);

Open in new window

Author

Commented:
Ray, before your response I managed to sort it out. On the same page the user enters their email, when they click submit I have the random key update in the database and an email is sent all on the same page. I just wasn't thinking it through properly. I did take your advice a long time ago and I did purchase that book in pdf format already. I just honestly haven't had time to go through it properly. I am trying to do this and record my new album at the same time. Maybe I can teach you something about sound engineering?? :P

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial