如何在Javascript中连接多个php变量

时间:2018-04-10 18:49:56

标签: javascript php mysql concatenation

您好我正在尝试在我的网站中使用地理数据,而我无法动态提取餐馆的地址并将其放入我的javascript中。我正在使用get方法从网址中提取restaurant_id,然后使用它来提取餐厅的完整地址。这是我遇到问题的代码行(var destinationAddress):

$con=mysqli_connect("root","");
        $rest_id2=$_GET['id'];
        $rest_id=(int)$rest_id2;
        $sql="SELECT * from restaurant WHERE restaurant_id='".$rest_id."'";
        $result=mysqli_query($con,$sql);
        $rows=mysqli_fetch_assoc($result);
?>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=true"></script>
<script>
$(document).ready(function() {

    //exit early if no geolocation
    if(!navigator.geolocation) return;

    var destinationAddress = "<?php echo $rows['address'].$rows['city'].$rows['state'].$rows['zip']; ?>";
</script>
</head>
....

任何人都知道我在这里做错了吗?

3 个答案:

答案 0 :(得分:0)

尝试添加逗号和空格。

var destinationAddress = "<?php echo $rows['address'].$rows['city'].$rows['state'].$rows['zip']; ?>";

答案 1 :(得分:0)

因为忘记关闭函数}),请尝试

<script>
$(document).ready(function() {

    //exit early if no geolocation
    if(!navigator.geolocation) return;

    var destinationAddress = "<?php echo $rows['address'].$rows['city'].$rows['state'].$rows['zip']; ?>";
})
</script>

答案 2 :(得分:0)

让您的生活更轻松:在模板之外构建目标地址:

private string GetTextFromPage(PdfReader pdfReader, int page)
{
     StringBuilder pageText = new StringBuilder();
     var cpage = pdfReader.GetPageN(page);
     var content = cpage.Get(PdfName.CONTENTS);

     //Error for casting Pdfarray to PRIndirectReference
     var indirectReference = (PRIndirectReference)content;
}

然后简单地

$rows=mysqli_fetch_assoc($result);
if( $rows !== NULL )
{
    $destinationAddress = "{$rows['address']}, {$rows['city']}, {$rows['state']}, {$rows['zip']}";
}
else
{
    // no rows! (anomaly)
    $destinationAddress = "no destination address available";
}

// due to the way you'll later inject the variable
// into JavaScript code double quotes must be escaped

$destinationAddress = str_replace( '"', '\"', $destinationAddress );