-3

我正在尝试在带有复选框的表单上显示来自 mysql 的数据。问题是如何编写显示它的 js 脚本。代码是:

<form id="myForm" action="pdoakcja.php" method="post"> 
<!--Instruktor: <input type="text" name="name" /> -->
Permissions:<input type="checkbox" name="M1" value="M1" />M1
<input type="checkbox" name="M2" value="M2" />M2
<input type="submit" value="Szukaj" /> 
</form>


<div id='name-data'>Instruktorzy o podanych uprawnieniach:</div>
<script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>

<script> 
............??????
</script> 
4

3 回答 3

0

这是一个伪示例,展示了如何使用 jQuery 进行操作,当您单击复选框时,它也会更新,因此您可以完全删除提交;你说你已经有一个数据库在做这项工作,所以我不会包括它。只需复制和粘贴。

<?php 
//Some pseudo data kinda as your receive it from a query
$datafromSql = array(
array('id'=>1,'permission'=>'M1','theData'=>'User has M1 permission'),
array('id'=>2,'permission'=>'M2','theData'=>'User has M2 permission'),
array('id'=>3,'permission'=>'M1','theData'=>'User has M1 permission'),
array('id'=>4,'permission'=>'M1','theData'=>'User has M1 permission'),
);

//Access the data
if($_SERVER['REQUEST_METHOD']=='POST'){
    $is_ajax = false;
    if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'){
        $is_ajax = true;
    }
    //pseudo code, really you would put your query here
    // SELECT theData FROM your_table WHERE permission=POST_VALUE ... ...

    //And then format your output
    $result=array();

    foreach($datafromSql as $row){
        if($is_ajax == true){
            foreach($_POST as $key=>$value){
                if($_POST[$key] == 'true' && $row['permission']==$key){
                    $result[]=$row['theData'].'<br />';
                }
            }
        }else{
            foreach($_POST as $key=>$value){
                if($_POST[$key] == $row['permission']){
                    $result[]=$row['theData'].'<br />';
                }
            }
        }
    }
    $result = implode('<hr />',$result);

    //AJAX Response, echo and then die.
    if($is_ajax === true){
        header('Content-Type: text/html');
        //example output sent back to the jQuery callback
        echo $result;
        //echo '<pre>'.print_r($_POST,true).'</pre>';
        die;
    }

}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js" charset="utf-8"></script>
<script type="text/javascript">
function update(){
    $.post('./<?php echo basename(__FILE__)?>',
    {
        M1:  $("#M1").is(':checked'),
        M2:  $("#M2").is(':checked')
    },
    function(data) {
        $('#result').replaceWith('<div id="result"><h1>The Result:</h1>'+ data +'</div>');
    });
}
</script>

</head>

<body>

<form method="POST" action="<?php echo basename(__FILE__)?>">
Permissions: 
<input type="checkbox" id="M1" name="M1" value="M1" onChange="update()"/>M1
<input type="checkbox" id="M2" name="M2" value="M2" onChange="update()"/>M2
<input type="submit" value="Szukaj" /> 
</form>

<p id='result'><?php echo isset($result)?$result:null;?></p>

</body>

</html>
于 2012-08-21T11:44:44.200 回答
0

您可以通过使用 jquery 表单插件来解决您的问题,这将帮助您提交表单而无需重新加载页面并在同一页面中向您显示目标页面的返回。只需按照说明进行操作:

先下载这个jquery表单插件并保存。

然后

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<!-- This jquery.form.js is for Submitting form data using jquery and Ajax  -->
<script type="text/javascript" src="js/jquery.form.js"></script> 
 <script type="text/javascript"> 

   $(document).ready(function() { 
    var options = { 

        success:       showResponse             

        };              
 // bind form using 'ajaxForm'              
   $('#myForm').ajaxForm(options); 
 });                    

// post-submit callback                      
 function showResponse(responseText, statusText, xhr, $form)  {                         
 if(responseText==1){

    $("#error").html('No Result Found');            


 } else{
    $("#result").html(responseText);                        

    }                   

}                                

</script>           
<form id="myForm" enctype="multipart/form-data" action="pdoakcja.php" 
 method="post"  name="myForm">

  <!--Instruktor: <input type="text" name="name" /> -->
   Permissions:<input type="checkbox" name="M1" value="M1" />M1
   <input type="checkbox" name="M2" value="M2" />M2
   <input type="submit" value="Szukaj" /> 

</form>

 <span id="error"></span>
 <span id="result"></span>

您的 pdoakcja.php 文件:(我从您的另一篇文章中获得以下代码但尚未检查)

 <?php
 $query = mysql_query("SELECT * FROM permissions WHERE m LIKE '".$_POST['M1']."' OR m  LIKE '".$_POST['M2']."' OR mn LIKE '".$_POST['MN1']."' ");  
   if($query) {
    while($permissions = mysql_fetch_assoc($query)){
    $query2 = mysql_query("SELECT name_surname FROM instruktorzy WHERE  instruktor_id='".$permissions['instruktor_id']."'");  
     while($Mdwa = mysql_fetch_assoc($query2)){
        echo "<p style=\"font-size: 14px; font-family: Helvetica; background-color:  #FFFFFF\"> ".$Mdwa['name_surname']."<br />" ; "</p>" ;
     }
   }
 } else {echo "1";}
  ?>

我希望这对你有用。有关详细信息,您可以研究 jquery 表单插件的网站。

于 2012-08-21T11:30:56.480 回答
-1

您应该使用PHP MySQL 函数从数据库中检索您想要的数据,然后通过 PHP 而不是 javascript 显示它们。

特别是看看这个:mysql_fetch_assoc - 有一个完整的工作示例。

于 2012-08-21T10:50:42.880 回答