Contact form

I hope someone could help me on this. Everything is working well, from filling up the html form, sending for validation and redirect to thank you form of my website, and e-mail is successfully received, with the data validated except for the choices in the check boxes which are never shown in the e-mail.

Here is my HTML code:

<form method="POST"
action="mydomain/send.php">
<input type="hidden" name="mailto"
value="myemail@mydomain.com"><!-- ...  -->
<input type="hidden" name="thanksurl"
value="mydomain/thankyouform.html"><!--
... -->

          <p><font color="#0000FF"><b>Please fill-up this form to send us your inquiry</b></font></p>

                <br />

                <table cellpadding="2" cellspacing="0" border="1" bgcolor="#dbcbb2">
                  <tr valign="top">
                    <td><font face="Verdana" size="2" color="#000000"> Name </font></td>
                    <td><input type="text" name="Name" size="30"></td>
        	  </tr>
                  <tr valign="top">
                    <td><font face="Verdana" size="2" color="#000000"> Address </font></td>
                    <td><input type="text" name="Address" size="30"> </td>
                  </tr>
                  <tr valign="top">
                    <td><font face="Verdana" size="2" color="#000000"> Email </font></td>
                    <td><input type="text" name="E-Mail Address" size="30"> </td>
                  </tr>
                  <tr valign="top">
                    <td><font face="Verdana" size="2" color="#000000">Interested in </font></td>
                    <td><input type="text" name="Interested in" size="30" value="Tuscania CDO" readonly="readonly"></td>
                  </tr>
                  <tr valign="top">
                  <td><font face="Verdana" size="2" color="#000000"> Click Box of <br /> Chosen Unit</font></td>
                  <td>
                    <div align="left">
                      <input type="checkbox" name="option1" value="Amalfi"> Amalfi
                      <input type="checkbox" name="option2" value="Capri"> Capri
                      <input type="checkbox" name="option3" value="Genova"> Genova
                      <input type="checkbox" name="option4" value="Verona I"> Verona I <br />
                      <input type="checkbox" name="option5" value="Verona II"> Verona II
                      <input type="checkbox" name="option6" value="Treviso"> Treviso
                      <input type="checkbox" name="option7" value="Special Design"> Special Design <br />
                      <input type="checkbox" name="option7" value="New Design"> New Design
                    </div>
                  </td>

                  <tr valign="top">
                    <td><font face="Verdana" size="2" color="#000000"> Your Inquiry </font></td>
                    <td><textarea name="Inquiry" cols="40" rows="4"></textarea><br> </td>
                  </tr>
                  <tr>
                    <td colspan="2"><input type="submit" class="btn" value="Send Email" name="Submit"> <input type="reset" class="btn" value="  Clear  " name="Clear"></td>
                  </tr>
                </table>

</form>

My PHP Script:

<?php
// Clean up the input values
foreach($_POST as $key => $value) {
  if(ini_get('magic_quotes_gpc'))
    $_POST[$key] = stripslashes($_POST[$key]);

  $_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
}

// Assign the input values to variables for easy reference
$name = $_POST["name"];
$address = $_POST["address"];
$email = $_POST["email"];
$confirm = $_POST["confirm_email"];
$interested_in = $_POST["interested_in"];
$option = $_POST["option"];
$option = implode(', ', $option);
$message = $_POST["message"];

// Send the email *********** enter your email address and message info *******************
$to = "myemail@mydomain.com";
$subject = "Website message: $name";
$message = "From:\
$name\
\
Address:\
$address\
\
Email:\
$email\
\
Interested_in:\
$interested_in\
\
Option:\
&option\
\
Message:\
$message";


$headers = "From: $email";

mail($to, $subject, $message, $headers);

if(!empty($_POST["confirm_email"])) {
    $response = array(
        "success" => true,
        "content" => "<span class='success'><li>Thank you!</li></span>"
    );
    die(json_encode($response));
}
/* Redirect visitor to the thank you page */
header('Location: mydomain/thankyouform.html');
exit();

?>

The JSCRIPT:

/*--validation--*/
$(function() {
            function validateform() {
                var valid = true;
                $(".req").css("border","1px solid green");
                $(".req").each(function() {
                    if($(this).val() == "" ||  $(this).val().replace(/\\s/g, '').length == 0) {
                        $(this).css("border","1px solid red");$(".required").css("display","block");
                        valid = false;
                    }
                });
                return valid;
                }

                $("#submit").click(function() {
                $('#myform').submit(validateform);
                    $('$name').submit();
                });

        });

Thank you in advance. Further, may I know if the PHP script, safe for spammers and hackers?

You should name the option like this


<input type="checkbox" name="option[]" value="Amalfi"> Amalfi 
<input type="checkbox" name="option[]" value="Capri"> Capri 
<input type="checkbox" name="option[]" value="Genova"> Genova 
<input type="checkbox" name="option[]" value="Verona I"> Verona I <br />
<input type="checkbox" name="option[]" value="Verona II"> Verona II 
<input type="checkbox" name="option[]" value="Treviso"> Treviso 
<input type="checkbox" name="option[]" value="Special Design"> Special Design <br />
<input type="checkbox" name="option[]" value="New Design"> New Design 

