Jump to content

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

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.