Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to pass a php array to The JavaScript Function FindCheckBoxes(Test) using onClick functionality. But this code does not seem to work. It Seems like JSON_ENCODING is not working.

How do I pass my $array variable to the JavaScript Function. I have searched the internet but can't get it to work. That is, alert spits out nothing(a blank). I want '$array' parameters to be there Here is my code:

<!DOCTYPE html>
<!--[if lt IE 7 ]> <html lang="en" class="no-js ie6 lt8"> <![endif]-->
<!--[if IE 7 ]>    <html lang="en" class="no-js ie7 lt8"> <![endif]-->
<!--[if IE 8 ]>    <html lang="en" class="no-js ie8 lt8"> <![endif]-->
<!--[if IE 9 ]>    <html lang="en" class="no-js ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html lang="en" class="no-js"> <!--<![endif]-->

    <head>
        <meta charset="UTF-8" />
        <!-- <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">  -->
        <title>Login and Registration Form with HTML5 and CSS3</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
        <meta name="description" content="Facebook Timeline" />
        <meta name="keywords" content="html5, css3, form, switch, animation, :target, pseudo-class" />
        <meta name="author" content="Codrops" />
        <link rel="shortcut icon" href="../favicon.ico"> 
        <link rel="stylesheet" type="text/css" href="css/demo.css" />
        <link rel="stylesheet" type="text/css" href="css/style1.css" />
        <link rel="stylesheet" type="text/css" href="css/animate-custom.css" />

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript">
    <?php 
    $array =array(); 
    array_push($array,"aa");
    ?>
 function comment(){
     var jFirst = document.getElementById("usernamesignup").value;
    alert("Your name is: "+ jFirst);
 }
  function findCheckedBoxes(test) {
  alert("here");
            var select_array= new Array();
alert(test);            
           var AllBoxes = $(".pp");
           for(i=0;i<AllBoxes.length;i++)
           {
               if(AllBoxes[i].checked)
               {

                   select_array.push(foo[i]);
               }
               }
               alert(select_array);
               return true;
       }
 </script>

    </head>
    <body>
    <?php
 //PostID = 0 means its not a constraint 
 function comment($pauthor, $author, $substr, $start, $postid, $end) { 
     $q = "select * from Comment where Author like '".$author."%' and Text like '%".$substr."%' and Time >= '".$start."' and Time <= '".$end."' and (PostID = ".strval($postid)." or ".strval($postid)." = 0) and PostID in (select PostID from Post where Author like '".$pauthor."%')"; 

  return $q; 

 } 

 function user($user, $pre, $post, $sepr) { 
    if($user == 1) return $pre."1".$post.$sepr.$pre."2".$post.$sepr.$pre."3".$post; 
    if($user == 2) return $pre."2".$post.$sepr.$pre."3".$post; 
    //if($user == 3)  
    return $pre."3".$post; 
 } 

 function comb($q1, $q2, $q3) { 
    $str = "'000'"; 
    for($i = $q1; $i <= 3; $i++) { 
        for($j = $q2; $j <= 3; $j++) { 
            for($k = $q3; $k <= 3; $k++) { 
                $str = $str.", '".strval($i).strval($j).strval($k)."'"; 
            } 
        } 
    } 
    return $str; 
 } 

 function perm($g1, $g2, $g3,$union,$user) { 
    if($union) { 
        if($g1 && $g2 && $g3) //return "Permission_bits like %3% or Permission_bits like %2% or Permission_bits like %1%"; 
                                                    return    user($user, "Permission_bits like '%", "%'", " or "); 
        if($g1 && $g2) return user($user, "Permission_bits like '", "__'", " or ").user($user, "Permission_bits like '_", "_'", " or "); 
        if($g2 && $g3) return user($user, "Permission_bits like '__", "'", " or ").user($user, "Permission_bits like '_", "_'", " or "); 
        if($g1 && $g3) return user($user, "Permission_bits like '", "__'", " or ").user($user, "Permission_bits like '__", "'", " or "); 
        if($g1) return user($user, "Permission_bits like '", "__'", " or "); 
        if($g2) return user($user, "Permission_bits like '_", "_'", " or "); 
        if($g3) return user($user, "Permission_bits like '__", "'", " or "); 
        return "Permission_bits = '000'"; 
    } else { 
        if($g1 && $g2 && $g3) return    "Permission_bits in (".comb($user, $user, $user).")"; 
        if($g1 && $g2) return "Permission_bits in (".comb($user, $user, 0).")"; 
        if($g2 && $g3) return "Permission_bits in (".comb(0, $user, $user).")"; 
        if($g1 && $g3) return "Permission_bits in (".comb($user, 0, $user).")"; 
        if($g1) return "Permission_bits in (".comb($user, 0, 0).")"; 
        if($g2) return "Permission_bits in (".comb(0, $user, 0).")"; 
        if($g3) return "Permission_bits in (".comb(0, 0, $user).")"; 
        return "Permission_bits = '000'"; 
    } 
 } 
 ?> 
