简体   繁体   English

我的href链接在ajax调用中包含时不起作用

[英]My href links not working when contained within ajax call

I am having a bizarre problem that I cannot figure out. 我有一个奇怪的问题,我无法弄清楚。 It is quite possible that I just do not know something fundamental which is the cause. 我很可能不知道什么是根本原因。 I really have no idea. 我真的不知道。 Anyway; 无论如何; I have a function on a page that uses mysql to look up divisions; 我在一个使用mysql查找分区的页面上有一个函数; then it has buttons for each division; 然后它有每个部门的按钮; and when you click each button; 当你点击每个按钮; a team list for each division appears within a div container. 每个部门的团队列表显示在div容器中。 the buttons send a "div_id" value to the container. 按钮向容器发送“div_id”值。 The container then uses ajax to go back to mysql and then look up the teams for that division and output information on them. 然后容器使用ajax返回mysql,然后查找该部门的团队并输出有关它们的信息。 This is all working. 这一切都有效。 But when I try and have the php file which is called from ajax list a simple href link for each team; 但是当我尝试从ajax列表调用的php文件时,每个团队都有一个简单的href链接; the links do not appear. 链接不会出现。 It really seems that I cannot have href links present in the php file that is called by ajax. 我似乎无法在ajax调用的php文件中出现href链接。

I do not think I need to post all of the code for all the buttons and all that, if so please let me know; 我不认为我需要发布所有按钮的所有代码以及所有这些,如果有,请告诉我; here is the script that does the ajax call on the php file: 这是在php文件上执行ajax调用的脚本:

<script>

 function MyRadFunction(DivId) {
 var answer = DivId;
$.ajax({
cache: false,
type: 'GET',
url: 'http://jba.gtdsites.com/gscript3_links.php',
data: 'answer=' + answer,
dataType: 'html',
success: function(response) {
    $('#ajax_content').html(response);
}
});

    }
</script>

and here is the php file that is called: 这是被称为的php文件:

<?php
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) 
    && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'
) {
    // AJAX request
    $answer = $_GET['answer'];
    $div_id=$answer;






    /* Connect to a mysql database using driver invocation */
$dsn = 'mysql:dbname=gtdtables;host=gtdtables.db.1567753.hostedresource.com';
$user = 'username';
$password = 'password';


try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage(); 
}


$sql = "SELECT * FROM teams WHERE div_id=$div_id";
foreach ($dbh->query($sql) as $resultsg1)
{

$team_id=$resultsg1[team_id];
$team_name=$resultsg1[team_name];



echo "<a href='/team-schedules/?gc_start=2&team_id=<?php echo $team_id; ?>'><?php echo  $team_name; ?></a><br/>";

echo $team_name . "<br/>";
echo $team_id . "<br/>";
?>
Teams Page not link
<a href="/final-exam-1-intro/">Final Exam 1</a>

<a href="http://jba.gtdsites.com/team-schedules/?gc_start=2&team_id=<?php echo $team_id;  ?>"><?php echo $team_name; ?></a><br/>


<?php

}

}
?>

I echoes the team name and team id just fine; 我很好地回应了团队名称和团队ID; and where i just have it print text that works as well; 我只是在那里打印文本也可以; but all of the html hyperlink stuff just does not appear; 但是所有的html超链接东西都没有出现; I tried a few different ways. 我尝试了几种不同的方法。 Can anyone tell me why this is happening? 谁能告诉我为什么会这样?

Your error is kinda funny, but here it is, upon examining the site , on the network tab the markup built from PHP is rendered correctly. 您的错误很有趣,但是在检查网站时 ,在网络选项卡上,可以正确呈现从PHP构建的标记。 But the thing is: 但问题是:

The CSS is the problem: CSS是问题:

a { color: #fff; }

Your text color on the links are white. 链接上的文字颜色为白色。 Therefore its not seen, but its there, just change it accordingly. 因此,它没有看到,但它在那里,只是相应地改变它。

a { color: #000; } /** or which color you want **/

And in your PHP, properly concatenate the values: 在PHP中,正确连接值:

echo "<a href='/team-schedules/?gc_start=2&team_id=".$team_id."'>".$team_name."</a><br/>";

Important note: Don't just directly use variables inside your statements, use prepared statements to produce safer queries. 重要说明:不要直接在语句中使用变量,使用预准备语句来生成更安全的查询。 Here is a tutorial for that. 是一个教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM