douxunwei7083 2011-12-27 09:51
浏览 73
已采纳

如何将已执行的查询返回到PHP的$ .ajax数据[关闭]

I have code which posts a variable to php:

$(document).ready(function() {
    $('ul.sub_menu a').click(function() {
        var txt = $(this).text();  
        //console.log("you clicked"+txt);
            $.ajax({
        type: 'POST',
        url: 'thegamer.php',
        data: {send_txt: txt},
        success: function( data ){
        //now echo the data where you want
        // for example
        //$('#result').html( data );
        // or you can see in on firebug
        console.log( 'Return:' + data );
            }               
     });
});
});

The php that gets the request:

   <?php
include 'dbconnect.php';
$q=$_REQUEST['send_txt'];

  //echo $q;

 $sql="select imgurl from images where family='shoes'";
$result = mysql_query($sql);


include 'dbclose.php';
?>

How do I return the data back to the ajax request?

  • 写回答

3条回答 默认 最新

  • duanqujing3863 2011-12-27 15:16
    关注

    Hm, I really suggest you learn more about Javascript, JQuery, AJAX and PHP before you attempt it. It looks like you have no clue how to go about this.

    I'll point you in the right direction starting with W3 Schools (although definitely not the best resource but, it's a good start):

    W3 Schools

    Here's some more information about JQuery from their own JQuery API Documentation

    More specifically, the $.ajax function

    And here's how you connect to a Mysql database using PHP


    With that being said, this is a general example, using your information, of how you would go about doing an AJAX/server response. I post this so that you might learn something from this code, as it does specifically what you requested:

    Javascript

    $(document).ready(function() {
        $('#element').click(function() {
            //Get data to be sent to server
            //var sFamily = $("#family_search").text();
            //var sFamily = $(this).text();
            var sFamily = "family";
            $.ajax({
                type: 'POST',
                url: 'thegamer.php',
                data: {family: sFamily },
                success: function(response){
                    //Use response
                    //alert("Server echo: "+response);
                    $("#output_element").html(response);
                },
                error: function(msg){
                    alert("Error: "+msg);
                }
            });
        });
    });
    

    PHP file "thegamer.php"

    <?php
        //Credentials
        $server = "localhost";
        $user = "root";
        $password ="admin";
        $db = "dbo";
    
        //Connect
        $link = mysql_connect($server, $user, $password);
        //Select database
        mysql_select_db($db, $link);
    
        //Assemble query
        $family = mysql_real_escape_string($_POST['family'], $link);
        $query = "SELECT imgurl FROM images WHERE family='$family'";
    
        //Query database
        $result = mysql_query($query, $link);
    
        //Output result, send back to ajax as var 'response'
        echo "<table>";
        if(mysql_num_rows($result) > 0){
            //Fetch rows
            while($row = mysql_fetch_array($result)){
                echo "<tr><td>".$row['imgurl']."</td></tr>";
            }
        }else{
            echo "<tr><td>No results matching family \"$family\"</td></tr>";
        }
        echo "</table>";
    
        //mysql_free_result($result)
        //mysql_close($link);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度