www.webdeveloper.com
Results 1 to 12 of 12

Thread: how to count no of taxtboxe in php on a wb page

  1. #1
    Join Date
    May 2014
    Posts
    8

    how to count no of taxtboxe in php on a wb page

    how to count no of taxtboxe in php on a web page

  2. #2
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    are you pulling the page thru curl/socket request? or outputting the page?

  3. #3
    Join Date
    May 2014
    Posts
    8
    this is my code:--

    <html>
    <head>
    <meta charset="UTF-8">
    <title>Our Local site</title>

    </head>
    <body>
    <form action="welcome.php" method="post">
    <input id="t1" name="t1"type="text" placeholder="table name"><br>
    <input id="s1" name="s1" type="text" placeholder="skill">
    <input id="s2"type="text" placeholder="skill">
    <input id="s3"type="text" placeholder="skill">
    <input id="s4"type="text" placeholder="skill">
    <input id="s5"type="text" placeholder="skill">
    <input id="s6"type="text" placeholder="skill">
    <input id="s7"type="text" placeholder="skill">
    <input id="s8"type="text" placeholder="skill">
    <input id="s9"type="text" placeholder="skill">
    <input id="c1"type="text" placeholder="city">
    <input id="c2"type="text" placeholder="city">
    <input id="c3"type="text" placeholder="city">
    <input id="c4"type="text" placeholder="city">
    <input id="c5"type="text" placeholder="city">
    <input id="c6"type="text" placeholder="city"><br>
    <input id="r1"type="text" placeholder="Region in city">
    <input id="r2"type="text" placeholder="Region in city">
    <input id="r3"type="text" placeholder="Region in city">
    <input id="cn1"type="text" placeholder="country">
    <input id="cn2"type="text" placeholder="country">
    <input id="cn3"type="text" placeholder="country">
    <input id="l1"type="text" placeholder="Latitude in degrees">
    <input id="l2"type="text" placeholder="Longitude in degrees">
    <input type="submit">



    </form>
    </body>
    </html>

    upon clicking the submit button i want to count the no of textboxes i have on this page...please suggest method..

  4. #4
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    You can do
    PHP Code:
    <?php
    if (isset($_POST)) {
        echo 
    count($_POST);
    }
    ?>
    $_POST holds every form element name/value pair.

  5. #5
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    561
    Of course, you need a name attribute on all these inputs if you want to find them in the POST array. Id is unnecessary unless you have some JS that needs it.

  6. #6
    Join Date
    May 2014
    Posts
    676
    @ginerjm has it right. No names, no values server-side. Remember, NAME is for what's sent to the server, ID is what you should target from scripting, CSS or hash. Unless for some reason you want to use nyetscape 4 style form access by name -- which we were supposed to stop doing fifteen years ago -- which is why most developers out there still do it

    Names USUALLY must be unique, but.... you can pass them as arrays.

    Code:
    <input id="s2" name="skill[]" type="text" placeholder="skill">
    <input id="s3" name="skill[]" type="text" placeholder="skill">
    <input id="s4" name="skill[]" type="text" placeholder="skill">
    <input id="s5" name="skill[]" type="text" placeholder="skill">
    <input id="s6" name="skill[]" type="text" placeholder="skill">
    <input id="s7" name="skill[]" type="text" placeholder="skill">
    <input id="s8" name="skill[]" type="text" placeholder="skill">
    Would let you access those where $_POST['skill'] would be an array containing the values of those inputs.

    Now that said, STOP USING PLACEHOLDER INSTEAD OF LABEL. It is NOT a replacement for LABEL, and that's inaccessible trash no matter how many artsy fartsy types want to piss all over usability with it.

    http://www.pardot.com/faqs/best-prac...rs-and-labels/
    http://blog.paciellogroup.com/2011/0...der-attribute/
    http://www.webaxe.org/placeholder-at...s-not-a-label/

    and the specification:
    http://www.w3.org/html/wg/drafts/htm...lder-attribute

    ... and I quote:
    The placeholder attribute should not be used as a replacement for a label. For a longer hint or other advisory text, place the text next to the control.
    WHY? and why is the script-tard equivalent also rubbish? FALSE SIMPLICITY.
    http://baymard.com/blog/false-simplicity

    So I'm hoping that was just 'for testing', as incomplete inaccessible forms like that are a hefty part of what's wrong with a LOT of websites.
    Java is to JavaScript as Ham is to Hamburger.

  7. #7
    Join Date
    May 2014
    Posts
    8
    i get your point and i respec u for giving proof. also i wanna ask can i count inputs using js and then pass the value to server..i mean to php??

  8. #8
    Join Date
    May 2014
    Posts
    8
    now i have this code

    <!DOCTYPE html>
    <!--
    To change this license header, choose License Headers in Project Properties.
    To change this template file, choose Tools | Templates
    and open the template in the editor.
    -->
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Our Local site</title>

    </head>
    <body>
    <form action="welcome.php" method="post">
    <input id="t1" name="t1"type="text" placeholder="table name"><br>
    <input id="s1"type="text" name="skill[]"placeholder="skill or item or spec">
    <input id="s2" type="text" name="skill[]" placeholder="skill or item or spec">
    <input id="s3" type="text" name="skill[]" placeholder="skill or item or spec">
    <input id="s4"type="text" name="skill[]" placeholder="skill or item or spec">
    <input id="s5"type="text" name="skill[]" placeholder="skill or item or spec">
    <input id="s6"type="text" name="skill[]" placeholder="skill or item or spec">
    <input id="s7"type="text" name="skill[]" placeholder="skill or item or spec">
    <input id="s8"type="text" name="skill[]" placeholder="skill or item or spec">
    <input id="s9"type="text" name="skill[]" placeholder="skill or item or spec">
    <input id="c1"type="text" name="city[]"placeholder="city">
    <input id="c2"type="text" name="city[]"placeholder="city">
    <input id="c3"type="text" name="city[]" placeholder="city">
    <input id="c4"type="text" name="city[]" placeholder="city">
    <input id="c5"type="text" name="city[]" placeholder="city">
    <input id="c6"type="text" name="city[]"placeholder="city"><br>
    <input id="r1"type="text" name="region[]"placeholder="Region in city">
    <input id="r2"type="text" name="region[]" placeholder="Region in city">
    <input id="r3"type="text" name="region[]" placeholder="Region in city">
    <input id="cn1"type="text" name="country[]" placeholder="country">
    <input id="cn2"type="text" name="country[]" placeholder="country">
    <input id="cn3"type="text" name="country[]" placeholder="country">
    <input id="l1"type="text" name="lat"placeholder="Latitude in degrees">
    <input id="l2"type="text" name="long"placeholder="Longitude in degrees">
    <input type="submit">
    </form>
    </body>
    </html>


    i want to check whether boxes with names city are empty or not..how should i do this???

  9. #9
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    PHP Code:
    <?php
    if (isset($_POST)) {
        if (isset(
    $_POST['city'])) {
            
    $city = array();

            foreach (
    $_POST['city'] as $value) {
                if (!empty(
    $value)) // add only non-empty values...
                    
    $city[] = $value;
            }
            
    print_r($city); // print the list of cities, excluding empties
        
    }
    }
    ?>

  10. #10
    Join Date
    May 2014
    Posts
    676
    ShrineDesigns has it right, but I'd probably make that a function since with all those different [] you'd probably want to do it on each of them.

    Oh, and @ShrineDesigns, $_POST, $_GET and $_REQUEST always exists and are set, even if empty... so your first IF statement would always be true.

    Also if all you want to do is check if the array is empty, using count would make it simpler.

    Code:
    function postArrayHasValues($index) {
    	if (!isset($_POST[$index])) return false;
    	return count($_POST[$index]) > 0;
    }
    Which you would call thus:

    Code:
    echo '
    Has Cities : ', postArrayHasValues('city'), '<br />
    Has Regions : ', postArrayHasValues('region'), '<br />
    Has Countries : ', postArrayHasValues('country'), '<br />';
    You could easily change that to return 0 if it's not set and the count instead of a compare if you need to know how many there are.

    Code:
    function countPostArray($index) {
    	if (!isset($_POST[$index])) return 0;
    	return count($_POST[$index]);
    }
    Java is to JavaScript as Ham is to Hamburger.

  11. #11
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    Quote Originally Posted by deathshadow View Post
    Oh, and @ShrineDesigns, $_POST, $_GET and $_REQUEST always exists and are set, even if empty... so your first IF statement would always be true.
    Hmm... they must've updated that recently. I've always done this for testing for POST
    PHP Code:
    if ($_POST) {
        
    // ...


  12. #12
    Join Date
    May 2014
    Posts
    676
    You probably saw someone else doing it and assumed it was right... or misinterpreted the check for an array index existing instead of the element itself. I've seen it enough places being done it's easy enough to mistake it for a valid practice... but the superglobals:

    http://www.php.net/manual/en/languag...perglobals.php

    Always exist, even when empty... and it's been that way since they were introduced in PHP 4.1 and the old $HTTP_POST_VARS was deprecated...

    The deprecated $HTTP_POST_VARS on the other hand, doesn't exist unless there is post data, so maybe that's what you were thinking or what where you learned that from thought still applied. When $_POST was introduced and $HTTP_POST_VARS put on the chopping block, a lot of people just did search/replace in their code, so a lot of things you had to do with the older version are still to be found -- even when unnecessary.

    It's like the people who starting out with mysqli or PDO put their connection in global scope and still try to glue together their queries -- it's how it used to work, they're just not grasping WHY the change was made; much akin to the jacktards who used JavaScript to replicate the TARGET attributes behavior as TARGET is deprecated, without asking "WHY was it deprecated?"

    Basically, the wonderful mismatch you always find between old and new methodologies; particularly if you follow some tutorial instead of going right to the horses' mouth -- the specification and/or official manual...

    ...or simply asking the question "WHY?" WAY too many people these days aren't asking the question.
    Last edited by deathshadow; 05-26-2014 at 07:34 PM.
    Java is to JavaScript as Ham is to Hamburger.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles