在javascript中调用php函数

时间:2013-10-11 16:59:43

标签: javascript php ajax function call

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

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

2 个答案:

答案 0 :(得分: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 
}

答案 1 :(得分:1)

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

function ajaxCall(){
$.ajax({
    type: "GET",
    url: "scripts/on/serverside.php"
   });
};