Solved

PHP if..else option in an else section

Posted on 2011-09-26
5
219 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
  • 3
  • 2
5 Comments
 
LVL 6

Expert Comment

by:neorush
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Genius!
Worked like a charm!
Thanks very much! :-)
0
 

Author Closing Comment

by:funkkube
Comment Utility
Genius!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Someone recently asked me about how to display a progress indicator on a page while an iframe is loading. And I remember when I first came across this myself. It was a bit tricky to get my head around, but really, it's very simple. The most impor…
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now