Thanks for the reply magentoexpert, but it worsen the return message to my mail box. Input fields does not have any data, here what was returned to my mail box: (no information at all)

From:

Address:

Email:

Interested_in:

Option:
&option

Message:

Let 's a basic debug in PHP, put these line as first line of PHP script then you will see what you can get


echo "<pre>";
print_r($_POST);
echo "</pre>";die;

Then submit form and you will know how to solve the problem.

If you still don’t know how to solve, please paste the result after submit to here. I will take care the rest for you.

Hi magentoexpert,

Placing the basic debug in PHP returned " http://www.mydomain.com/send.php " showing the following details:

Array
(
[mailto] => myemail@mydomain.com
[thanksurl] => mydomain.com/thankyouform.html
[Name] => Test name
[Address] => Test address
[E-Mail_Address] => myemail@mydomain.com
[Interested_in] => Tuscania CDO
[option] => Array
(
[0] => Amalfi
[1] => Treviso
)

[Inquiry] =&gt; test 
[Submit] =&gt; Send Email

)

the data was not processed therefore, there was no e-mail sent to the actual e-mail address I encoded in the test form

Your $message variable doesn’t include the $options variable. you need to change

Option:\
[COLOR="#FF0000"][COLOR="#FF0000"]&[/COLOR][/COLOR]option

to

Option:\
[COLOR="#FF0000"]$[/COLOR]option

Change the code like this

// Assign the input values to variables for easy reference 
$name = $_POST["Name"]; 
$address = $_POST["Address"]; 
$email = $_POST["E-Mail_Address"]; 
$confirm = $_POST["confirm_email"]; 
$interested_in = $_POST["interested_in"]; 
$option = $_POST["option"]; 
$option = implode(', ', $option); 
$message = $_POST["Inquiry"]; 

// Send the email *********** enter your email address and message info ******************* 
$to = "myemail@mydomain.com";  
$subject = "Website message: $name"; 
$message = "From:\
$name\
\
Address:\
$address\
\
Email:\
$email\
\
Interested_in:\
$interested_in\
\
Option:\
$option\
\
Message:\
$message";

Then it should work well for sure!

Hi,

The code now redirects to the thankyou.html page and e-mail already reaches my inbox. It shows the Subject Message, Name and e-mail address of the sender, and reply to sender’s e-mail address on the header, but the body of the message does not show the “Interested in” and the “Option” data. Please check this out:

From:
sender’s name is present

Address:
sender address in shown

Email:
myemail@mydoamain.com” is present

Interested_in: (blank)

Option: (blank)

Message:
“test” message present

Thanks for helping out. I hope you still have the time to help me in fixing this…more power!

In HTML file, I see your name is still “Interested in”, it 's wrong with the result submit debug, let 's change it. Other than that, did you do this;

<input type="checkbox" name="option[]" value="Amalfi"> Amalfi 
<input type="checkbox" name="option[]" value="Capri"> Capri 
<input type="checkbox" name="option[]" value="Genova"> Genova 
<input type="checkbox" name="option[]" value="Verona I"> Verona I <br />
<input type="checkbox" name="option[]" value="Verona II"> Verona II 
<input type="checkbox" name="option[]" value="Treviso"> Treviso 
<input type="checkbox" name="option[]" value="Special Design"> Special Design <br />
<input type="checkbox" name="option[]" value="New Design"> New Design

I think you are in a very first step to work with PHP, try to do everything patient and carefully.

The “interested_in” field corrected with code for checkboxes previously replaced as in your above post, it now shows the latest result as follows: (only the “Option:” field remians blank)

From:
sender’s name is present

Address:
sender address in shown

Email:
myemail@mydoamain.com” is present

Interested_in:
Tuscania CDO

Option: (blank)

Message:
“test” message present

And, yes, I am newbie-trying to learn PHP and I am grateful to you for helping me.

You don’t really respond specifically to advice given, so it’s hard to know where you are up to and if you’ve tried some of the suggestions above.

Hi ralph.m,

It’s really nice to hear from you again. With due respect to you, I followed every advice I received on his thread from magentoexpert and you who find time to help me out. Honestly, I feel so grateful for both of you. I hope you’ll not get tired in helping me fix it. Kindly see the full updated codes for your reference:

The HTML code:

<form method=“POST”
action=“mydomain/send.php”>
<input type=“hidden” name=“mailto”
value="myemail@mydomain.com"><!-- … –>
<input type=“hidden” name=“thanksurl”
value=“mydomain/thankyouform.html”><!–
… –>

