从PHP嵌入变量

时间:2019-07-31 09:23:27

标签: php jquery mysql

我正在 试图将数据库值从php脚本压缩/嵌入到我的api URL 但是我不知道如何正确使用它,我对php不熟悉

它需要从数据库中嵌入变量以访问这样的API值 api / rfq / 1(示例)

到目前为止,我已经尝试过

php代码:


<?php 
require_once('connection.php');
session_start();

if(!$con){
    die('Please Check Your Connection'.mysqli_error());
}else{
    $query="select BusinessID from business where username='".$_SESSION['User']."'";
    $result=mysqli_query($con,$query);           
    while($row =mysqli_fetch_array($result)){
          $results[]=implode($row['BusinessID']);
    }            
    echo json_encode($results);            
           // echo 'Works ASF '+$hey;
}           

?>

jQuery文件:


$(function() {  
    $.getJSON('getID.php', function(data) { 
        $.each(data, function() {
            alert(data.column);
        })  
    })

    var $hello = $('#hello');   
     $.ajax({
        type: 'GET',
        dataType: "json",
        url: 'http://slimapp/api/rfq/"'+$_SESSION['User']+'"',
        success: function(hello){
            $.each(hello, function(i,order){
                $hello.append('<div class="panel panel-default"><div class="panel-thumbnail"></div><div class="panel-body"><p class="lead"><b><img src="assets/img/Profile.png" height="28px" width="28px">'+order.Title+'<a href="#postModal" class="pull-right" role="button" data-toggle="modal"><i class="glyphicon glyphicon-plus"></i>Make Bid</a></b></p><p><img src="assets/img/RFQ.png" height="28px" width="28px">'+order.Description+'</p><p><img src="assets/img/Clock.png" height="28px" width="28px">'+order.Date+'</p><hr/><p><img src="assets/img/email.png" height="28px" width="28px"><a href="https://mail.google.com/mail/?view=cm&fs=1&to=someone@example.com&su=SUBJECT&body=BODY&bcc=someone.else@example.com">'+order.email+'</a></p><p><img src="assets/img/Phone.png" height="28px" width="28px"><a href="">'+order.cellphone+'</a><p><textarea class="form-control" placeholder="Comment"></textarea><br/><li id="btn1"></li><button class="btn btn-success pull-left" type="button" style="border-radius:12px">Comment</button></p></div></div>');       
            })
        }
    }) 

});
  

我希望像这样的http://slimapp/api/rfq/1

从网址中获取数据

1 个答案:

答案 0 :(得分:1)

您的Jquery文件应如下所示:

function apiCall(user) {

    var url = 'http://slimapp/api/rfq/'+ user +''; // This var is creating the right URL, using the parameter USER of the function

     $.ajax({
        type: 'GET',
        dataType: "json",
        url: url, // Calling the new right URL

        success: function(hello){

            // Handle your success

        }
    }) 

};

您要在此处创建一个新函数,该函数接收要在URL中传递的参数User。

您的PHP文件应如下所示:

<?php

    $results = '1';

?>

<script>

    apiCall(<?php echo $results ?>); // Passing the value 1 as a parameter for the function

</script>

在这里,您要调用函数 apiCall 并将变量 $ results 的值作为参数传递。