kirkh34 Posted May 7, 2010 Share Posted May 7, 2010 hello, i have a compose message file with a form that inserts data into tables, i'm trying to figure out how to make an error appear if they have left a title or body blank, i'm not sure why this isn't working, here is the code <?php session_start(); include_once "scripts/connect_to_mysql.php"; $logout = ""; $login_table = ""; $message_title= $_POST['message_title']; $message_content= $_POST['message_content']; $to_user= $_POST['to_user']; $from_user= $_POST['from_user']; if (isset ($_POST['submit'])){ if((!$message_title) || (!$message_content)){ if (!$message_title){ $msgToUser = "You need a title!"; include_once "msgToUser.php"; } if(!$message_content){ $msgToUser = "You need a body!"; include_once "msgToUser.php"; } } } else { $sql = mysql_query("INSERT INTO message_rec (to_user, message_content, message_title) VALUES('$to_user','$message_content','$message_title')") or die (mysql_error()); $sql = mysql_query("INSERT INTO message_sent (from_user, message_content, message_title) VALUES('$from_user','$message_content','$message_title')") or die (mysql_error()); $msgToUser = "Your message has been sent"; include_once "msgToUser.php"; } ?> Quote Link to comment Share on other sites More sharing options...
kratsg Posted May 7, 2010 Share Posted May 7, 2010 isset() only checks to see if the variable in question has been set (IE: if it has a value, which could be blank). If you want to check to see if there is no value set, use the empty() function. Quote Link to comment Share on other sites More sharing options...
kirkh34 Posted May 7, 2010 Author Share Posted May 7, 2010 well the submit is under isset().. it's the next part down that's not working, if i leave an empty title or body it still says "message has been sent" and data is put into the table as blanks Quote Link to comment Share on other sites More sharing options...
anups Posted May 7, 2010 Share Posted May 7, 2010 you can use isset with trim if(isset($_POST['key']) && !empty(trim($_POST['key']))){ // do something } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.