calling a php function in javascript

牧云@^-^@ 提交于 2019-12-03 23:03:14

问题


I dont know how to use ajax in my problem: I have a function in php (assign) that update a temporary table in database, I want to when user user click on a button (feedback function that is defined in javascript) this function (assign) run, what should I do?

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

回答1:


There is only one way to call a php function after the page is loaded:

1.ajax:

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

in your PHP, write

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 
}



回答2:


There are many great guides on the internet. I will however suggest you get too know JQuery. It will help you on your learning curve.

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


来源:https://stackoverflow.com/questions/19323518/calling-a-php-function-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!