<p><font color=“#0000FF”><b>Please fill-up this form to send us your inquiry</b></font></p>

            &lt;br /&gt;

            &lt;table cellpadding="2" cellspacing="0" border="1" bgcolor="#dbcbb2"&gt;
              &lt;tr valign="top"&gt; 
                &lt;td&gt;&lt;font face="Verdana" size="2" color="#000000"&gt; Name &lt;/font&gt;&lt;/td&gt; 
                &lt;td&gt;&lt;input type="text" name="Name" size="30"&gt;&lt;/td&gt;
    	  &lt;/tr&gt;
              &lt;tr valign="top"&gt; 
                &lt;td&gt;&lt;font face="Verdana" size="2" color="#000000"&gt; Address &lt;/font&gt;&lt;/td&gt; 
                &lt;td&gt;&lt;input type="text" name="Address" size="30"&gt; &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr valign="top"&gt; 
                &lt;td&gt;&lt;font face="Verdana" size="2" color="#000000"&gt; Email &lt;/font&gt;&lt;/td&gt; 
                &lt;td&gt;&lt;input type="text" name="E-Mail Address" size="30"&gt; &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr valign="top"&gt; 
                &lt;td&gt;&lt;font face="Verdana" size="2" color="#000000"&gt;Interested_in &lt;/font&gt;&lt;/td&gt; 
                &lt;td&gt;&lt;input type="text" name="Interested_in" size="30" value="Tuscania CDO" readonly="readonly"&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr valign="top"&gt; 
              &lt;td&gt;&lt;font face="Verdana" size="2" color="#000000"&gt; Click Box of &lt;br /&gt; Chosen Unit&lt;/font&gt;&lt;/td&gt; 
              &lt;td&gt;
                &lt;div align="left"&gt;

<input type=“checkbox” name=“option” value=“Amalfi”> Amalfi
<input type=“checkbox” name=“option” value=“Capri”> Capri
<input type=“checkbox” name=“option” value=“Genova”> Genova
<input type=“checkbox” name=“option” value=“Verona I”> Verona I <br />
<input type=“checkbox” name=“option” value=“Verona II”> Verona II
<input type=“checkbox” name=“option” value=“Treviso”> Treviso
<input type=“checkbox” name=“option” value=“Special Design”> Special Design <br />
<input type=“checkbox” name=“option” value=“New Design”> New Design

                &lt;/div&gt;
              &lt;/td&gt; 
              
              &lt;tr valign="top"&gt; 
                &lt;td&gt;&lt;font face="Verdana" size="2" color="#000000"&gt; Your Inquiry &lt;/font&gt;&lt;/td&gt; 
                &lt;td&gt;&lt;textarea name="Inquiry" cols="40" rows="4"&gt;&lt;/textarea&gt;&lt;br&gt; &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt; 
                &lt;td colspan="2"&gt;&lt;input type="submit" class="btn" value="Send Email" name="Submit"&gt; &lt;input type="reset" class="btn" value="  Clear  " name="Clear"&gt;&lt;/td&gt;
              &lt;/tr&gt;
            &lt;/table&gt;

</form>

The updated PHP Code:

<?php

// Clean up the input values
foreach($_POST as $key => $value) {
if(ini_get(‘magic_quotes_gpc’))
$_POST[$key] = stripslashes($_POST[$key]);

$_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
}

/// Assign the input values to variables for easy reference
$name = $_POST[“Name”];
$address = $_POST[“Address”];
$email = $_POST[“E-Mail_Address”];
$confirm = $_POST[“confirm_email”];
$interested_in = $_POST[“Interested_in”];
$option = $_POST[“Option”];
$option = implode(', ', $option);
$message = $_POST[“Inquiry”];

// Send the email *********** enter your email address and message info *******************
$to = “myemail@mydomain.com”;
$subject = “Website message: $name”;
$message = “From:
$name

Address:
$address

Email:
$email

Interested_in:
$interested_in

Option:
$option

Message:
$message”;

$headers = “From: $email”;

mail($to, $subject, $message, $headers);

if(!empty($_POST[“confirm_email”])) {
$response = array(
“success” => true,
“content” => “<span class=‘success’></span>”
);
die(json_encode($response));
}
/* Redirect visitor to the thank you page */
header(‘Location: mydomain/thankyouform.html’);
exit();

?>

I followed every instruction on this thread. I have posted a reply with the updated codes as instructed but my post was cued for approval by a moderator.

One thing I notice now is that you have this in the PHP:

$option = $_POST["[COLOR="#FF0000"]Option[/COLOR]"]; 

and this in the HTML:

<input type="checkbox" name="[COLOR="#FF0000"]option[/COLOR][]" value="Amalfi"> Amalfi 

Do you see a discrepancy there? :wink:

yes, the brackets in the name="option’ in the HTML code which is not present in the PHP code. do I have to also place brackets in the PHP code to make it look like this… $option = $_POST[“Option”];

No, it was simpler than that. :slight_smile: (You just needed to compare the two bits in red.) In the HTML you have a lower case o, but uppercase in the PHP. These things matter—which is why I (almost) never use capitals in code, as it just causes unnecessary mistakes like this.

I changed it to lower case, but just the same, the test e-mail show no data on the option field.

No more help to come?..thank you guys for your time…

I’ve moved it to the PHP forum. I’ve pushed my helpfulness as far as it can go. PHP ain’t my thing.