写入页面的PHP链接未被JAVASCRIPT函数检索

PHP link written into page not being retrieved by a JAVASCRIPT function

本文关键字:JAVASCRIPT 函数 检索 链接 PHP      更新时间:2023-09-26

我正在用PHP在页面上编写以下代码。此代码创建一个 ID 等于 $intIdType(数据库中主键的值)的 HREF 链接,$count获取数据库上每个类别的记录数。所有这些都在一个 WHILE 中,该 WHILE 读取每条记录并将结果写入 PAGE

          echo "<a href='#' id='$intIdType'>";//Starts creating the link
          echo "$intIdType -";
          echo $arrBusinessTypes["business_Description"]; 
          echo "(";
          echo " $count1 )"."</a>";

现在,结果出现在页面上后,它将如下所示:

$intIdType    $arrBusinessTypes       $count
--------------------------------------------
    1       -Auto Sales               ( 1 )
    2       -Auto Repair              ( 1 )
    5       -Web Desig & I.T. Services( 2 )
    6       -Computer Shop            ( 1 )

上面的结果将每个rown显示为一个链接,我可以在其中单击它,但没有任何反应。即使只是JavaScript中的简单警报也不会显示。似乎它甚至从未到达Javascript。

我现在需要的是通过Javascript文件在页面上检索PHP生成的代码,这将允许我在.HTML页面中使用PHP生成的hiperlink。

如果我直接在页面中写入 PHP 应该写的内容,它会起作用。我想知道这是否是因为Javascript无法将发布的数据从PHP读取到页面中。

Javascript文件看起来像这样:

window.onload=post_result
function post_result() {
$("#1").click(function() { //This is the HREF ID that is written by PHP into the page
    $('#list_results').load("results.php");//This seeks to run a PHP file in a DIV
    $('.title').text('Listing Categories');//This just replaces the Title in the page
})

我只是一个初学者尝试。感谢您的任何帮助。

    echo "<a id='$intIdType'>";//Starts creating the link echo "$intIdType -";  
    echo "$intIdType -";  
    echo $arrBusinessTypes["business_Description"];  
    echo "("; echo " $count1 )"."</a>"; 

只需删除 href 属性并尝试

备注 #1:将自定义数据输出为 HTML 时引用自定义数据:回声$arrBusinessTypes["business_Description"];必须是echo htmlspecialchars($arrBusinessTypes["business_Description"]);

备注#2:这里不需要window.onload处理程序。在这一段代码中,您选择复杂的方式来做简单的事情。为什么不写直接点击处理程序?编写函数根据参数加载一些数据,并执行以下操作:

$htmlspecialchars = function($s){return htmlspecialchars($s);};
echo <<<HTML
<a href='#' id='$intIdType' onclick='loadInfo($intIdType)'>
  $intIdType -{$htmlspecialchars($arrBusinessTypes["business_Description"])} ( $count1 )
</a>
HTML;

(转换为 heredoc 以使 HTML 更干净)