1

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>
4

2 回答 2

1

json_encode只需返回值 - 你仍然需要告诉 php 如何处理它,即。替换这个:

<?php json_encode($array); ?>

有了这个:

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

长时间盯着屏幕可能出现的情况?

于 2013-03-07T09:54:50.230 回答
0

您必须首先评估 json 代码。您传递给函数的参数是字符串格式。如果不是工作测试,则首先测试删除引号eval('<?php json_encode($array); ?>')eval( '(' + '<?php json_encode($array); ?>' + ')' )

于 2013-03-06T18:17:45.913 回答