我有以下代码:
<?php
include('Connection.php');
$query = $conn->prepare("SELECT DISTINCT c.movieName, c.castName, c.movieImdbId, f.year, f.posterLink FROM cast_movie as c JOIN film_info as f ON c.ImdbId = f.ImdbId WHERE c.castName = :q");
$query->execute(array(':q' => $searchText ));
while($row = $query->fetch(PDO::FETCH_ASSOC)):
?>
<tr>
<td><a href="http://www.imdb.com/title/<?php echo urlencode($row['ImdbId']); ?>"><?php echo $row['movieName']; ?></a></td>
<td><a href="#" id="add">Add to list</a>
</tr>
<?php
endwhile;
?>
<script type="text/javascript">
$('#add').on('click', function (e) {
var selectedOpts = "<?php echo $row['movieName']; ?>";
var obj = {
"movie_name":selectedOpts,
"movie_info": ""
};
parent.window.opener.addToBasket(selectedOpts); //addToBasket is a function that add selected movies to the basket.
});
</script>
当我点击“添加到列表”链接时,它会向篮子添加Nothing(我的意思是,函数addToBasket工作正常,但“selected”的值为null ..
有人可以告诉我如何在我的javascript代码中使用<?php echo $row['movieName'];
吗?我知道我必须把它放在“”之间,但我认为应该有另一个问题..(可能因为这行在javascript代码中不知道,但我不知道如何修复它。)
我还添加了这张图片来澄清问题:
提前致谢,
答案 0 :(得分:1)
没有经过测试的代码,但这应该对您有用:)
<?php
include('Connection.php');
$query = $conn->prepare("SELECT DISTINCT c.movieName, c.castName, c.movieImdbId, f.year, f.posterLink FROM cast_movie as c JOIN film_info as f ON c.ImdbId = f.ImdbId WHERE c.castName = :q");
$query->execute(array(':q' => $searchText ));
while($row = $query->fetch(PDO::FETCH_ASSOC)):
?>
<tr>
<td><a href="http://www.imdb.com/title/<?php echo urlencode($row['ImdbId']); ?>"><?php echo $row['movieName']; ?></a></td>
<td><a href="javascript:addmovie('<?php echo $row['movieName']; ?>')" id="add">Add to list</a>
</tr>
<?php
endwhile;
?>
<script type="text/javascript">
function addmovie(movieName) {
var obj = {
"movie_name":movieName,
"movie_info": ""
};
parent.window.opener.addToBasket(obj); //addToBasket is a function that add selected movies to the basket.
}
</script>
答案 1 :(得分:0)
您可以使用.prev()jquery选项,该选项将返回您的&#39; #add td之前的dom对象。 在这种情况下,旧的电影名称,并获取前一个值作为selectedOps
答案 2 :(得分:0)
我可以看到几个问题:
您使用过id =&#34;添加&#34;对于所有行(电影),然后将click事件附加到id。更改ID =&#34;添加&#34; to class =&#34;添加&#34;。
将电影名称放在数据属性中,如:
<td><a href="#" class="add" data-movieName="<?php echo $row['movieName']; ?>">Add to list</a>
然后将javascript更改为:
<script type="text/javascript">
$('.add').on('click', function (e) {
var selectedOpts = $(e).data("movieName");
var obj = {
"movie_name":selectedOpts,
"movie_info": ""
};
parent.window.opener.addToBasket(selectedOpts); //addToBasket is a function that add selected movies to the basket.
});
</script>
这应该有用。
答案 3 :(得分:0)
你的第一个错误是你正在使用&#34; id&#34;获取值
<a href="#" id="add">Add to list</a>
你已经使用了while循环,因为你的id被重复并且我们都知道&#34; id&#34; js中的元素在整个页面中应该是唯一的。
有许多可用于您的问题的解决方案,其中一个是
while($row = $query->fetch(PDO::FETCH_ASSOC)):
?>
<tr>
<td><a href="http://www.imdb.com/title/<?php echo urlencode($row['ImdbId']); ?>"><?php echo $row['movieName']; ?></a></td>
<td><a href="#" class="add" lang="<?php echo $row['movieName']; ?>">Add to list</a>
</tr>
<?php
endwhile;
?>
Javascript:
<script type="text/javascript">
$('.add').on('click', function (e) {
var selectedOpts = $(this).attr('lang');
var obj = {
"movie_name":selectedOpts,
"movie_info": ""
};
parent.window.opener.addToBasket(selectedOpts); //addToBasket is a function that add selected movies to the basket.
});
</script>
建议:避免在javascript代码中直接使用php,而是尝试使用hidden()变量。