Solved

PHP if..else option in an else section

Posted on 2011-09-26
5
221 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
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Html5 Index on a table 7 26
Call to undefined method mysqli_stmt on one live server but not the other 21 48
comma true 6 32
Is there a simpler dropbox system? 10 18
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

776 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