Jump to content

Archived

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

zirgs

check if empty

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.

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
ok it didn't work for me what i wanted .... ok anyone knows how to check if form was submited or not (i mean if submit button was pressed) ... any help?

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.