Jump to content

[SOLVED] Check for empty fields, or just spaces in it


peranha

Recommended Posts

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']);

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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';
}

Link to comment
Share on other sites

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 "==".

 

:)

 

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

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");
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.