简体   繁体   English

如何使用php变量通过Ajax从sql加载数据?

[英]How to load data from sql through ajax using php variable?

My HTML/PHP code: 我的HTML / PHP代码:

<br/><br/><div id="dialog-modal"></div><br/><br/>

<?php foreach (range(0, 29) as $rs) { ?>
<a data-toggle="modal" href="#" data-href="rsc1<?php echo $rs;?>" class="link">pvz - rsc1<?php echo $rs;?></a><br/>
<?php } ?>

My JavaScript code: 我的JavaScript代码:

$('.link').on('click',function(e){
var linkValue = $(this).attr('data-href');
$.ajax({
    cache: false,
    type: 'GET',
    //url: 'details.php',
    //data: 'i=' + linkValue,
    success: function(data) {
        $('.ui-dialog-title').html(linkValue)
        $('#dialog-modal').html(linkValue).dialog();
    }

}); 
 e.preventDefault();
});

The details.php code: details.php代码:

$i = $_GET['i'];
echo $i;

This script opens only new dialog with my sent data from url data-href . 该脚本仅打开一个新对话框,其中包含我从url data-href发送的数据。 All I want to do is to take some data from sql db into that dialog window by variable $i 我要做的就是通过变量$i从sql db中获取一些数据到该对话框窗口中。

I think you want to know how to get data from sql database and show it in your ajax response. 我认为您想知道如何从sql数据库获取数据并将其显示在ajax响应中。 If so then try something like this: 如果是这样,请尝试如下操作:

details.php code: details.php代码:

 $i = $_GET['i'];//getting your data
    $link = mysqli_connect("localhost", "my_user", "my_password", "db name");//set your correct database connection string
   //check if connection errors 
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
   //make  a query with valid table name   
    $result = mysqli_query($link, "SELECT * from your_table ");
    if($result->num_rows){ //check if any data found
        while ($row = mysql_fetch_assoc($query)) {
            echo  $row['id'];// echo this data
        }
    }
    else{
        echo "no data found!";//echo no data found
    }

    mysqli_close($link); // close mysql connection

In this php page what ever i have echoed it will send as ajax response in your success call back's data. 在此php页面中,我所回应过的内容将作为ajax响应发送到您成功回叫的数据中。 that will display in your modal. 将显示在您的模式中。 I just tried to give you a basic idea. 我只是想给你一个基本的想法。 I think it will help you. 我认为它将为您提供帮助。

Some good resource links: http://www.phptutorialforbeginners.com/2013/01/jquery-ajax-tutorial-and-example-of.html http://www.cleverweb.nl/php/jquery-ajax-call-tutorial/ 一些好的资源链接: http : //www.phptutorialforbeginners.com/2013/01/jquery-ajax-tutorial-and-example-of.html http://www.cleverweb.nl/php/jquery-ajax-call-教程/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM