peranha Posted March 1, 2008 Share Posted March 1, 2008 how do I check to see if there is anything in the post other than spaces. Here is the code I have so far, the first is java and the second is PHP. This is my check for for the PM and comments //Check the form submission for errors function checkForm() { var subject = document.editform.subject; var post = document.editform.post; //Check to make sure post lengths are sensible if (subject.value.length < 2 && post.value.length < 2) { alert("This is a short post!" + " \n \n " + "We require that each post (and subject) \n" + "be at least 2 characters long. \n \n" + "Go back and try again."); return false; } else { if (subject.value.length < 2) { alert("We require that the subject \n" + "be at least 2 characters long. \n \n" + "Go back and try again."); return false; } else { if (post.value.length < 2) { alert("We require that each post \n" + "be at least 2 characters long. \n \n" + "Go back and try again."); return false; } else { return true; } } } } this is the code for PHP for empty strings. // Escape strings, and make sure they are filled in $subject = empty($_POST['subject']) ? die ("<b class=red>ERROR: Enter Subject</b>") : mysql_real_escape_string(strip_tags($_POST['subject'])); $comment = empty($_POST['post']) ? die ("<b class=red>ERROR: Enter a Comment</b>") : mysql_real_escape_string($_POST['post']); Quote Link to comment Share on other sites More sharing options...
toplay Posted March 1, 2008 Share Posted March 1, 2008 See: http://us2.php.net/manual/en/function.trim.php http://us2.php.net/manual/en/function.strlen.php http://us2.php.net/manual/en/ref.ctype.php http://us2.php.net/manual/en/function.preg-match.php Quote Link to comment Share on other sites More sharing options...
shocker-z Posted March 1, 2008 Share Posted March 1, 2008 I generally use this to check for empty and a simple space. if ((str_replace(' ','',$_POST['subject']) == '') && (str_replace(' ','',$_POST['post']) == '')) { $subject = mysql_real_escape_string(strip_tags($_POST['subject'])); $comment = mysql_real_escape_string($_POST['post']); } else { echo 'Empty field found'; } this will strip all spaces and then check to see if it's equal to '' Regards Liam Quote Link to comment Share on other sites More sharing options...
haku Posted March 1, 2008 Share Posted March 1, 2008 Same as the above (I copied it) but a little tidier: if (trim($_POST['subject']) == '') && (trim($_POST['post']) == '')) { $subject = mysql_real_escape_string(strip_tags($_POST['subject'])); $comment = mysql_real_escape_string($_POST['post']); } else { echo 'Empty field found'; } Quote Link to comment Share on other sites More sharing options...
toplay Posted March 1, 2008 Share Posted March 1, 2008 Same as the above (I copied it) but a little tidier: if (trim($_POST['subject']) == '') && (trim($_POST['post']) == '')) { $subject = mysql_real_escape_string(strip_tags($_POST['subject'])); $comment = mysql_real_escape_string($_POST['post']); } else { echo 'Empty field found'; } You have your "if" condition wrong. That will execute the mysql_real_escape_string() on empty fields. I think you meant to use "!=" instead of "==". Quote Link to comment Share on other sites More sharing options...
shocker-z Posted March 1, 2008 Share Posted March 1, 2008 Same as the above (I copied it) but a little tidier: if (trim($_POST['subject']) == '') && (trim($_POST['post']) == '')) { $subject = mysql_real_escape_string(strip_tags($_POST['subject'])); $comment = mysql_real_escape_string($_POST['post']); } else { echo 'Empty field found'; } cheat! lol only joking i can be a bit of a messy coder, totaly self taught and always in a rush lol Same as the above (I copied it) but a little tidier: if (trim($_POST['subject']) == '') && (trim($_POST['post']) == '')) { $subject = mysql_real_escape_string(strip_tags($_POST['subject'])); $comment = mysql_real_escape_string($_POST['post']); } else { echo 'Empty field found'; } You have your "if" condition wrong. That will execute the mysql_real_escape_string() on empty fields. I think you meant to use "!=" instead of "==". See where copying gets you! lmao cheers toplay Quote Link to comment Share on other sites More sharing options...
haku Posted March 1, 2008 Share Posted March 1, 2008 Heh, oops! Quote Link to comment Share on other sites More sharing options...
peranha Posted March 2, 2008 Author Share Posted March 2, 2008 Same as the above (I copied it) but a little tidier: if (trim($_POST['subject']) != '') && (trim($_POST['post']) != '')) { $subject = mysql_real_escape_string(strip_tags($_POST['subject'])); $comment = mysql_real_escape_string($_POST['post']); } else { echo 'Empty field found'; } Gives me this error PHP Parse error: syntax error, unexpected T_BOOLEAN_AND in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\bscardealers\\addcomm1.php on line 20 Line 20 is the if line. Quote Link to comment Share on other sites More sharing options...
haku Posted March 2, 2008 Share Posted March 2, 2008 Post some of the code before that. Maybe about 20 lines or so. Quote Link to comment Share on other sites More sharing options...
peranha Posted March 2, 2008 Author Share Posted March 2, 2008 This is the whole page. vital.php is for sessions, which is on all pages, so no problems with that. header.php is for the header, it is on all pages, so no errors there <?php // Created By Peranha ?> <?php include ("includes/vital.php"); ?> <?php include ("includes/header.php"); ?> <?php // open connection $connection = mysql_connect($server, $user, $pass) or die ("Unable to connect!"); // select database mysql_select_db($db) or die ("Unable to select database!"); if ((trim($_POST['subject']) != '') && (trim($_POST['post']) != '')) { $subject = mysql_real_escape_string(strip_tags($_POST['subject'])); $comment = mysql_real_escape_string(strip_tags($_POST['post'])); } else { echo 'Empty fields found'; } // create query $query = "INSERT INTO " . $pre . "comments (subject, comment, user, timestamp) VALUES ('$subject', '$comment', '$_SESSION[username]', '$stamp')"; // execute query $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); // close connection mysql_close($connection); ?> <?php include ("includes/footer.php"); ?> <?php // Reload user to the userposts.php page header("Location: comments.php"); ?> Quote Link to comment Share on other sites More sharing options...
peranha Posted March 2, 2008 Author Share Posted March 2, 2008 Got it working this is what I ended up doing. if ((trim($_POST['subject']) != '') && (trim($_POST['post']) != '')) { $subject = mysql_real_escape_string(strip_tags($_POST['subject'])); $comment = mysql_real_escape_string(strip_tags($_POST['post'])); } else { echo 'Empty field found'; die; } The first ( was missing before trim. Thanks for the help. 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.