Jump to content

[SOLVED] Empty Form


Ken2k7

Recommended Posts

I think what Liam might be getting at is the usefullness of said function. it just doesn't seem very functional to check if all fields are empty, you usually need to check fields individually. The code I wrote would return false even if only one field is filled in.

 

Anyway... here it is a function.

 

<?php

  function all_empty_post() {
    $empty = true;
    foreach($_POST as $v) {
      if (!empty($v)) {
        $empty = false;
        break;
      }
    }
    return $empty;
  }

  if (all_empty_post()) {
    echo "no fields where filled in";
  }

?>

Link to comment
https://forums.phpfreaks.com/topic/65987-solved-empty-form/#findComment-330082
Share on other sites

Oh, I see. But it's ridiculous to make a lot of if statements. Would this work on inserting values if the field is not empty? I'm not sure if php can use void functions since this one, if it works, doesn't return anything.

 

<?php

  function all_empty_post() {
    $empty = true;
    foreach ($_POST as $v)
      if (!empty($v)) {
        mysql_query("INSERT INTO tableName ($v) VALUES ($_POST[$v])") or die(mysql_error());
        $empty = false;
      }
    return $empty;
  }

  if (all_empty_post()) {
    echo "no fields where filled in";
  }

?>

Link to comment
https://forums.phpfreaks.com/topic/65987-solved-empty-form/#findComment-330130
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.