Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

processing a form with if empty messages

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

processing a form with if empty messages

Postby Wright67uk » Sun Jan 20, 2013 11:49 am

I've taken this as an extract of a much larger file.

If $score1 is empty the no score message appears, the exit() occurs. and the footer div will not appear.
If $score1 is filled, the success message and the footer div both appear fine.

If I remove the exit, then both the success and the no score message appears, and the footer div displays fine.

I would like the no score message to appear when $score1 is empty, aswell as the footer div to appear.

How should I restructure my code so that is so?

Any basic examples would be great!


Code: Select all
<input type="text"   name="score1"/>
<input type="text"   name="score2"/>

<?php
 if(isset($_POST['processForm']))
   {
      $score1 = $_POST['score'];
      if (empty($score1)) {echo "You haven't entered a score for score2"; exit();}
      else if (empty($score2)) {echo "You haven't entered a score for score1"; exit();}
         
#connection here
$sql = "INSERT STATEMENT HERE";
mysql_query($sql);
         
echo "success message"; 
   
   };
?>
</form>
<div id="footer">Footer</div>
Wright67uk
New php-forum User
New php-forum User
 
Posts: 1
Joined: Sun Jan 20, 2013 11:47 am

Re: processing a form with if empty messages

Postby simplypixie » Tue Jan 22, 2013 12:51 am

You don't need to exit, you just need to assign the text to a variable and then echo the variable (not sure where you are getting score2 from either as nothing in your code so I have added it in as I think it should be)
Code: Select all
$msg = '';
$score1 = $_POST['score1'];
$score2 = $_POST['score2'];
if (empty($score1)) {
    $msg = "You haven't entered a score for score1";
} elseif (empty($score2)) {
    $msg = "You haven't entered a score for score2";
} else {       
    #connection here
    $sql = "INSERT STATEMENT HERE";
    mysql_query($sql);
    // if successful query assign a success message to the $msg variable
 }       
echo $msg; 
User avatar
simplypixie
php-forum Active User
php-forum Active User
 
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire


Return to PHP coding => General

Who is online

Users browsing this forum: mweingand and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron