1

I'm struggling with this problem and can't figure it out. Here's what I've done:

Form:

<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 looping:

<?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:

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

So when i press,

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

The input value will change the value from input and submit form after, but the problem remains: how can i put this function inside the loop, so the value changes to the proper value from database?

This value is being loaded correctly: <?php echo $row_tournaments['location']; ?>

But outside the loop it always shows the first result from the database.

Please help can't figure it out.

luk2302
  • 55,258
  • 23
  • 97
  • 137
Bruno
  • 99
  • 1
  • 10
  • You should [stop using mysql_functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) and switch to `mysqli` instead – Machavity Jun 05 '15 at 21:46
  • Change the function to `giveThatInputAValue("")` and catch it as an argument in the javascript function – adeneo Jun 05 '15 at 21:46

1 Answers1

0

You'll need to repeat the value for each call to the JS function:

php looping:

    <?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
    }
    ?>

and then make your JS function receive it as a parameter:

function:

function giveThatInputAValue(location){
var elem = document.getElementById("search");
elem.value = location;
document.forms["form"].submit();}
Marcos Dimitrio
  • 6,651
  • 5
  • 38
  • 62