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
https://forums.phpfreaks.com/topic/12589-check-if-empty/
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
https://forums.phpfreaks.com/topic/12589-check-if-empty/#findComment-48246
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
https://forums.phpfreaks.com/topic/12589-check-if-empty/#findComment-48395
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.