6 Replies - 1560 Views - Last Post: 13 September 2011 - 10:08 PM

#1 swim_fan08  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 238
  • Joined: 19-February 09

If statement problem

Posted 08 September 2011 - 09:02 PM

So far everything works, but my alert box for when a person enters an invalid email address is not showing up. Not too sure if its my IF-statements or that I have two onsubmit functions. I've attached my php for sending emails and my captcha code php. Also, how do I go about if a user just happens to enter a wrong number in the captcha? How do I warn them about that.




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Contact</title>
<link href="style.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">
function ValidateContactForm()
{
if (document.getElementById("text").value === "") 
{
	window.alert("Please provide a detailed description or comment.");
	document.getElementById("text").focus();
	}
if (document.getElementById("author").value === "")
{
	window.alert("Please provide your name.");
	document.getElementById("author").focus();
}
if (document.getElementById("verif_box").value === "")
{
	window.alert("Please enter captcha code.");
	document.getElementById("verif_box").focus();
}  
function checkEmail() 
{
var email = document.getElementById("email");
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (!filter.test(email.value)) 
{
alert('Please provide a valid email address');
email.focus;
}
return true; // successful
  }
  return false; // failed validation
}

</script>
</head>
<body>

<div id="templatemo_wrapper"> 

	<div id="templatemo_header">

 
        
        <ul id="social_box">
                    <li><a href="http://www.facebook.com/"><img 

src="images/facebook.png" alt="facebook" /></a></li>
            <li><a href="http://www.twitter.com/"><img src="images/twitter.png" 

alt="twitter" /></a></li>              
        </ul>
        
      	<div id="site_title">
            <h1><a href="index.htm"><img src="images/logo2.png" alt="logo" 

/><span></span></a></h1>
        </div> <!-- end of site_title -->
        
      
    </div> <!-- end of templatemo_header -->
    


<!-- end of templatemo_menu -->
    
    <div id="templatemo_content_wrapper">
    	<div id="templatemo_content_top"></div>
        <div id="templatemo_content">
        
            <h2>Contact</h2>



<p><b>E-mail: starr05@gmail.com </b></p>

<p><b><a href="http://www.twitter.com/">Twitter</a></b></p>
        
            <div class="cleaner_h50"></div>
            
            	<div id="contact_form">
            
                    <h4>Quick Contact</h4>
                    
                    <form method="post" name="ContactForm" id="contact" action="email.php" 

onsubmit="return ValidateContactForm() && checkEmail();">
                        
                        <div class="col_w340 float_l">
                        
                            <label for="author">Name (* Required):</label> <input 

name="author" type="text" class="input_field" id="author" maxlength="40" />
                          	<div class="cleaner_h10"></div>

 <label for="subject">Subject:</label> <input name="subject" type="text" class="input_field" 

id="subject" maxlength="40" />
                          	<div class="cleaner_h10"></div>

                            
                            <label for="email">Email (* Required):</label> <input 

name="email" type="text" class="input_field" id="email" maxlength="40" />
                          	<div class="cleaner_h10"></div>
                            
                            
Type verification image:<br />
<input name="verif_box" type="text" id="verif_box" style="padding:2px; border:1px solid 

#CCCCCC; width:180px; height:14px;font-family:Verdana, Arial, Helvetica, sans-serif; 

font-size:11px;"/>
<img src="verificationimage.php?<?php echo rand(0,9999);?>" alt="verification image, type it 

in the box" width="50" height="24" align="absbottom" /><br />
<br />                        

</div>
                        


                        <div class="col_w340 float_r">
                        
                            <label for="text">Message (*Required):</label> <textarea 

id="text" name="text" rows="0" cols="0" class="required"></textarea>
                            <div class="cleaner_h10"></div>
                            
                            <input type="submit" class="submit_btn float_l" name="submit" 

id="submit" value="Send" />
                            <input type="reset" class="submit_btn float_r" name="reset" 

id="reset" value="Reset" />
                            
                        </div>
                        
                   </form>

            </div> 

            <div class="cleaner"></div>
            
        </div>
        <div id="templatemo_content_bottom"></div>
	</div>
    
    <div id="templatemo_sp_box">
    
    	<div class="col_w340 float_l">
         
			</div>
        </div>
        
        <div class="col_w340 float_r">
        
			</div>
        </div>
    
    </div>
    
    <div id="templatemo_footer">

      
        Copyright © 2011 <a href="www.twitter.com/">Starr</a><br/> 
        <a href="http://www.iwebsitetemplate.com" target="_parent">Website Templates</a> 
        by <a href="http://www.templatemo.com" target="_parent">Free CSS Templates</a>
    
    </div> <!-- end of templatemo_footer -->
    
</div> <!-- end of templatemo_wrapper -->    

</body>
</html>








<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Contact</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>


<?php


$first_name=$_POST['name'];
$email_address=$_POST['email'];
$subject=$_POST['subject'];
$message=$_POST['text'];

mail("starr05@gmail.com","Subject: $subject",
$message, "From: $first_name <$email_address>");

echo "Thank you for using our mail form.<br/>";
echo "Your email has been sent.";
?>




</body>
</html>






<?php


header('Content-type: image/jpeg');

$width = 50;
$height = 24;

$my_image = imagecreatetruecolor($width, $height);

