php 在javascript中调用php函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19323518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:16:14  来源:igfitidea点击:

calling a php function in javascript

javascriptphpajaxfunctioncall

提问by Stone

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?

我不知道如何在我的问题中使用 ajax:我在 php (assign) 中有一个函数可以更新数据库中的临时表,我想在用户单击按钮时(在 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>  

回答by what is sleep

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

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

1.ajax:

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

在你的 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 
}

回答by what is sleep

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.

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

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