0

Im just new in using ajax and I was following a tutorial from this link http://ricochen.wordpress.com/2011/08/02/passing-array-of-checkbox-values-to-php-through-jquery-example/
But i wasnt able to make it work can you pls help me on this?

html code :

<html>
<head>
<script src="jquery-1.9.1.min.js" type="text/javascript" charset="utf-8"></script> 
 <script type="text/javascript">
                function doit() {
                        var p=[];
                        $('input.cb').each( function() {
                                if($(this).attr('checked')) {
                                        p.push($(this).attr('rel'));
                                }
                        } );
                        $.ajax( {
                                url:'/test2.php',
                                type:'POST',
                                data: {list:p},
                                success: function(res) {
                                        alert(res);
                                }
                        });
                }
        </script>
</head>
<body>       
 <input type="checkbox" class="cb" rel="1"></input>This is 1<br />
        <input type="checkbox" class="cb" rel="abc"></input>This is abc<br />
        <input type="checkbox" class="cb" rel="999"></input>This is 999<br />
        <a href="javascript:void(0)" onclick="doit()">Click</a>       
</body>
</html>  

test2.php :

<?php

        print_r(@$_POST['list']);

?>
5
  • And what exactly isn't working ? Commented Apr 7, 2014 at 11:48
  • try to alert something just after the function doit() { and check whether it is alerting.. Commented Apr 7, 2014 at 11:49
  • @jenz i tried using this and replace the onClick function and it worked function qwe() { alert('test'); } Commented Apr 7, 2014 at 11:52
  • @jenz what i need is to pass array checkbox so i saw this tutorial and try it but it doesnt work Commented Apr 7, 2014 at 11:53
  • check whether the data is passed to test2.php by echoing some text inside that file.. Commented Apr 7, 2014 at 11:57

4 Answers 4

1

try this:
1:you use <input></input> this is wrong!!! <input type='checkbox' name='' />
2:you each all checkbox you can each $('input.cb:checked') and remove if
3:you want post ajax so use $.post

<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript" charset="utf-8"></script> 
 <script type="text/javascript">
                function doit() {
                        var p=[];
                        $('input.cb:checked').each(function(index,value) {                                
                                        p.push($(value).attr('rel'));
                        });
                        $.post('/test2.php',{list:p}).done(function(res) {alert(res);});
                }
        </script>
</head>
<body>       
        <input type="checkbox" class="cb" rel="1"/>This is 1<br />
        <input type="checkbox" class="cb" rel="abc"/>This is abc<br />
        <input type="checkbox" class="cb" rel="999"/>This is 999<br />
        <a href="javascript:void(0)" onclick="doit()">Click</a>       
</body>
</html>
Sign up to request clarification or add additional context in comments.

3 Comments

i tried using this and remove this part $.post('/test2.php',{list:p}).done(function(res) there was no alert showing
because this is not done may not found your php file,i test it and give alert Array ( [0] => 1 [1] => abc ) your php file must beside your html file in <b>web server</b> if not work change file address and remove / before test2.php
ok you saved my day !!!!! if i can check this as an aswer three times i would tnx!
1

Try with this

$('input.cb').each( function() {
       if($(this).is(':checked')) { // change this line in your code
            p.push($(this).attr('rel'));
       }
 });

DEMO

6 Comments

OK i love you! finally it works! can you help me further on my goal if its ok?
@zxc yes tell me :-)
tnx! uhm i need to pass the array checkbox to the test2.php what i did in the test2.php is $list=$_POST['list']; im having a problem on the ajax post part
uhm theres no error showing but theres no data getting inserted in the db. what i did is get the POST[LIST] then insert it into db.
is print_r(@$_POST['list']); or echo display the checkbox value or not?
|
0

using onclick="" attribut is kind of deprecated + you should'nt use a link for an action you should use a button. link are for pages and button for action. http://jsfiddle.net/tLq8L/1/ <-- check it here for a working version

 <script>
 function doit() {

                    var p=[];
                    $('input.cb').each( function() {
                            if($(this).attr('checked')) {
                                    p.push($(this).attr('rel'));
                            }
                    } );
                    $.ajax( {
                            url:'/test2.php',
                            type:'POST',
                            data: {list:p},
                            success: function(res) {
                                    alert(res);
                            }
                    });
            }
 </script>
  <input type="checkbox" class="cb" rel="1"></input>This is 1<br />
    <input type="checkbox" class="cb" rel="abc"></input>This is abc<br />
    <input type="checkbox" class="cb" rel="999"></input>This is 999<br />
    <a href="javascript:void(0)" onclick="doit()">Click</a>       

1 Comment

it's working check with a tool like httpfox or chrome developper to see the http request for course it's ending in error cause I don't have your test2.php
0

if($(this).prop('checked')) instead of if($(this).attr('checked')) will solve your problem

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.