...

View Full Version : PHP Contact Form mandatory fields problem



f_m_lil
08-19-2012, 10:34 PM
Hi I am pretty clueless when it comes to PHP. I have got my form to work but I need each field to be mandatory. I have added javascript which pops up with a box to say please enter email, message etc. if left blank but the form still sends, has anyone any ideas how I can stop this. I have attempted to alter the PHP but my knowledge is lacking.

Thanks in advance...


-----HTML----




<form name="formcheck" action="contactform2.php" class="submitform bottom-20" method="POST" onsubmit="return formCheck(this);">
<div>Name:* <br>
<input type=text name="Name"></div><br>
<div>Email:* <br>
<input type=text name="Email"></div><br>
<div>Reference:*<br>
<input type=text name="Reference"></div><br>
<div>Message:* <br>
<input type=text name="Message"></div><br>
<div class="send-wrap">
<div class="alignleft">
<input type=submit value="Submit Form">
</div>
<span class="alignright">* required</span>
</div>
</form>





-----JQUERY----




<script language="JavaScript">


function formCheck(formobj){
// Enter name of mandatory fields
var fieldRequired = Array("Name", "Email", "Reference", "Message");
// Enter field description to appear in the dialog box
var fieldDescription = Array("Name", "Email", "Reference", "Message");
// dialog message
var alertMsg = "Please complete the following fields:\n";

var l_Msg = alertMsg.length;

for (var i = 0; i < fieldRequired.length; i++){
var obj = formobj.elements[fieldRequired[i]];
if (obj){
switch(obj.type){
case "select-one":
if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
case "select-multiple":
if (obj.selectedIndex == -1){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
case "text":
case "textarea":
if (obj.value == "" || obj.value == null){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
default:
}
if (obj.type == undefined){
var blnchecked = false;
for (var j = 0; j < obj.length; j++){
if (obj[j].checked){
blnchecked = true;
}
}
if (!blnchecked){
alertMsg += " - " + fieldDescription[i] + "\n";
}
}
}
}

if (alertMsg.length == l_Msg){
return true;
}else{
alert(alertMsg);
return false;
}
}

</script>





-----PHP----




<?php

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$reference = $_POST['reference'];
$formcontent="From: $name \n Message: $message";
$recipient = "fayelittlewood@hotmail.com";
$subject = "Wayne Long Website Contact Form";
$mailheader = "From: $email \r\n";
$errorString = '';
if(sizeof($_POST)==0){
$errorString .= 'No form data submitted<br />';
die($errorString);
}
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href='www.fayelittlewood.co.uk/thankyou.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";

//If form was submitted
if ($_POST['submitted']==1) {
if ($_POST[Name]){
$name = $_POST[Name]; //If name was entered
}
if ($_POST[Email]){
$email= $_POST[Email]; //If email was entered
}
if ($_POST[Reference]){
$reference = $_POST[Reference]; //If reference was entered
}
if ($_POST[Message]){
$message= $_POST[Message]; //If message was entered
}
}

//If all fields present
if ($title && $textentry){
//Do something
echo "<div class=\"box green\">Form completed!</div>";
}

if ($_POST['submitted']==1) {
$errormsg = ""; //Initialize errors
if ($_POST[title]){
$title = $_POST[title]; //If title was entered
}
else{
$errormsg = "Please enter title";
}
if ($_POST[textentry]){
$textentry = $_POST[textentry]; //If comment was entered
}
else{
if ($errormsg){ //If there is already an error, add next error
$errormsg = $errormsg . " & content";
}else{
$errormsg = "Please enter content";
}
}
}
?>

BuildMyWeb
08-19-2012, 11:09 PM
you can do your form validation in js or php or both. but the two wont interact.

in your php, you would need to check that the POST array has the values you are looking for.

for example:


<?php

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$reference = $_POST['reference'];
$formcontent="From: $name \n Message: $message";
$recipient = "fayelittlewood@hotmail.com";
$subject = "Wayne Long Website Contact Form";
$mailheader = "From: $email \r\n";
$errorString = '';

if( empty($name) )
{
exit('no name was entered.');
}
?>

AndrewGSW
08-19-2012, 11:27 PM
Your JS code works for me. That is, it doesn't submit if any of the fields are empty.


$name = $_POST['name'];

This will generate a PHP error. You should confirm if the posts' values have been set before attempting to read them:


if (isset($_POST['name'])) {
$name = $_POST['name'];
}

and sanitize the post data as well.

BTW You can just do this:


onsubmit="formCheck(this);"

and language="JavaScript" has been deprecated for a (long!) while now. It should be:


<script type="text/javascript">



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum