Php form not submitting data

I have a PHP form that is submitting blank data by email:
$name, $email, $message is not being sent by email.

I have attached the entire code below.

I am not seeing what I am missing to submit the data.

Thanks in advance.

<?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"];
$email = $_POST["email"];
$message = $_POST["message"];

// Send the email *********** enter your email address and message info *******************
$to = "myemail@myemail.com"; 
$subject = "Website message: $name";
$message = "From:\n$name\n\nEmail:\n$email\n\nMessage:\n$message";
$headers = "From: $email";

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

?>




<!DOCTYPE HTML>
<html>
<head>
<title>Hello</title>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>


<style type="text/css">
label{float:left;width:150px;margin:2% 0;}
input,textarea{width:200px;border:1px solid #ccc;font:16px/26px sans;color:#222;margin:2% 0;}
.required{display:none;color:red;}
</style>

<script type="text/javascript">
$(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);
                    $('#myform').submit();
                });
            
        });
</script>

</head>
<body>


<!--form-->
<div id="success"></div>
<div id="required"></div>
<div id="error"></div>


<form id="myform" name="form"  action="sent.html" method="post">
<div class="required">* red fields are required</div> 
    <div><label>Name</label><input id="name" name="name" class="req" type="text" /></div>
    <div><label>Email</label><input id="email" name="email" type="email"/></div>
    <div><label>Confirm</label><input id="confirm" name="confirm" type="text"/></div>
    <div><label>Message</label><textarea id="message" name="message" type="text"></textarea></div>
    <div><input type="submit" id="submit"></submit>
</form>



</body>
</html>

Open in new window

mewebsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
I see "sent.html" as action...
is .html defined as php processing app in your web server?
apart from that, you are missing the check that the page was actually submitted before sending ...

other than that, I cannot see an easy "trap"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
InsoftserviceCommented:
Please replace it with your code

<?php
// Clean up the input values
$data = array();
echo "<pre>";print_r($_POST);
foreach($_POST as $key => $value) {
  if(ini_get('magic_quotes_gpc'))
    $data[$key] = stripslashes($_POST[$key]);
 
  $data[$key] = htmlspecialchars(strip_tags($_POST[$key]));
}

// Assign the input values to variables for easy reference
$name = $data["name"];
$email = $data["email"];
$message = $data["message"];


echo "<pre>";print_r($data);
0
Dave BaldwinFixer of ProblemsCommented:
What do you mean by "blank data by email"?  Are you receiving an email at all?

@angelIII is right.  The form will 'post' to "sent.html" but it won't run thru the PHP processor and send any email under normal circumstances.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

rinfoCommented:
I think you have issues with this line only
$message = "From:\n$name\n\nEmail:\n$email\n\nMessage:\n$message";
because you have mentioned that its not printing name, email etc.
You might consider using </br> instead of  \n .
Different os has different way to deal with carriage return and line feed .
And i have always considered using </br> for line break.
0
Julian HansenCommented:
You might consider using </br> instead of  \n .
@rinfo - you are assuming that the receipient is an HTML aware source. <br/> (Not </br>) is an HTML tag. The argument that different systems handle linefeeds differently is separate from the outputting of text to an html aware source.
From his code it is obvious he is sending the email as a text email in which case <br/> will output as <br/> and not a line feed.

Unless the server has been configured to pass .html files through the php processer - AngelIII has probably nailed this by pointing out that send.html is going to be interpreted as an HTML file and not PHP - the code it contains is not going to execute. Changing it to send.php will solve the problem - I tested this locally and it works with the name change.

One addition you might need to make in your send.php is either a redirect to a page to display after sending or output of some kind to acknowlege sending (a thank you or success page) for example
<?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"];
$email = $_POST["email"];
$message = $_POST["message"];

// Send the email *********** enter your email address and message info *******************
$to = "myemail@myemail.com"; 
$subject = "Website message: $name";
$message = "From:\n$name\n\nEmail:\n$email\n\nMessage:\n$message";
$headers = "From: $email";

mail($to, $subject, $message, $headers);
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Hello</title>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
</head>
<body>
<p>Email sent successfully</p>
</body>
</html>

Open in new window

0
rinfoCommented:
I never said it was a solution .
I just mentioned what i felt was a practical problem i have regularly faced.
Thanks
0
Ray PaseurCommented:
Here is a script that works.  You might try starting with this as the foundation and tailoring it to your exact needs.

<?php // RAY_form_to_email.php
error_reporting(E_ALL);


// SEND MAIL FROM A FORM


// REQUIRED VALUES ARE PREPOPULATED - CHANGE THESE FOR YOUR WORK
$from = 'NoReply@Your.org';
$subj = 'Contact Form';

// THIS IS AN ARRAY OF RECIPIENTS - CHANGE THESE FOR YOUR WORK
$to[] = 'You@Your.org';
$to[] = 'Her@Your.org';
$to[] = 'Him@Your.org';
$to[] = 'Ray.Paseur@Gmail.com';


