www.webdeveloper.com
Results 1 to 4 of 4

Thread: [RESOLVED] Form validating issues; form action to php file negates validation, why?

  1. #1
    Join Date
    Oct 2009
    Posts
    13

    resolved [RESOLVED] Form validating issues; form action to php file negates validation, why?

    Hi,

    I really could use some help. I realize there are probably lots of posts on validating forms with Javascript, but I have been at this all day and been pouring through Google and I havenít found what Iím looking for Ė I feel like my validation is close, but I just canít get it to work in two areas. They may be related; they may not, but Iím really stuck. I have a contact form (only three fields, name, email, and message). I want to make sure that there is something in each field and that the email is at least sort of legit before the message is sent through.

    Iíve been trying to mesh tutorials and this is what Iíve come up with. However, my ďYour message has been sentĒ alert which I was trying to apply at the end (if everything was verified) never comes through and Iím absolutely stumped. Any pointers would be greatly appreciated.

    Code:
    <script type="text/javascript">
    function checkform()
    {
    	if (document.contactAPC.name.value == '')
    	{
    		alert('Please enter a name.');
    		return false;
    	}
    	else if(document.contactAPC.email.value == '')
    	{
    		alert('Please enter an email address.');
    		return false;
    	}
    	else if(document.contactAPC.message.value == '')
    	{
    		alert('Please write a message.');
    		return false;
    	}
    	else if(document.contactAPC.email.value != '')
    	{	
    	emailpattern = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
    		if(!emailpattern.test(document.contactAPC.email.value)) 
    			{
    			 alert("Please enter a valid email address");
    			 return false;
    			}
    	}
    	else 
    	{
    		//Why can't I get this message to show?
    				return true;
    				alert("Your message has been sent.");
    	}
    }
    </script>
    
    
    </head>
    <body>
    <h2>Contact Us</h2>
    	<form name="contactAPC" method="post" action="" onSubmit="return checkform()"> 
    	Name <input type="text" size="26" id="name" name="name" class="txtfield_details"> 
    	<br /> 
    	<br />Email <input type="text" size="26" id="email" name="email" class="txtfield_details"> 
    	<br /> 
    	<br /> 
    	Question/Comment
    	<br /><textarea cols="24" rows="4" class="txtfield_details" name="message" id="message"></textarea> 
    	<br /> 
    	<br /><input type="submit" value="Submit" name="submit" class="btns" /> 
    	<input type="reset" value="Clear" name="clear" class="btns" /> 
    	</form>
    Now my other issue is that when I attach the php file (below) to the form action (ie. action=Ēmyfile.phpĒ) Ė the form no longer even bothers to remotely try to validate. It just sends the contents straight through and I get my ďYour message has been sent messageĒ and this also confuses me. I thought checkform() needed to come back as true for the submit button to move the user forward.

    Code:
    <?php
    $email = $_REQUEST['email'] ;
    
    $message = 
    $_POST["name"] ."\n". 
    $_POST["email"] ."\n<>".
    $_POST["message"] ."<>";
    
    mail( "myemail@yahoo.com "From my Website",
    $message, "From: $email" ); 
    header("Location: http://www.mywebsite.org");
    ?>
    If anyone could shed some light on this, it would make my day. Itís just been over a year since Iíve done Javascript and I never quite got the form validation to work right and it just a bit frustrating.

    Thanks.

  2. #2
    Join Date
    Oct 2009
    Posts
    13

    Problem Solved!

    I knew it was something simple (and quite possibly stupid). A mentor helped me catch the error(s), which resolved both issues.

    For anyone else who might ever have a similar problem. In the last, else-statement, I need to have return true; come AFTER the alert box so that the script does exit before I need the last action to run.

    Also I had (although not on this copy) a comment to hide the javascript from older browsers at the bottom of the script:

    Code:
     //stop hiding script -->
    I didn't have the comment on it and it was spitting out a error (apparently in Firefox), although IE8 was reading everything fine/no errors, so that's why I was just really confused.

    I love helpful people. Fixing this did really make my evening so much better. : )

  3. #3
    Join Date
    Mar 2010
    Posts
    2,803
    Code:
     
    {
    //Why can't I get this message to show?
    return true;
    
    alert("Your message has been sent.");
    
    }
    I haven't looked at the rest of your code, but your alert() needs to be above the return statement.

    As it is now if the return is executed, the function returns to the calling statement without displaying "Your message has been sent.").

    **** OK you beat me to it ***** My 2 typing fingers aren't quick enough

    We need a DELETE button for posts..!!!!
    Last edited by tirna; 03-22-2010 at 09:38 PM.

  4. #4
    Join Date
    Oct 2009
    Posts
    13
    [QUOTE]In the last, else-statement, I need to have return true; come AFTER the alert box so that the script does exit before I need the last action to run.
    /QUOTE]

    I mistyped. The part in bold should be does not.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles