-1

I thought it would be easy (silly, I know) and so I followed the instructions I found at this site for a PHP email form: http://www.html-form-guide.com/email-form/dreamweaver-email-form.html I didn't need help with the Dreamweaver interface at all, I just needed the script, and I knew (or thought I knew) how to adapt their simple form into the form I needed, and adapt the script accordingly.

The form in question may be found at this URL: http://nineinchbride.com/SuitedForWar_BookTwo_PreOrderForm.php

The code as it presently exists on the page is as follows:

<form id="PreOrder_Book_2" name="PreOrder_Book_2" method="post" action="">
          <p>Your Name:
            <input type="text" name="CustomerName" id="CustomerName" />
          </p>
          <p>Your Email:&nbsp;
            <input type="text" name="CustomerEmail" id="CustomerEmail" />
          </p>
          <p>
            <input type="checkbox" name="NotifyPaperback" id="NotifyPaperback" />
          Notify me when paperback is available.</p>
          <p>
            <input type="checkbox" name="Notify_eBook" id="Notify_eBook" />
          Notify me when eBook is available.</p>
          <p>Desired eBook Format:</p>
          <p>
            <label>
              <input type="radio" name="eBookFormats" value=".mobi" id="eBookFormats_0" />
              .mobi (Kindle)</label>
            <br />
            <label>
              <input type="radio" name="eBookFormats" value=".epub" id="eBookFormats_1" />
              .epub (Nook / Ipad / Sony / Kobo)</label>
            <br />
            <label>
              <input type="radio" name="eBookFormats" value=".pdf" id="eBookFormats_2" />
              .pdf (All readers)</label>
            </p>
          <p>
            <input type="submit" name="button" id="button" value="Submit" />
            <br />
          </p>
        </form><script>
        function validateForm()
{
    var name=document.forms["PreOrder_Book_2"]["CustomerName"].value;
    if (name==null || name=="")
    {
        alert("Name cannot be left blank.");
        return false;
    }

    var z=document.forms["PreOrder_Book_2"]["CustomerEmail"].value;
    if (z==null || z=="")
      {
      alert("Please enter your email.");
      return false;
      }
}

</script>
<script><?php
if(!isset($_POST['submit']))
{
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the form!";
}
$name = $_POST['CustomerName'];
$visitor_email = $_POST['CustomerEmail'];
$message = $_POST['NotifyPaperback'];
$message = $_POST['Notify_eBook'];
$message = $_POST['eBookFormats'];

//Validate first
if(empty($name)||empty($visitor_email)) 
{
    echo "Name and email are mandatory!";
    exit;
}

$email_from = 'webmanager@nineinchbride.com';//<== Put your email address here
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
    "email address: $visitor_email\n".
    "Here is the message:\n $message".

$to = "webmanager@nineinchbride.com";//<== Put your email address here
$headers = "From: $email_from \r\n";

//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: http://nineinchbride.com');

?></script>

Please bear in mind that while I know my way around code a bit (enough to adapt things a little, adjust naming for consistency and the like), I am not a programmer per se, so please go easy on me.


Update 1:

Okay, I'm making progress here. I made the following changes to the PHP:

$name = $_POST['CustomerName'];
$visitor_email = $_POST['CustomerEmail'];
$message1 = $_POST['NotifyPaperback'];
$message2 = $_POST['Notify_eBook'];
$message3 = $_POST['eBookFormats'];

//Validate first
if(empty($name)||empty($visitor_email)) 
{
    echo "Name and email are mandatory!";
    exit;
}

$email_from = 'webmanager@nineinchbride.com';//<== Put your email address here
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
    "email address: $visitor_email\n".
    "Notify When Paperback Is Available: $message1\n".
    "Notify When eBook Is Available: $message2\n".
    "My eBook Format Is: $message3\n".

And, hurray, I'm getting all the form data. Figured it out for myself too ;-)

However, none of the validation is working. And the re-direct after successful submission is not working either. Any idea what's up with that?


Update 2:

Wow, validation problem solved, thank you Poria! I just added

<input type="button" name="button" id="button" value="Submit" onclick="return validateForm();"/>

to the form itself in place of the submit button I had before, and now the front end validation is working. Great!

But now the form itself does not submit any longer! What did I do wrong?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • What is the problem ? Not sending emails ? – Pardeep Poria May 11 '16 at 06:40
  • I'm getting 'an email' all right, that's not the problem. The problem is that the checkbox data and/or the radio button data is not coming through. All I get is the sender's name and email. Thanks. – user6318597 May 11 '16 at 06:54
  • I think the problem is here: $message = $_POST['NotifyPaperback']; $message = $_POST['Notify_eBook']; $message = $_POST['eBookFormats']; But I don't know how to fix it. – user6318597 May 11 '16 at 06:58
  • $message = $_POST['NotifyPaperback']; $message .= $_POST['Notify_eBook']; $message .= $_POST['eBookFormats']; See the dots for concatenation used before "=" sign – Pardeep Poria May 11 '16 at 07:21
  • use a flag for validation flag = true; and inside your ifs set flag = true/false as per validation rule and the return true/false at the end of validation function as per your flag value – Pardeep Poria May 11 '16 at 07:50
  • Poria, thanks, but you'd need to 'show me,' as I'm not far enough along to understand what you're asking me to change. – user6318597 May 11 '16 at 07:55
  • http://stackoverflow.com/questions/15060292/a-simple-jquery-form-validation-script try this – Pardeep Poria May 11 '16 at 08:03
  • Also you are not calling the javascript function anywhere in your code , you need to Also dont forget to use input type button instead of input type submit – Pardeep Poria May 11 '16 at 08:05
  • Wonderful, thank you Poria for spelling it out for me! Works like a charm. Only problem left is the redirect after submission: that's still not working. – user6318597 May 11 '16 at 08:22
  • You mean you are not able to submit the form after validation check ? – Pardeep Poria May 11 '16 at 08:25
  • This isn't supposed to be debugging help. It's supposed to be one question per post and then rather than change the question, accept the answer to the first question (ask @Poria to post it as an answer) and post a new question. – Elin May 11 '16 at 08:27
  • Yes, Poria, that is what I mean. Now the form itself does not submit any longer! What did I do wrong? Also, the page should redirect to a page other than the form page. And there is another viewer getting cranky about this long discussion, can you email me directly? see email in the form. Thanks. – user6318597 May 11 '16 at 08:31
  • your form action is empty action="" so problem is this one – JYoThI May 11 '16 at 08:35
  • Hi there. Progress updates are fine in questions, but not at the start - please keep updates to the end, as they are confusing for readers who did not see the original. Thanks! – halfer May 11 '16 at 08:39
  • @user6318597 please accept the answer as you said in comments it is working. – Pardeep Poria May 11 '16 at 08:54

1 Answers1

1

Your first mistake is

$message = $_POST['NotifyPaperback'];
$message = $_POST['Notify_eBook'];
$message = $_POST['eBookFormats'];

change it to

$message = $_POST['NotifyPaperback'];
$message .= $_POST['Notify_eBook'];
$message .= $_POST['eBookFormats'];

not the dot(.) for concatenation.

Secondly you never called the validation function

change the button like this

<input type="button" name="submit" id="button" value="Submit"  onclick="return validateForm();"/>

Now form will be submit.

Hope it helps!

For further questions please post another question.

Ashish Chaturvedi
  • 1,352
  • 3
  • 13
  • 37
Pardeep Poria
  • 1,049
  • 1
  • 7
  • 12