Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP Contact Form mandatory fields problem

    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----

    Code:
       <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----

    Code:
    <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----

    Code:
        <?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";  
                }  
            }  
        }  
        ?>

  • #2
    New Coder
    Join Date
    May 2012
    Posts
    17
    Thanks
    0
    Thanked 1 Time in 1 Post
    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 Code:
    <?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.');
    }
    ?>
    Freelance Web Developer | Web Hosting
    Web Development, Hosting, Graphic Design, SEO

  • #3
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Your JS code works for me. That is, it doesn't submit if any of the fields are empty.

    PHP Code:
    $name $_POST['name']; 
    This will generate a PHP error. You should confirm if the posts' values have been set before attempting to read them:

    PHP Code:
    if (isset($_POST['name'])) {
        
    $name $_POST['name'];

    and sanitize the post data as well.

    BTW You can just do this:

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

    Code:
    <script type="text/javascript">
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS


  •  

    Posting Permissions

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