0

Ok I have googled for hours and found nothing that works. I need help desperately

I have got a form with the following elements in it.

<form action="sendmail.php" method="post" name="enquiry">
    <table> 
        <tr>
            <td>Name:-</td> 
            <td><input type="text" name="name" /></td>
        </tr> 
        <tr>
            <td>Company:-</td>
            <td> <input type="text" name="company" /></td>
        </tr>
        <tr>
            <td>E-Mail:-</td>
            <td><input type="text" name="email" /></td> 
        </tr>
        <tr>
            <td>Contact:-</td>
           <td> <input type="text" name="contact" /></td>
        </tr>
        <tr>
            <td>Requirement:-</td>
            <td><textarea name="msg"></textarea></td>
        </tr>
    </table>
    <input type="submit" value="submit" onclick="return validation(this.form);">
    <input type="reset" value="Reset" />
</form>

and the php has a line like this:

<?php
if(mail($to, $subject,$message)) {
   echo "<SCRIPT LANGUAGE='JavaScript'>alert('Your Enquiry was sent successfully we  will get back to you shortly');
              window.location.href='javascript:history.go(-1)';
              document.enquiry.reset();
         </SCRIPT>";
}
?> 

Then there's an "else" with the same code but a different message.

The problem is when it redirects back to the form page it still has the data in the fields. What I prefer is a clean form with no data filled in when the user returns to the page.

Please note I am a newbie to javascript so try suggesting a fix that would be easy to comprehend.

Thanks a lot in advance!

Rimble
  • 873
  • 8
  • 22
TDsouza
  • 910
  • 2
  • 13
  • 38
  • Your reset is happening after your redirect. – Wayne Whitty Oct 25 '12 at 10:30
  • As in???? that page loads and then resets?? thought that's what should happen... how do i fix it?? sry but im still new to javascript – TDsouza Oct 25 '12 at 10:34
  • 1
    'needs help desperately' - is such language to be welcome according to SO plicy ? – Istiaque Ahmed Oct 25 '12 at 10:38
  • 1
    Apologies for the language.. I'm new to the community,promise to develop better etiquettes with time... – TDsouza Oct 25 '12 at 10:43
  • ok I just solved the issue for now I replaced the href='javascript:history.go(-1)'; document.enquiry.reset(); with a href to the form itself..however I'v also decided to take Ed Jellard's suggestion for a thank-you page.. Thanks a lot people! – TDsouza Oct 25 '12 at 10:44

6 Answers6

2

That's because you're going back in the history - browsers remember forms when you go back/forward.

What you are doing isn't a "normal" user experience - I would suggest you print out a nice looking page saying thanks in the "sendmail.php" file and then let the user navigate to wherever they want to on your site.

EdJ
  • 1,296
  • 1
  • 11
  • 16
  • 1
    +1. Added to above, just set a settimeout say 5 sec and redirect back to the form. Don't use history. Just redirect it to the url. – itachi Oct 25 '12 at 10:41
  • thanks a ton!! for now i'v placed a link back to the form instead of going back in history.. works fine!! and I also plan on working on the thank you page you suggested! – TDsouza Oct 25 '12 at 10:47
  • asking for your answer being accepted... hmm... you explained the problem well, but where is your solution? (code wise) – Rafael Herscovici Oct 25 '12 at 20:08
1

Your Problem is with the javascript code:

window.location.href='javascript:history.go(-1)';
document.enquiry.reset();

Remove this code

EDIT:

Use this code:

if(mail($to, $subject,$message)){?>
    <SCRIPT LANGUAGE='JavaScript'>
    alert('Your Enquiry was sent successfully we  will get back to you shortly');
    window.location.href='/*YOUR FORM PAGE LINK*/';
    </SCRIPT>
<?php } 
Vipin Jain
  • 1,382
  • 1
  • 10
  • 19
  • had a feeling that could be the reason.. it's all experimental code.. i wanted it to redirect back to the form after submit but for some reason everytime i'd add a url I'd get an error... what to u suggest i replace the above code with to get the same effect?? – TDsouza Oct 25 '12 at 10:38
1

as Ed said, you are going back in history, and the browser remembers it.

Try this:

instead of this line: window.location.href='javascript:history.go(-1)';

write : window.location.href='RelativePathToActualForm';

Rafael Herscovici
  • 16,558
  • 19
  • 65
  • 93
0

After processing the form post you should redirect to another page (or again to your form) and display your message there e.g.:

php header("Location: form.php?formpostwassuccessful=1");

then the form page

if(isset($_GET['formpostwassuccessful']))
  echo "Thank you. Your form was sent.";

also see redirect after post?

Community
  • 1
  • 1
cghamburg
  • 29
  • 3
0

Simple solution : just use jquery - a single line is to be usee in the head section to include jquery in your page.

The javascript snippet you gave should contain:

window.location="theform_page_address" ;

instead of

window.location.href='javascript:history.go(-1)';
              document.enquiry.reset();

and in the form page address: add the following js snippet in the head section :

<script type="text/javascript">
    $(document).ready(function(){


    $('input').val('');
    $('select').val('-1');

    $('textarea').val();

    });

</script>

EDIT: I assume your form has input and textarea elements which have a blank default value, , and select elements that have -1 as the value for the default option.

Istiaque Ahmed
  • 6,072
  • 24
  • 75
  • 141
0

First look at example of using php session-based flash messages http://mikeeverhart.net/php/session-based-flash-messages/

Usual flow:

  1. User fills in the form
  2. Presses submit
  3. Your php script sendmail.php validates the form
  4. If there were some validation errors [set error message]
  5. If there were some mail sending errors [set error message]
  6. If everything is ok [set success message]
  7. Call from php redirect to the form display page.

[set error|success message] - using the link provided above how to set the message.

Now important part every time you render your form, you should check for set messages, if there are some messages - display them.

How do those messages work? It simply sets the message to session ($_SESSION) array, and then when the message is displayed, the message is unset from session.

Mantas
  • 3,025
  • 1
  • 14
  • 9