无法从JS调用PHP函数

时间:2018-07-27 02:06:17

标签: javascript php html innerhtml

我正在尝试在HTML文档的JS块内运行PHP函数。 该功能包括PHP,HTML和JS。

该函数包括一个循环,该循环可以正常运行直到最后一次,在该循环中它会打印出以下内容:

“; document.getElementById(” code“)。innerHTML + = sayThis;

错误消息显示: 未捕获的语法错误:意外的标识符

该函数称为load_offers,它从数据库中检索数据,并将其显示在列表对象中。

它会执行我应该做的事,直到我添加以下内容:

<script type="text/javascript">
var sayThis = "<?php load_offers(); ?>";
document.getElementById("code").innerHTML += sayThis;
</script>

需要调用此函数,以便在用户向下滚动到页面末尾时它又加载了6个列表对象,而不是一次加载所有内容。这就是为什么我需要JS来调用函数的原因。

以下是函数:

    function load_offers(){
//Gets variables needed to connect to mysql.
    global $servername;
    global $username;
    global $password;
    global $dbname;
    global $fetched;
    global $amount;

    $conn = new mysqli($servername, $username, $password, $dbname);

//Creates a query that loads a limited number of offers.
     $query = "SELECT ID, TITLE, IMAGELINK, DATE_ADDED, OLD_PRICE, NEW_PRICE, SAVE_PERCENT FROM erbjudanden WHERE APPROVED = 'Y' ORDER BY DATE_ADDED DESC 
    LIMIT $fetched, $amount";

    $result = $conn->query($query);

//Declares arrays.
    $all_offers = array();
    $titles = array();
    $imagelinks = array();
    $dates = array();
    $old_prices = array();
    $new_prices = array();
    $percents = array();
    $link = array();
    $i = 0;
    $bad_chars = array("'", "!", ",", "\"", "(", ")", "[", "]", "#", "&", "+", "=", "@", "$", "%", "|" );

//Gets data from row into arrays.
     while($row = $result->fetch_assoc()){
        $all_offers[$i] = $row['ID'];
        $titles[$i] = $row['TITLE'];
        $imagelinks[$i] = $row['IMAGELINK'];
        $dates[$i] = $row['DATE_ADDED'];
        $old_prices[$i] = $row['OLD_PRICE'];
        $new_prices[$i] = $row['NEW_PRICE'];
        $percents[$i] = $row['SAVE_PERCENT'];

//Creates a link with only numbers, lowercase letters and - instead of spaces.
        $link[$i] = $row['TITLE'];
        $link[$i] = strtolower($link[$i]);
        $link[$i] = str_replace(" ", "-", $link[$i]);
    foreach($bad_chars as $bad){
    $link[$i] = str_replace($bad, "", $link[$i]);
    }
    $link[$i] = str_replace("--", "-", $link[$i]);
    $link[$i] = $link[$i] . "-" . $all_offers[$i];
        $i++;

       }


//Writes out data as listobjects.
    $a = 0;
    foreach ($all_offers as $offer)
    {
    $aID = $fetched + $a;
    echo'<a href="'.$link[$a].'">
            <li class = "listobjekt">
            <h4  align = "center" >' . $titles[$a] . '</h4>
            <img src="' . $imagelinks[$a] . '" class = "tellme" id = "tiny'. $aID .'">
            <table class = "pri">
            <tr><td width="50">Förr:</td><td id ="for"> ' . $old_prices[$a] . ' :-</td></tr>
            <tr><td>Nu:</td><td id = "bu">' . $new_prices[$a] . ' :-</td></tr>
            <tr><td >Spara:</td><td id = "spara">' . $percents[$a] . ' %</td></tr>
            </table>
            </li></a>

//Makes image fit it's container and align correctly.

    <script type="text/javascript">
    var file = document.getElementById("tiny'.$aID.'");

    if(file.height < file.width){

    file.style.width="180px";
    padding = file.width - file.height;
    padding = padding / 2;
    padding = padding+"px";
    file.style.marginTop=padding;
    file.style.marginBottom=padding;

    }
    else{
    file.style.height="180px";

    padding = file.height - file.width;
    padding = padding / 2;
    padding = padding+"px";
    file.style.marginLeft=padding;
    file.style.marginRight=padding;
    }
    </script>
    ';


    $a++;
    }
    }

希望我很清楚。

2 个答案:

答案 0 :(得分:1)

在PHP文档中运行JS可能更简单

为了能够正常工作,它必须位于脚本标记之间的文档正文中。

注意:我在机器上对此进行了测试,并且可以正常工作。文件扩展名为.php

<html lang="en">
<head>
  <meta charset="UTF-8">
  <title></title>
</head>
<body>
<script>
  var test = "<?php echo 'test'; ?>";
  console.log(test);
</script>
</body>
</html>

编辑:显然,这不是最佳实践,这样运行PHP可能会更好:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title></title>
</head>
<body>
<script>
  var test = <?= json_encode('test') ?>;
  console.log(test);
</script>
</body>
</html>

评论中对Phil的支持,以帮助我解决这个问题。

答案 1 :(得分:0)

首先,使用PHP调整图像。

第二,确保javascript变量sayThis在一行上。 Javascript不支持带有单引号的多行。

我在您的计算机上对此进行了测试,它可以正常工作! :)