Solved

PHP if..else option in an else section

Posted on 2011-09-26
5
224 Views
Last Modified: 2012-05-12
Hi
I have the following code in a web page and I need to specify if the {$_POST['txtUsername']} is blank not to say "invalid email address"
how can i do this?
Cheers
Mat
<?php 

} 

} 
else { 


  echo "Invalid email address."; 
} 

?>

Open in new window

0
Comment
Question by:funkkube
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 6

Expert Comment

by:neorush
ID: 36663382
I'm not sure where the rest of the code is....but it would be something like this:
else if($_POST['txtUsername']=='') {
    echo "Invalid email address.";
}
0
 

Author Comment

by:funkkube
ID: 36666746
Hi
Thanks for that..but,,
when the page first opens the message still comes up with INVALID EMAIL which is what i am trying to get rid of..
I just need it to come once the user has pressed LOGIN - which i need to change to SUBMIT which i have just done..
cheers
mat

<p>Please register your email to gain access to vocal stems.</p>
<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
    <p><label for="txtUsername">Email Address:</label> 
    <br /><input type="text" title="Enter your Email Address" name="txtUsername" /></p> 

    <p><input type="submit" name="Submit" value="Login" /></p> 

</form> 

<p>
  <?php 

if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['txtUsername'])) { 
  { 

?> 
  
  Right Click on link to download the file</p>
<p> <a href="xxxxx">b vox 1</a></p>
<p>
  <a href="/xxxxxx">b vox 2</a></p>
<p>
  <a href="/xxxxxx">lead vox</a>

<?php
 $to = "XXXXXXXXXXXX";
 $subject = "REMIX COMPETITION - SOMEONE HAS DOWNLOADED THE STEMS!";
 $body = "Another entry in the REMIX Competition from - {$_POST['txtUsername']}";
 if (mail($to, $subject, $body)) {
   echo("<p></p>");
  } else {
   echo("<p>.</p>");
  }
?>

<?php

$myFile = "remixcomp/mercyremix.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "{$_POST['txtUsername']} \n";
fwrite($fh, $stringData);
fclose($fh);



?>
  <?php 

} 

} 
else if($_POST['txtUsername']=='') { 
    echo "Invalid email address."; 
}

?>

Open in new window

0
 
LVL 6

Accepted Solution

by:
neorush earned 500 total points
ID: 36668542
This should work:
<p>Please register your email to gain access to vocal stems.</p>
<form name="form" method="post" action="<?=$_SERVER['PHP_SELF']?>"> 
    <p><label for="txtUsername">Email Address:</label> 
    <br /><input type="text" title="Enter your Email Address" name="txtUsername" /></p> 
    <p><input type="submit" name="Submit" value="Login" /></p> 

</form> 

<p>
<?php 

if(isset($_POST['txtUsername']) && eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['txtUsername'])) {

?> 
  Right Click on link to download the file</p>
<p> <a href="xxxxx">b vox 1</a></p>
<p>
  <a href="/xxxxxx">b vox 2</a></p>
<p>
  <a href="/xxxxxx">lead vox</a>

<?php
 $to = "XXXXXXXXXXXX";
 $subject = "REMIX COMPETITION - SOMEONE HAS DOWNLOADED THE STEMS!";
 $body = "Another entry in the REMIX Competition from - {$_POST['txtUsername']}";
 if(mail($to, $subject, $body)) echo("<p></p>");
 else echo("<p>.</p>");
?>

<?php

	$myFile = "remixcomp/mercyremix.txt";
	$fh = fopen($myFile, 'a') or die("can't open file");
	$stringData = "{$_POST['txtUsername']} \n";
	fwrite($fh, $stringData);
	fclose($fh);

} 
else if(isset($_POST['txtUsername'])) { 
    echo "Invalid email address."; 
}

?>

Open in new window

0
 

Author Comment

by:funkkube
ID: 36671124
Genius!
Worked like a charm!
Thanks very much! :-)
0
 

Author Closing Comment

by:funkkube
ID: 36673258
Genius!
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Find out what you should include to make the best professional email signature for your organization.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question