Page 1 of 1

How to make the error message box pop up?!

Posted: Mon Apr 14, 2008 10:47 pm
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>; 
 

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

Posted: Fri Apr 18, 2008 2:54 pm
by samb0057
<script type="text/javascript">
alert('This is an error message.');
</script>

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

Posted: Fri Apr 18, 2008 6:25 pm
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.

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

Posted: Wed Apr 23, 2008 9:20 pm
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!

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

Posted: Thu Apr 24, 2008 5:26 am
by aceconcepts
javascript is client-side so you would want to throw the message once validation has taken place.

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

Posted: Thu Apr 24, 2008 5:59 am
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.