在PHP中重复函数

时间:2015-06-05 21:30:08

标签: php mysql loops onclick

我正在努力解决这个问题并且无法解决这个问题。这就是我所做的:

形式:

<form method="POST" action="search.php" name="form" id="form">
<input type="text" name="search" id="search" placeholder="Introduza a sua Localização">
<input type="submit" name="Submit" value="Pesquisar">
</form>

php循环:

<?php if (mysql_num_rows($tournaments) !=0){
do { 
?>
<div id="rightDetails">
<i class="fa fa-gamepad"></i> <a href="games.php?id_game=<?php echo $row_tournaments['id_game']; ?>"><?php echo $row_tournaments['game']; ?></a><br>
<i class="fa fa-calendar-o"></i> <a href="#"><?php echo $row_tournaments['date']; ?></a><br>
<i class="fa fa-pencil-square-o"></i> <a href="tournaments.php?id_tournament=<?php echo $row_tournaments['id_tournament']; ?>#disqus_thread">Sem comentários</a><br>
<i class="fa fa-map-marker"></i> <a onClick="giveThatInputAValue()"><?php echo $row_tournaments['location']; ?></a><br>
<img src="images/<?php echo $row_tournaments['online']; ?>.png"> <a href="#"><?php echo $row_tournaments['online']; ?></a>
</div>
<?php } while ($row_tournaments = mysql_fetch_assoc($tournaments));
} else {
?>
<div id="noresults"><p>Sem Torneios</p></div>
<?php
}
?>

功能

function giveThatInputAValue(){
var elem = document.getElementById("search");
elem.value = "<?php echo $row_tournaments['location']; ?>";
document.forms["form"].submit();}

所以当我按下时,

<a onClick="giveThatInputAValue()"><?php echo $row_tournaments['location']; ?></a>

输入值将改变输入和提交表单之后的值,但问题仍然存在:如何将此函数放入循环中,以便值从数据库更改为正确的值?

正确加载此值:<?php echo $row_tournaments['location']; ?>

但在循环之外,它总是显示数据库的第一个结果。

请帮忙解决不了。

1 个答案:

答案 0 :(得分:0)

您需要为每次调用JS函数重复该值:

php循环:

    <?php if (mysql_num_rows($tournaments) !=0){
    do { 
    ?>
    <div id="rightDetails">
    <i class="fa fa-gamepad"></i> <a href="games.php?id_game=<?php echo $row_tournaments['id_game']; ?>"><?php echo $row_tournaments['game']; ?></a><br>
    <i class="fa fa-calendar-o"></i> <a href="#"><?php echo $row_tournaments['date']; ?></a><br>
    <i class="fa fa-pencil-square-o"></i> <a href="tournaments.php?id_tournament=<?php echo $row_tournaments['id_tournament']; ?>#disqus_thread">Sem comentários</a><br>
    <i class="fa fa-map-marker"></i> <a onClick="giveThatInputAValue('<?php echo htmlentities($row_tournaments['location'], ENT_QUOTES); ?>')"><?php echo $row_tournaments['location']; ?></a><br>
    <img src="images/<?php echo $row_tournaments['online']; ?>.png"> <a href="#"><?php echo $row_tournaments['online']; ?></a>
    </div>
    <?php } while ($row_tournaments = mysql_fetch_assoc($tournaments));
    } else {
    ?>
    <div id="noresults"><p>Sem Torneios</p></div>
    <?php
    }
    ?>

然后让你的JS函数接收它作为参数:

功能

function giveThatInputAValue(location){
var elem = document.getElementById("search");
elem.value = location;
document.forms["form"].submit();}