Results 1 to 6 of 6

Thread: how get number of item posted

  1. #1
    Join Date
    May 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default how get number of item posted

    hi all
    i want write simple code that can replay me number of form item that not empty
    for example
    i write this code in html page
    HTML Code:
    <form action="getnum.php" method="post">
    <input name="test1" type="text" /><br />
    <input name="test2" type="text" /><br />
    <input name="test3" type="text" /><br />
    <input name="test4" type="text" /><br />
    <input name="test5" type="text" /><br />
    <input name="test6" type="checkbox" value="yes" /><br />
    <input name="" type="submit" value="Submit" />
    </form>
    when this page posted another page i want see how many item is empty or not empty

  2. #2
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    PHP Code:
    $a 0;

    if (!empty(
    $_POST['text1'])) {
        
    $a++;
    }
    if (!empty(
    $_POST['text2'])) {
        
    $a++;
    }
    if (!empty(
    $_POST['text3'])) {
        
    $a++;
    }
    if (!empty(
    $_POST['text4'])) {
        
    $a++;
    }
    if (!empty(
    $_POST['text5'])) {
        
    $a++;
    }
    if (!empty(
    $_POST['text6'])) {
        
    $a++;
    }

    echo 
    $a
    Is a simple way to figure out the number of not empty fields. Short of that, you could loop over the entire form to find the not empty fields.
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  3. #3
    Join Date
    May 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    hi
    this way is correct, but if i have 60 item in $_POST i most write Sixty Times of "if (){}" condition
    Is there another way?
    If I am using a loop in the name must be different in digits
    but I have either a different name
    for example 'name' 'last name' 'email' ...
    Last edited by mostafa_dadgar; 01-03-2013 at 11:16 AM.

  4. #4
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    You've just changed your requirements

    Bernie's loop suggestion works fine if the name fields were sequentially numbered, like the ones you stated in your opening post. You could increment the number part of "text1", "text2", "text3" too.

    To answer the revised query, you could check differently named form fields like this : http://stackoverflow.com/questions/3...ields-required
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  5. #5
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    I forgot to include the empty count - something like;
    PHP Code:
    // Required field names
    $required = array('login''password''confirm''name''phone''email');

    // Loop over field names, make sure each one exists and is not empty
    $error false;
    $a 0;
    foreach(
    $required as $field) {
      if (empty(
    $_POST[$field])) {
        
    $error true;
        
    $a++;
      }
    }

    if (
    $error) {
      echo 
    "All fields are required.\n";
      echo 
    "$a fields are empty.";
    } else {
      echo 
    "Proceed...";

    Untested - on iphone.
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  6. The Following User Says Thank You to Beverleyh For This Useful Post:

    mostafa_dadgar (01-27-2013)

  7. #6
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    You always use HTML arrays and access the values as an array like this:
    http://www.coderprofile.com/site/pastebin/16731

Similar Threads

  1. as posted elseware
    By ajfmrf in forum PHP
    Replies: 3
    Last Post: 08-30-2011, 08:14 PM
  2. Replies: 0
    Last Post: 11-03-2010, 03:12 PM
  3. Add a sequential number to each list item?
    By gwmbox in forum JavaScript
    Replies: 3
    Last Post: 05-26-2010, 09:33 AM
  4. General help with the samples posted here
    By Subhash in forum Dynamic Drive scripts help
    Replies: 3
    Last Post: 05-15-2007, 06:43 PM
  5. Pausing up-down scroller number of item
    By yman in forum Dynamic Drive scripts help
    Replies: 3
    Last Post: 03-02-2007, 11:09 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •