Jump to content


Photo

check if empty


  • Please log in to reply
4 replies to this topic

#1 zirgs

zirgs
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 21 June 2006 - 09:50 PM

hi there i'm using this 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);
}
}

?>
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.

#2 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 21 June 2006 - 10:09 PM

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:
<?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);
    }
}
?>


#3 zirgs

zirgs
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 21 June 2006 - 10:24 PM

ok i'll try it out later.thanks.

#4 zirgs

zirgs
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 22 June 2006 - 06:52 AM

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?


#5 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 22 June 2006 - 07:06 AM

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]
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users