Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP if..else option in an else section

Posted on 2011-09-26
5
Medium Priority
?
227 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 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

636 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