Jump to content

check if empty


zirgs

Recommended Posts

hi there i'm using this code
[code]
<?php

if ( $_POST['author']=="name" || $_POST['comment']=="comment" )
{
if (isset($_POST))
{
echo "Some fields were left blank!";
}
}
elseif ( $_POST['author']=="" || $_POST['comment']=="" )
{
if (!isset($_POST))
{
echo "Some fields were left blank!";
}
}
else
{
include 'db_connect.php';
$date = date('d/m/Y G:i');

$query="INSERT INTO comments (newid,author,comment,date) VALUES ('$_POST[id]','$_POST[author]','$_POST[comment]','$date')";
if(mysql_db_query($current_db,$query))
{
echo "Comment added!";
mysql_close($database);
}
}

?>
[/code]
it works fine until i press submit when there isn't anything writen into fields.I need to then show "Some fields were left blank!"
anyone help ? what i'm doing wrong?thanks.
Link to comment
Share on other sites

a few things. your first set says "isset($_POST)" and the second says "!isset($_POST)" That might be a problem.

Another area I'd do differently is the $_POST['whatever']=="". Maybe a strlen($_POST['whatever'])>0 or !$_POST['whatever'].

I'd also reduce the if statements a bit by putting the if($_POST) up top, instead of checking for post under each if statement.

So, if I may rewrite a bit:[code]<?php
if($_POST) {
    if ( $_POST['author']=="name" || $_POST['comment']=="comment" ) {
    echo "Some fields were left blank!";
    } elseif ( !$_POST['author'] || !$_POST['comment'] ) {
    echo "Some fields were left blank!";

    } else {
    include 'db_connect.php';
    $date = date('d/m/Y G:i');
    $query="INSERT INTO comments (newid,author,comment,date) VALUES ('$_POST[id]','$_POST[author]','$_POST[comment]','$date')";
    if(mysql_db_query($current_db,$query)) {
        echo "Comment added!";
        mysql_close($database);
    }
}
?>[/code]
Link to comment
Share on other sites

to check to see if the user pressed the submit button, you would name your submit button. example:

<input type = 'submit' name='submit' value='submit'>

and then in your processing script, you would do like so:

if($_POST['submit']) {
.
.
.
}


but overall, check out this thread:

[a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=96378&hl=\" target=\"_blank\"]http://www.phpfreaks.com/forums/index.php?...topic=96378&hl=[/a]
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.