How to make the error message box pop up?!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
masterphp
Forum Newbie
Posts: 11
Joined: Sun Apr 06, 2008 8:12 pm

How to make the error message box pop up?!

Post by masterphp »

I have designed a simple php code to handle "leave message board" function, there are two fields must be entered by user-"Username" and "Message Content", if either one of those field is empty, then a error message box should be pop up stating that"those two fields must be enterd", for testing purpose, I leave those two fields blank and when I click "Submit" button, then error message doesn't pop up, so can anyone help me with that? Thanks! The following is my code:

Code: Select all

<!--add.php--------------->
<?php
    require_once("sys_conf.inc");
 
    //decide whether message and username is empty
    $errorm="";
     if (!isset($message) or $message=="" or !isset($name) or $name=="") 
    { 
        $errorm="<font color=red></font>* Must be entered"; 
    } 
    else 
    { 
        
        $link_id=mysql_connect($DBHOST,$DBUSER,$DBPWD);         
        mysql_select_db($DBNAME);               
    
        $space = "&nbsp;"; 
        $time = date("YmdHi"); 
        $ip=$REMOTE_ADDR; 
        $message=StripSlashes($message);        
        $message=htmlspecialchars($message); 
        $message=nl2br($message);           
 
        $guestcontent = "<tr><td><font color=#AB00E1>Message Content:</font><br>$message"; 
        $guestcontent=$guestcontent."<br><font color=#6633FF>Username:  </font>$name"; 
        if ($email !="") 
            $guestcontent=$guestcontent."<br><font color=#9900CC>Email:</font><a href=\"mailto:$email\">$email</a>"."$space"; 
        if ($Domain !="") 
            $guestcontent=$guestcontent."<font color=#9900CC>Domain"; 
        if ($Team !="") 
            $guestcontent=$guestcontent."<br><font color=#0000FF>Team:</font>";
        $guestcontent=ereg_replace(chr(10),"",$guestcontent); 
        $guestcontent=$guestcontent."<hr size=1></td></tr>\n"; 
 
        
        $str="insert into guestbook (time,ip,name,domain,team,email,message,content,is_delete)";
        $str=$str."values('$time','$ip','$name','$domain','$team','$email','$message','$guestcontent','n')";
        //echo $str;
        $result=mysql_query($str,$link_id);     
        mysql_close($link_id);   
    }
?>
<html> 
    <head> 
        <title>Search
            </title> 
    <style> 
        <!-- 
            A:link {text-decoration: none ; color:0000ff} 
            A:visited {text-decoration: none; color:004080} 
            A:active {text-decoration: none} 
            A:hover {text-decoration: underline; color:ff0000} 
            BODY {FONT-SIZE:10pt} 
            TH {FONT-SIZE:10 pt} 
            TD {FONT-SIZE: 10pt} 
            TEXTAREA 
            { 
            FONT-FAMILY: "Time News Roman"; 
            FONT-SIZE: 10pt; 
            }
        --> 
    </style> 
</head>
<body bgcolor=#FFFFFD> 
<center>
    <?php include  "head.html"?>
    <h2>Post Message</h2>
    <table width="68%" border="1" cellpadding="3" cellspacing="0" bordercolor="#E3E3E3"> 
        <form method="POST" action="add.php"> 
        <?php
 
        if ($errorm) 
        { 
            echo "<tr>"; 
            echo "<td colspan=3 height=32> "; 
            echo "$errorm"; 
            echo "</td>"; 
            echo "</tr>"; 
        } 
        ?>
        <tr>    
            <td width="32%" bgcolor="#F0F0F0"><font color="#000000">*Username<font color="#FF0033"></font></font></td> 
            <td colspan="2" width="78%" bgcolor="#F0F0F0"><font color="#00FF00">    
                <input type="text" name="name" size="40"> 
                </font></td> 
        </tr> 
        <tr>    
            <td width="22%" height="29">Domain:</td> 
            <td colspan="2" height="29" width="78%">    
                <input type="text" name="domain" size="40"> 
            </td> 
        </tr> 
        <tr>    
            <td width="22%" height="27" bgcolor="#F0F0F0">Team:</td> 
            <td colspan="2" height="27" width="78%" bgcolor="#F0F0F0">  
                <input type="text" name="team" size=40">
            </td> 
        </tr> 
        <tr>    
            <td width="22%" height="20">Email:</td> 
            <td colspan="2" height="20" width="78%"><font color="#00FF00">  
                <input type="text" name="email" size="40"> 
                </font></td> 
        </tr> 
        <tr>    
            <td colspan="3" valign="middle" align="left">   
                <div align="center"><font color="#000000">*Leave Message</font><font color="#FF0033"></font><font color="#00FF00"><br> 
                <textarea rows="6" name="message" cols="55" wrap="VIRTUAL"></textarea> 
                </font></div> 
            </td> 
        </tr> 
        <tr bgcolor="#F0F0F0">  
            <td colspan="3" height="24">    
                <div align="center"><font color="#00FF00">  
                <input type="submit" value="Submit" name="ok"> 
                &nbsp;&nbsp;&nbsp;  
                <input type="reset" value="Clear" name="cencel"> 
                </font>
            </td> 
        </tr> 
        </form> 
    </table>    
    
    </center> 
    </body> 
</html>
Actually I figure it out that the following one line of PHP code can make the "pop up window error message" happen, but I got syntax error for that, so can anyone help me with this? how can I make a pop up window stating the error message? Thanks!

Code: Select all

$errorm="<li><a target="_blank"* Field must be entered"</a></li>; 
 
samb0057
Forum Commoner
Posts: 27
Joined: Wed Mar 26, 2008 9:51 am

Re: How to make the error message box pop up?!

Post by samb0057 »

<script type="text/javascript">
alert('This is an error message.');
</script>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: How to make the error message box pop up?!

Post by califdon »

I agree with samb0057, you should be doing this in Javascript before submitting the form to the server. No point in sending a request to the server that can't be used. To trigger the Javascript, you need to do it either in the onClick event of the Submit button or in the onBlur event of the 2 required Input elements, or something like that. In other words, this kind of data validation should not be done in PHP, since that requires that the request be submitted to the server.
masterphp
Forum Newbie
Posts: 11
Joined: Sun Apr 06, 2008 8:12 pm

Re: How to make the error message box pop up?!

Post by masterphp »

Thanks for that help, but my question is that where should I insert this JavaScript code? I couldn't figure it out where I am supposed to put this code into my php code?! need more help, thanks!
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: How to make the error message box pop up?!

Post by aceconcepts »

javascript is client-side so you would want to throw the message once validation has taken place.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: How to make the error message box pop up?!

Post by onion2k »

califdon wrote:In other words, this kind of data validation should not be done in PHP, since that requires that the request be submitted to the server.
You should do all validation in both JS and PHP, or just PHP if you're too lazy to bother doing both. User's can turn off JS, meaning they can turn off all the validation if you only use JS. That would be bad.
Post Reply