// IF THE DATA HAS BEEN POSTED (AT LEAST SOMETHING IN THE EMAIL INPUT CONTROL)
if (!empty($_POST['e']))
{
    // CLEAN UP THE POTENTIALLY BAD AND DANGEROUS DATA
    $safe_mail = clean_string($_POST['e']);
    $safe_name = clean_string($_POST['n']);
    $safe_fone = clean_string($_POST['t']);
    $save_idea = clean_string($_POST['i']);

    // CONSTRUCT THE MESSAGE THROUGH STRING CONCATENATION
    $content    = NULL;
    $content   .= "You have a New Query From $safe_name" . PHP_EOL . PHP_EOL;
    $content   .= "Tel No: $safe_fone" . PHP_EOL;
    $content   .= "Email: $safe_mail" . PHP_EOL;

    // SEND MAIL TO EACH RECIPIENT
    foreach ($to as $recipient)
    {
        if (!mail( $recipient, $subj, $content, "From: $from\r\n"))
        {
            echo PHP_EOL . "<br/>MAIL FAILED FOR $recipient";
        }
        else
        {
            echo PHP_EOL . "<br/>MAIL WORKED FOR $recipient";
        }
    }

    // PRODUCE THE THANK-YOU PAGE
    echo '<p>THANK YOU</p>' . PHP_EOL;
    die('TASK COMPLETE');
}


// A FORM TO TAKE CLIENT INPUT FOR THIS SCRIPT
$form = <<<ENDFORM
<form method="post">
Please enter your contact information
<br/>Email: <input name="e" />
<br/>Phone: <input name="t" />
<br/>Name:  <input name="n" />
<br/>Ideas? <textarea name="i"></textarea>
<br/><input type="submit" />
</form>
ENDFORM;

echo $form;


// A FUNCTION TO CLEAN UP THE DATA - AVOID BECOMING AN OPEN-RELAY FOR SPAM
function clean_string($str)
{
    // IF MAGIC QUOTES IS ON, WE NEED TO REMOVE SLASHES
    $str = stripslashes($str);

    // REMOVE EXCESS WHITESPACE
    $rgx
    = '#'                // REGEX DELIMITER
    . '\s'               // MATCH THE WHITESPACE CHARACTER(S)
    . '\s+'              // MORE THAN ONE CONTIGUOUS INSTANCE OF WHITESPACE
    . '#'                // REGEX DELIMITER
    ;
    $str = preg_replace($rgx, ' ', $str);

    // REMOVE UNWANTED CHARACTERS
    $rgx
    = '#'                // REGEX DELIMITER
    . '['                // START OF A CHARACTER CLASS
    . '^'                // NEGATION - MATCH NONE OF THE CHARACTERS IN THIS CLASS
    . 'A-Z0-9'           // KEEP LETTERS AND NUMBERS
    . '"'                // KEEP DOUBLE QUOTES
    . "'"                // KEEP SINGLE QUOTES
    . '@&+:?_.,/\-'      // KEEP SOME SPECIAL CHARACTERS (ESCAPED HYPHEN)
    . ' '                // KEEP BLANKS
    . ']'                // END OF THE CHARACTER CLASS
    . '#'                // REGEX DELIMITER
    . 'i'                // CASE-INSENSITIVE
    ;
    $str = preg_replace($rgx, NULL, $str);

    // REMOVE EXCESSIVE INPUT
    $str = substr($str, 0, 255);

    return trim($str);
}

Open in new window

Best of luck with it, ~Ray
0
mewebsAuthor Commented:
Thanks. It was the send.html
The send.html is the thank you page. Not any script.
I changed it as follows and it works, except I do not get the Thank you message.

But that deserves another question and points.
0
mewebsAuthor Commented:
here is how I fixed it.

contact.html
<!DOCTYPE HTML>
<html>
<head>
<title>Hello</title>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript" src="send.js"></script>

<style type="text/css">
label{float:left;width:150px;margin:2% 0;}
input,textarea{width:200px;border:1px solid #ccc;font:16px/26px sans;color:#222;margin:2% 0;}
.required{display:none;color:red;}
</style>



</head>
<body>
<p></p>
<!--form-->
<div id="success"></div>
<div id="required"></div>
<form id="myform" name="form" action="send.php" method="post">
<div class="required">* red fields are required</div>
<div><label>Name</label><input id="name" name="name" class="req" type="text" onblur="toUpper(this.value);" /></div>
<div><label>Email</label><input id="email" name="email" type="email" /></div>
<div><label>Phone</label><input id="phone" name="phone" type="tel" class="req" /></div>
<div><label>Confirm</label><input id="confirm_email" name="confirm_email" type="email" class="req2" /></div>
<div><label>Message</label><textarea id="message" name="message" type="text"></textarea></div>
<div><input type="submit" id="submit" /></div>
</form>
</body>
</html>

Open in new window




send.php
<?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"];
$email = $_POST["email"];
$confirm = $_POST["confirm_email"];
$phone = $_POST["phone"];
$message = $_POST["message"];

// Send the email *********** enter your email address and message info *******************
$to = "email@myemail.com"; 
$subject = "Website message: $name";
$message = "From:\n$name\n\nEmail:\n$email\n\nPhone:\n$phone\n\nMessage:\n$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));
}
// Die with a success message
$response = array(
    "success" => true,
    "content" => "<span class='success'><li>Thank you! Your message has been sent.</li></span>"
);
die(json_encode($response));

?>

Open in new window



send.js
/*--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();
                });
            
        });

Open in new window

0
mewebsAuthor Commented:
here is the link to my question about the success error:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/Q_28253524.html

thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.