<?php
$author=$_REQUEST['author'];
$pauthor=$_REQUEST['post_author'];
$text=$_REQUEST['text'];
$start=$_REQUEST['start'];
$end=$_REQUEST['end'];
?>



        <div class="container">
            <!-- Codrops top bar -->
            <div class="codrops-top">

                <span class="right">

                </span>
                <div class="clr"></div>
            </div><!--/ Codrops top bar -->
            <header>
                <h1>FACEBOOK TIMELINE</h1>

            </header>
            <section>               
                <div id="container_demo" >
                    <!-- hidden anchor to stop jump http://www.css3create.com/Astuce-Empecher-le-scroll-avec-l-utilisation-de-target#wrap4  -->
                    <a class="hiddenanchor" id="toregister"></a>
                    <a class="hiddenanchor" id="tologin"></a>
                    <div id="wrapper">
                        <div id="login" class="animate form">
                            <form action="comments.php" autocomplete="on"> 
                                <h1>COMMENTS</h1> 
                                <br>
                                <br>

                                <TABLE>
                                <td>
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</td>
                                <td>
<img src="comments.jpg" align="center" width=400>   

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

</td>



                                <td>

<FORM id="FORM"  action="comments.php" method="post">
<INPUT TYPE="submit" id="1" VALUE="START A NEW SEARCH HERE " >
</FORM>
                            </td>
                                </TABLE>
                                                                <br>
                                <br>
                                <TABLE BORDER="0" WIDTH=200px ALIGN=CENTER>
<tr>
<h1>YOUR RESULTS</h1>
<INPUT  TYPE="button"  onClick="findCheckedBoxes('<?php  json_encode($array); ?>')" VALUE="CHANGE PERMISSION TO GROUP MEMBERS ONLY" />
<INPUT  TYPE="button"  onclick="findCheckedBoxes()" VALUE="CHANGE PERMISSION TO YOUR FRIENDS ONLY"/>
<INPUT  TYPE="button"  onclick="findCheckedBoxes()" VALUE="CHANGE PERMISSION TO EVERYBODY" />
<?php 
//echo '<FORM id="MYFORM0"  onsubmit="return findCheckedBoxes()" >

//</FORM>.<br>.<br>';
 // Connects to your Database 
 mysql_connect("10.142.233.33", "root", "limiwisn") or die(mysql_error()); 
  //mysql_connect("", "root", "guitar") or die(mysql_error()); 
$count=0;
 mysql_select_db("Ass3") or die(mysql_error()); 
  $datai = mysql_query(comment($pauthor, $author, $text, $start, 0, $end))
  or die(mysql_error());
 while($infoi = mysql_fetch_array( $datai )) 
 {
$count = $count + 1;
 }  
 echo '<tr><td><font size=5>';
 echo "Total number of Comments ->".strval($count)."<br><br>";
 echo '</tr></td></font>';
 echo "<br>";
 mysql_select_db("Ass3") or die(mysql_error()); 
  $datai = mysql_query(comment($pauthor, $author, $text, $start, 0, $end))
  or die(mysql_error());
 echo '<tr><td><font size=4>';

 while($infoi = mysql_fetch_array( $datai )) 
 {
  echo $infoi['CommentID']."<br>";
  echo '<input type="checkbox" class="pp" name="sdfsdg" value="my_checkbox"/>'.'<br>';
  echo $infoi['Author']."<br>";
  echo $infoi['Text']."<br>";
  echo $infoi['Time']."<br><br><br>"; 
  array_push($array,$infoi['CommentID']);
 }  
  echo "<br>";
  echo '</td></tr></font>';
echo '</table>'; 
print_r ($array);
 ?>
                                <br>
                                <br>





                            </form>
                        </div>



                    </div>
                </div>  
            </section>
        </div>
    </body>
</html>
share|improve this question
5  
Your code is far too long. Reduce it to the bare necessities! –  In God I Trust Mar 6 '13 at 17:54
    
So, what does "But this code does not seem to work" mean? What do you expect to happen and what actually happens? Are you seeing JS errors in the error console? What does your alert(test); spit out? What do you expect it to spit out? –  Steve Mar 6 '13 at 17:57
    
I am sorry but i have edited it now. Kindly have a look –  user2127569 Mar 6 '13 at 18:00
    
So when you look at the HTML source, what is contained in the the findCheckBoxes function call (inside the input's onClick attribute)? –  Steve Mar 6 '13 at 18:03
    
@Steve i din't get u. I want to pass the $array using onClick(that i get from php after modification ). But the Alert pops up an empty array –  user2127569 Mar 6 '13 at 18:07
show 8 more comments

2 Answers

json_encode simply returns the value - you still need to tell php what to do with it, ie. replace this:

<?php json_encode($array); ?>

with this:

<?php echo json_encode($array); ?>

Possible case of staring at the screen too long?

share|improve this answer
add comment

you must first eval the json code. the parameter that you have passed to function is in string format. first test to remove the quotes if not work test eval('<?php json_encode($array); ?>') or eval( '(' + '<?php json_encode($array); ?>' + ')' )

share|improve this answer
    
If you wanted to evaluate it in-line why would you choose eval over jsonp? –  Emissary Mar 7 '13 at 9:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.