+ Post New Thread
Results 1 to 6 of 6
Coding Thread, javascript and php mail form in Coding and Web Development; Hi, Im currently coding up a form that uses php mail command to send the information to someones email address. ...
  1. #1
    mikeglover's Avatar
    Join Date
    Mar 2009
    Location
    Northampton
    Posts
    149
    Thank Post
    33
    Thanked 30 Times in 9 Posts
    Rep Power
    16

    javascript and php mail form

    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?

    Code:
    ...
    <script type="text/javascript">
    
    function confSubmit(form) {
    if (confirm("Are you sure you want to submit the form?")) 
    {
    form.submit();
    }
    
    else {
    alert("You decided to not submit the form!");
    }
    }
    </script>
    
    ...
    
    <form method="POST" action="///php file location, changed for security reasons">
    
    ...
    
    <input type="button" onClick="confSubmit(this.form);" value="Submit Form">
    </form>
    This is the php part, its unchanged from when it was just a simple Submit form.

    Code:
    <?php
    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,

  2. #2
    ajb
    ajb is offline
    ajb's Avatar
    Join Date
    Jan 2010
    Location
    East Midlands
    Posts
    6
    Thank Post
    0
    Thanked 2 Times in 2 Posts
    Rep Power
    0
    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:

    PHP Code:
    echo print_r($_POSTTRUE);
    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.
    Last edited by ajb; 18th January 2010 at 10:40 AM. Reason: Continuation

  3. #3
    mikeglover's Avatar
    Join Date
    Mar 2009
    Location
    Northampton
    Posts
    149
    Thank Post
    33
    Thanked 30 Times in 9 Posts
    Rep Power
    16
    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.

  4. #4
    ajb
    ajb is offline
    ajb's Avatar
    Join Date
    Jan 2010
    Location
    East Midlands
    Posts
    6
    Thank Post
    0
    Thanked 2 Times in 2 Posts
    Rep Power
    0
    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...

    HTML Code:
     <form action="sendmail.php" method="post">
          <label>To</label><br />
          <input type="text" name="to" /><br />
          <label>Subject</label><br />
          <input type="text" name="subject" /><br />
          <label>Body</label><br />
          <textarea name="body"></textarea><br />
          <input type="button" onclick="confSubmit(this.form);" value="Submit" />
        </form>
    Don't forget to validate the data held in these fields before using them in the call to mail().

    Ta, Andy.

  5. #5
    mikeglover's Avatar
    Join Date
    Mar 2009
    Location
    Northampton
    Posts
    149
    Thank Post
    33
    Thanked 30 Times in 9 Posts
    Rep Power
    16
    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.

  6. #6
    ajb
    ajb is offline
    ajb's Avatar
    Join Date
    Jan 2010
    Location
    East Midlands
    Posts
    6
    Thank Post
    0
    Thanked 2 Times in 2 Posts
    Rep Power
    0
    I'm slightly perplexed actually.

    I first tried adding a name attribute of "submit" to the <input type="button" ... tag for the Submit button, but that didn't work.

    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.

    HTML Code:
    <input type="hidden" name="_submit" />
    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'].

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

    Let me know how you fair.

SHARE:
+ Post New Thread

Similar Threads

  1. PHP Mail
    By karldenton in forum Web Development
    Replies: 10
    Last Post: 15th May 2009, 07:25 PM
  2. [PHP] PHP Form Generator
    By _Bat_ in forum Web Development
    Replies: 1
    Last Post: 25th March 2009, 11:39 AM
  3. PHP form validation
    By thesk8rjesus in forum Web Development
    Replies: 10
    Last Post: 21st January 2009, 12:09 AM
  4. Mail form error
    By carolla in forum EduGeek Joomla 1.0 Package
    Replies: 1
    Last Post: 7th March 2008, 02:56 PM
  5. PHP or JavaScript password obfuscator
    By ajbritton in forum Web Development
    Replies: 2
    Last Post: 18th October 2006, 08:30 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •