0

我不知道如何在我的问题中使用 ajax:我在 php(分配)中有一个函数可以更新数据库中的临时表,我想当用户用户单击按钮(在 javascript 中定义的反馈函数)这个函数(分配)运行,我该怎么办?

   <script>

       function feedback(){
            var boxes = document.getElementsByClassName('box');
            for(var j = 0; j < boxes.length; j++){
                if(boxes[j].checked) {
                    assign(1);
                }
                else{
                    assign(0);
                }
            }
        } 

   </script>




  <?php
        $con = mysql_connect("localhost", "root", "")
        or die(mysql_error());   
        if (!$con) { 
            die('Could not connect to MySQL: ' . mysql_error()); 
        } 
        mysql_select_db("project", $con)
        or die(mysql_error());
        $result = mysql_query("select * from words");
        echo "<table border='1'>
           <tr>
              <th>word</th>
              <th>meaning</th>
              <th>checking</th>
            </tr>";
            while($row = mysql_fetch_array($result)) {
                  echo "<tr>";
                    echo "<td>" . $row['word'] . "</td>";
                    $idd= $row['id'] ;
                    echo "<td>". "<div class='hiding' style='display:none'>".$row['meaning']."</div>"."</td>";
                    echo "<td>";
                     echo "<input class=\"box\" name=\"$idd\" type=\"checkbox\" value=\"\"> ";
                    echo "</td>";
                  echo "</tr>";
                  }
         echo "</table>";

                function assign($checkparm){

                      //mysql_query("update words set checking=$checkparm ");
                       mysql_query("create TEMPORARY TABLE words1user1 as (SELECT * FROM words) ");         
                       mysql_query("update words1user1 set checking=$checkparm ");

                                      }

         mysql_close($con);                        
        ?>
        <button onclick="ShowMeanings()">ShowMeanings</button>
        <button onclick="feedback()">sendfeedback</button>  
4

2 回答 2

6

页面加载后调用 php 函数的方法只有一种:

1.ajax:

function callPHP() {
    $.ajax ({
        url: "yourPageName.php",
        data: { action : assign }, //optional
        success: function( result ) {
            //do something after you receive the result
        }
    }

在你的 PHP 中,写

if ($_POST["action"] == "assign")
{
    assign(your parameters); //You need to put the parameters you want to pass in
                             //the data field of the ajax call, and use $_POST[]
                             //to get them 
}
于 2013-10-11T17:11:08.550 回答
1

互联网上有很多很棒的指南。不过,我会建议你太了解JQuery。它将帮助您完成学习曲线。

function ajaxCall(){
$.ajax({
    type: "GET",
    url: "scripts/on/serverside.php"
   });
};
于 2013-10-11T17:11:21.627 回答