使用ajax从javascript调用php函数的问题

我正在尝试建立一个评级系统,需要从文件 startating.php 调用一个函数并在那里传递变量 - $id(post id)和评级百分比。我不知道如何调用这些变量,然后在 startating.php 中引用它们这是我的代码:


$( function() {

    var postID = <?php echo $id; ?>;


    var rating = new starRating( { // create first star rating system on page load

        containerId: '$id', // element id in the dom for this star rating system to use

        starWidth: 60, // width of stars

        starHeight: 60, // height of stars

        ratingPercent: '0%', // percentage star system should start 

        canRate: true, // can the user rate this star system?


        user: '$userLoggedIn',


        onRate: function() { // this function runs when a star is clicked on

            //console.log( rating );

            $.ajax({

                url: '../handlers/starating.php', 

                type: 'POST', 

                data: 'post_id='+postID+'&rating_num='ratingPercent,

                dataType: 'json',

                success: function(){

                    console.log('works');

                }

            }); 

            //alert('You rated ' + rating.newRating + ' starts' );

        }

    } );

} );


叮当猫咪
浏览 151回答 2
2回答

qq_花开花谢_0

首先,如果你想直接与 PHP 和 JavaScript 交互,你应该确保你的脚本在那个 .php 文件上,这意味着里面的脚本标签没有被导入。现在既然要在 php 中显示一个变量,我们使用echo,你应该用这个替换你的代码。$( function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var postID = '<?php echo $id; ?>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var rating = new starRating( { // create first star rating system on page load&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; containerId: '<?php echo $id; ?>', // element id in the dom for this star rating system to use&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; starWidth: 60, // width of stars&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; starHeight: 60, // height of stars&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ratingPercent: '0%', // percentage star system should start&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; canRate: true, // can the user rate this star system?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; user: '<?php echo $userLoggedIn; ?>',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onRate: function() { // this function runs when a star is clicked on&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //console.log( rating );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: '../handlers/starating.php',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: 'POST',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: 'post_id='+postID+'&rating_num='ratingPercent,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataType: 'json',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log('works');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //alert('You rated ' + rating.newRating + ' starts' );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } );

LEATH

首先,将php代码直接包含在js文件中并不是一个好主意,其中有很多原因,可读性、安全性等。这两者应该严格分开,并且ajax可以控制之间发生的事情。这是一个例子:JSfunction Call_AJAX({do_action,ajax_id,do_success}) {&nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type : 'POST',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url : ajax_url,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataType : 'json',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data : {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; action : do_action,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id: ajax_id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success : do_success&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; };phpfunction call_php_function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; $id&nbsp; &nbsp;= $_POST['id'];&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; //Do something wit the call then return result&nbsp; &nbsp; $result = do_something();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; echo json_encode($result);&nbsp;&nbsp;&nbsp; &nbsp; exit;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;};然后你可以这样称呼它js://Get some sort of resultvar result_append = function (data) {&nbsp; &nbsp; //Do something with the returned data&nbsp; &nbsp; console.log(data.result)};&nbsp;&nbsp;Call_AJAX(&nbsp; &nbsp; do_action = 'call_php_function',&nbsp; &nbsp; ajax_id = some_ajax_id,&nbsp; &nbsp; do_success = result_append);&nbsp; &nbsp;&nbsp;这样,您可以调用php(如call_php_function())中的特定函数,然后获取评分信息。得到结果后,通过json_encode. 一旦 ajax 调用成功,您就可以显示它们。您可以在双方实施错误处理。这样前端发生的任何事情都不会影响后端,反之亦然。希望这可以帮助。
打开App,查看更多内容
随时随地看视频慕课网APP