SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2010
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Please help with echoing checkbox values.

    Hello,

    My HTML+JS :

    HTML Code:
            <script type="text/javascript">
                    function passList() {
                            var p=[];
                            $('input.first').each( function() {
                                    if($(this).attr('checked')) {
                                            p.push($(this).attr('rel'));
                                    }
                            } );
                            $.ajax( {
                                    url:'process.php',
                                    type:'POST',
                                    data: {list:p},
                                    success: function(res) {
                                    $("#result").html(res);
                                            
                                    }
                            });
                    }
                   
                   function passType() {
                            var q=[];
                            $('input.second').each( function() {
                                    if($(this).attr('checked')) {
                                            q.push($(this).attr('rel'));
                                    }
                            } );
                            $.ajax( {
                                    url:'process.php',
                                    type:'POST',
                                    data: {type:q},
                                    success: function(res) {
                                    $("#result").html(res);
                                            
                                    }
                            });
                    }
                    
            </script>
    </head>
    <body>
            <input type="checkbox" class="first" rel="list1" onclick="passList()">list1<br />
            <input type="checkbox" class="first" rel="list2" onclick="passList()">list2<br />
            <input type="checkbox" class="first" rel="list3" onclick="passList()">list3<br />
            <input type="checkbox" class="first" rel="list4" onclick="passList()">list4<br />
           <br><br><br>
    	<input type="checkbox" class="second" rel="type1" onclick="passType()">type1<br />
            <input type="checkbox" class="second" rel="type2" onclick="passType()">type2<br />
            <input type="checkbox" class="second" rel="type3" onclick="passType()">type3<br />
            <br><br><br> 
    
            <div id="result"> </div>        
    </body>

    I am able to get the value in the div "result" thru the following script :

    PHP Code:
    if(isset($_POST['list']) && !isset($_POST['type'])) {
         foreach (
    $_POST['list'] as $list) {
          echo 
    "this is $list<br />"
          }
        }
        else if(!isset(
    $_POST['list']) && isset($_POST['type'])) {
         foreach (
    $_POST['type'] as $type) {
          echo 
    "type is : $type<br />"
         }
        }
        else if(isset(
    $_POST['list']) && isset($_POST['type'])) {
         foreach (
    $_POST['list'] as $list) {
          echo 
    "Showing ads for $list<br />"
           foreach (
    $_POST['type'] as $type) {
           echo 
    "type is : $type<br />"
          }
         }
        } 
    But i get the value of either the list or the type.

    I want to be able to get the value of both at the same time. I dont want to use a <form>.


    Please help.

    Thank you.

  2. #2
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You javascript functions each send a POST request. If you want to send one request with both pieces of data, just combine what it's doing into one.

    HTML Code:
            <script type="text/javascript">
                    function passAll() {
                            var p=[];
                            $('input.first').each( function() {
                                    if($(this).attr('checked')) {
                                            p.push($(this).attr('rel'));
                                    }
                            } );
                            var q=[];
                            $('input.second').each( function() {
                                    if($(this).attr('checked')) {
                                            q.push($(this).attr('rel'));
                                    }
                            } );
                            $.ajax( {
                                    url:'process.php',
                                    type:'POST',
                                    data: {list:p, type:q},
                                    success: function(res) {
                                    $("#result").html(res);
                                            
                                    }
                            });
                    }
            </script>
    But I think you'll have to check for empty rather than isset on the PHP side.
    - Robert

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2010
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thank you for your reply. I changed the script to pass both the variables in the same function, but still not getting both the checkboxes types.

    My PHP :
    Code:
    if(isset($_POST['list']) && empty($_POST['type'])) {
         foreach ($_POST['list'] as $list) {
          echo "this is $list<br />"; 
          }
        }
        else if(empty($_POST['list']) && isset($_POST['type'])) {
         foreach ($_POST['type'] as $type) {
          echo "type is : $type<br />"; 
         }
        }
        else if(isset($_POST['list']) && isset($_POST['type'])) {
         foreach ($_POST['list'] as $list) {
          echo "Showing ads for $list<br />"; 
           foreach ($_POST['type'] as $type) {
           echo "type is : $type<br />"; 
          }
         }
        }

    Am i doing it right ?

  4. #4
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You should probably var_dump( $_POST ) or something similar to see exactly what you're getting and compare it to what you're expecting. isset() on either of those should always come back true now so that's really not needed anymore but shouldn't cause any errors here.

    If it isn't working as you expect, post the contents of $_POST and what your PHP outputs.
    - Robert

  5. #5
    Non-Member
    Join Date
    Sep 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try out the below code.

    Name will be an array:

    <input type="checkbox" name="date[]" value="4th" />
    <input type="checkbox" name="date[]" value="5th" />
    <input type="checkbox" name="date[]" value="6th" />

    Then get value like this:

    <?php

    echo "You are available ";
    foreach($_POST["date"] as $value) {
    echo "$value";
    }

    ?>


Tags for this Thread

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
  •