javascript - 如何将 php 变量添加到此 Javascript 中?

标签 javascript php jquery mysql ajax

我正在尝试构建一个带有ajax过滤器的简单新闻页面,具体取决于用户想要查看的类别。下面的 javascript 连接到 php 文件并使用 mysql 数据库中的数据输出 HTML。我需要知道如何告诉 JS 将“标题”列中的数据放入 php 变量中,以便将其包装在正确的 href 链接中。

这是创建标题和链接的 php

$data_fields = '`id`, `heading`, `date`, `copy`, `summary`, `article`, `press_release`, `video`';
$from = '`media`';

$news_result = $db->selectByStrings($data_fields, $from, $where_conditions, $order_by);
$news = $db->getAllRows($news_result);

foreach ($news as $new) {
    echo '<h2 class="news"><a class="news" href="'.$base_href.$new['id'].'">'.$new['heading'].'</a></h2>';
}

我以某种方式需要包含这是单独的 JS 文件,确保它仅应用于标题列中的数据。

Javascript

function makeTable(data) {
    var tbl_body = "";
    $.each(data, function () {
        var tbl_row = "";
        $.each(this, function (k, v) {
            tbl_row += "<div class='row'><div class='col-md-8 col-md-offset-2'>" + v + "   </div></div>";
        });
        tbl_body += "<div class='non-white'>" + tbl_row + "</div>";
    });
    return tbl_body;
}

function getEmployeeFilterOptions() {
    var opts = [];
    $checkboxes.each(function () {
        if (this.checked) {
            opts.push(this.name);
        }
    });
    return opts;
}

function updateEmployees(opts) {
    $.ajax({
        type: "POST",
        url: "filter3.php",
        dataType: 'json',
        cache: false,
        data: {filterOpts: opts},
        success: function (records) {
            $('#employees div').html(makeTable(records));
        }
    });
}

var $checkboxes = $("input:checkbox");
$checkboxes.on("change", function () {
    var opts = getEmployeeFilterOptions();
    updateEmployees(opts);
});
updateEmployees();

上面连接的 php 文件:

<?php
$pdo = new PDO('mysql:host=localhost;dbname=xxx', 'xxx', 'xxx');
$select = 'SELECT heading, summary, created_date';
$from = ' FROM media';
$where = ' WHERE TRUE';
$opts = isset($_POST['filterOpts']) ? $_POST['filterOpts'] : array('');
if (in_array("article", $opts)) {
    $where .= " AND article = 1";
}
if (in_array("press_release", $opts)) {
    $where .= " AND press_release = 1";
}
if (in_array("video", $opts)) {
    $where .= " AND video = 1";
}
$sql = $select.$from.$where;
$statement = $pdo->prepare($sql);
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($results);
echo($json);
?>

最佳答案

我认为您正在寻找这样的东西:

var base_href = 'http://www.stackoverflow.com/';
function makeTable(data) {
    var tbl_body = "";
    $.each(data, function (index, row) {
        var tbl_row = "";
        $.each(row, function (k, v) {
            if(k == 'heading' && 'id' in row) {
                v = '<a class="news" href="' + base_href + row.id +'">' + v + '</a>';
            }
            tbl_row += "<div class='row'><div class='col-md-8 col-md-offset-2'>" 
            + v + "   </div></div>";
        });
        tbl_body += "<div class='non-white'>" + tbl_row + "</div>";
    });
    return tbl_body;
}

关于javascript - 如何将 php 变量添加到此 Javascript 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32521832/

相关文章:

javascript - 如何检测用户在文本字段中输入了特定字符 ("@")?

php - 我无法使用 PDO 计算数据库中的结果

php - Symfony 的可空自定义表单实体

javascript - 如何在 JQUERY 中制作具有不同类的按钮标签?

javascript - 我有分割屏幕的 javascript 代码,但是,如何从 Flex 应用程序运行它?

javascript - 当值为空时如何隐藏表中的行?

javascript - 防止在隐藏父项并读取子项的 css 时下载背景图像

php - 如何从node.js应用程序获取现有的php/python版本号

javascript - AJAX、JSON、PHP 联系表单、event.preventDefault 不起作用且数据未发送到电子邮件

jquery - 在 jQuery.ajax() GET 请求中记录构造的 URL 字符串