imagefill($my_image, 0, 0, 0xFFFFFF);

// add noise
for ($c = 0; $c < 40; $c++){
	$x = rand(0,$width-1);
	$y = rand(0,$height-1);
	imagesetpixel($my_image, $x, $y, 0x000000);
	}

$x = rand(1,10);
$y = rand(1,10);

$rand_string = rand(1000,9999);
imagestring($my_image, 5, $x, $y, $rand_string, 0x000000);

setcookie('tntcon',(md5($rand_string).'a4xn'));

imagejpeg($my_image);
imagedestroy($my_image);
?>


Is This A Good Question/Topic? 0
  • +

Replies To: If statement problem

#2 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Re: If statement problem

Posted 08 September 2011 - 10:36 PM

I believe you are just missing an 'else' statement. If this is new to you, maybe this will help.

if (value is true)
	{
            Do this
	}  
else
        {
            Do something else...
        }



That should explain the logic. You may have to nest a loop to check the condition before moving on to the next bit of code too. Hopefully that will help.
Was This Post Helpful? 0
  • +
  • -

#3 swim_fan08  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 238
  • Joined: 19-February 09

Re: If statement problem

Posted 09 September 2011 - 12:07 PM

View PostIsrael, on 08 September 2011 - 11:36 PM, said:

I believe you are just missing an 'else' statement. If this is new to you, maybe this will help.

if (value is true)
	{
            Do this
	}  
else
        {
            Do something else...
        }





That should explain the logic. You may have to nest a loop to check the condition before moving on to the next bit of code too. Hopefully that will help.



Like this?


function ValidateContactForm() {
     if (document.getElementById("text").value === "")
          alert("Please enter your comment.");
     else if (document.getElementById("author").value === "")
          alert("Please enter your name.");
     else if (document.getElementById("email").value === "")
          alert("Please enter your email address.");
     else return true;
   
     return false;
}



Was This Post Helpful? 0
  • +
  • -

#4 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Re: If statement problem

Posted 12 September 2011 - 05:37 AM

Quote

Like this?

function ValidateContactForm() {
if (document.getElementById("text").value === "")
alert("Please enter your comment.");
else if (document.getElementById("author").value === "")
alert("Please enter your name.");
else if (document.getElementById("email").value === "")
alert("Please enter your email address.");
else return true;

return false;
}


Actually no... You are missing braces in these so off the bat you have syntax errors. The logic of this is wrong too. I assume you want all of the statements in quotation marks to be asked. As soon as you got the first value here the others would be skipped. A function should never return true and false either. This would probably be another syntax error as well. I think this page could explain it better than I could.

There are ways to do what you're doing in Javascript. However, Javascript is client-side and therefore not as secure as php. I believe this link can show you everything you need to know, and make your site a little safer.

This post has been edited by Israel: 12 September 2011 - 05:38 AM

Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3480
  • View blog
  • Posts: 10,027
  • Joined: 08-June 10

Re: If statement problem

Posted 12 September 2011 - 07:02 AM

View PostIsrael, on 12 September 2011 - 02:37 PM, said:

You are missing braces in these so off the bat you have syntax errors.

though it is Bad Coding Practice, a lot of JS statements do not require braces, so it would be valid.
not using braces often causes nesting problems.

View PostIsrael, on 12 September 2011 - 02:37 PM, said:

A function should never return true and false either. This would probably be another syntax error as well.

a function will never return true and false at the same time (because you leave the function with the first return statement). thus no syntax error here.
nevertheless, it makes sense to return true if there is no error and false if there is (adapt to the intent of the function)


View PostIsrael, on 12 September 2011 - 02:37 PM, said:

I think this page could explain it better than I could.

there is no real elseif() in JS, what you really do are nested if()s
if () {
    // some code
}
else if() {
    // some other code
}
else {
    // last bit of code
}

is equivalent to
if () {
    // some code
}
else {
    if () {
        // some other code
    }
    else {
        // last bit of code
    }
}


PS. w3schools.com is one of the least favourable JS references (ref.). personally, I recommend the Mozilla Developer Network (easiest way to find something there is to google for "mdc"+keyword)

This post has been edited by Dormilich: 12 September 2011 - 07:03 AM

Was This Post Helpful? 1
  • +
  • -

#6 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Re: If statement problem

Posted 13 September 2011 - 02:22 PM

there is no real elseif() in JS, what you really do are nested if()s


True. I forgot to mention that.

My javascript is a little rusty. But I knew leaving out braces was a bad idea regardless. I knew that the function wouldn't return true and false but isn't this a syntax error as well. Perhaps I'm wrong, but I think Bash would say this is an ambiguous statement and call it a syntax error. Perhaps it is just logical. Either way, it is wrong. But thanks for correcting me.
Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3480
  • View blog
  • Posts: 10,027
  • Joined: 08-June 10

Re: If statement problem

Posted 13 September 2011 - 10:08 PM

View PostIsrael, on 13 September 2011 - 11:22 PM, said:

I knew that the function wouldn't return true and false but isn't this a syntax error as well. Perhaps I'm wrong, but I think Bash would say this is an ambiguous statement and call it a syntax error. Perhaps it is just logical. Either way, it is wrong. But thanks for correcting me.

the way the return value handled is correct (as far as I see). it returns true if neither error is triggered, otherwise false. I donít see a logical problem there.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1