Jump to content

Recommended Posts

Posted

Hi,

 

Im currently coding up a form that uses php mail command to send the information to someones email address.

 

I have two files html file (with the form) and php file that grabs the information via POST and mails it.

Everything was working fine until I needed to add a bit of Javascipt that brings up a 'Are you sure you want to send this?' prompt.

 

Here is parts of html file?

 

...
<br />
<br />
function confSubmit(form) {<br />
if (confirm("Are you sure you want to submit the form?")) <br />
{<br />
form.submit();<br />
}<br />
<br />
else {<br />
alert("You decided to not submit the form!");<br />
}<br />
}<br />


...

</pre><form method="POST" action="///php%20file%20location,%20changed%20for%20security%20reasons">

...


</form><b

 

This is the php part, its unchanged from when it was just a simple Submit form.

 

if(isset($_POST['submit'])) {

...

mail($to, $subject, $body);

} else {

echo "Oops something went wrong! See Mike in IT Office";

}
?>

 

I know that something needs to changing in the php code where "if(isset($_POST['submit']))" is but I'm not sure what to change it to.

 

Any help would be appreciated. :)

 

Thanks,

Posted (edited)

Hi Mike,

 

What was the original purpose of the call to isset()?

 

Also, I very much doubt that the variable $_POST['submit'] is actually set to anything any longer.

 

To quickly check which values are held in $_POST you can insert the following (to debug) preferably towards the very top of your PHP script:

 

echo print_r($_POST, TRUE);
exit(1);

 

If you remove the if statement and simply call mail() it should work correctly.

 

Ta, Andy.

 

P.S. How do you specify the to, subject, and body variables both in your HTML form and their assignment within your PHP script? You don't show them being assigned.

Edited by ajb
Continuation
Posted

Original purpose of isset() was that the form had a standard submit button at the bottom.

 

I'll try and removing the if statement and see if that works.

 

Thanks for your help.

Posted

When using JavaScript and a button element rather than an input element of type 'submit', the action is slightly different. There is no $_POST['submit'] variable created.

 

Have you used name="..." attributes on your form input elements so that you can reference them $_POST['to'], $_POST['subject'], $_POST['body'], etc.?

 

Something like...

 

   </pre><form action="sendmail.php" method="post">
     To

     

     Subject

     

     Body

     

     
   </for

 

Don't forget to validate the data held in these fields before using them in the call to mail().

 

Ta, Andy.

Posted

yes thats what I've done.

 

If no $_POST['submit'] variable is created. Then do you know what is needed for javascript button. If not I might just leave out the if statement. Only reason for if statement was that if something went wrong it stopped and outputed a message to the user saying something went wrong and to contact me.

Posted

I'm slightly perplexed actually.

 

I first tried adding a name attribute of "submit" to the

 

I then tried to add a hidden input field, again with a name attribute of "submit". Again, no joy.

 

Finally, I altered the name attribute to "_submit" (or something other than "submit") and all works well now.

 


 

You must, of course, also make amendments to your PHP file so that the isset() function checks for the existence of the $_POST['_submit'] variable, not $_POST['submit'].

 

if (isset($_POST['_submit'])) {
   // ...
} else {
   // ...
}

 

Let me know how you fair.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...