当我按下<a> Attribute

时间:2015-06-05 15:06:00

标签: php forms search onclick submit

Ok guys already search for it but no results at all, and cant figured it out how to do this, so my problem is:

I have this 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>

And what i would like to is when i press this:

<a onClick="">City</a>

The input value change to what i want in this case "City" and then automatically submit the form.

I already made a research about this and theres nothing i could take, any help would be appreciated.

Cumps.

Guys thanks to your help now its working but someone tell me please what the problem in this code:

<?php if (mysql_num_rows($tournaments) !=0){
     do { ?>
     <div id="mainContainer">
     <div id="leftContainer"><img src="images/tournaments/<?php echo $row_tournaments['logo']; ?>.png"></div>
     <div id="rightContainer">
     <div id="rightContent">
     <p><?php echo $row_tournaments['description']; ?></p>
     <a href="tournaments.php?id_tournament=<?php echo $row_tournaments['id_tournament']; ?>"><div id="galleryButton"><p>Entrar no torneio</p></div></a>
     </div>
     <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>

<script type="text/javascript">
function giveThatInputAValue(){
    var elem = document.getElementById("search");
    elem.value = "<?php echo $row_tournaments['city']; ?>";
/*document.forms["form"].submit();
*/}
</script>
<i class="fa fa-map-marker"></i> <a onClick="giveThatInputAValue()"><?php echo $row_tournaments['city']; ?></a><br>


<img src="images/<?php echo $row_tournaments['online']; ?>.png"> <a href="#"><?php echo $row_tournaments['online']; ?></a>
</div>
</div>
</div>
<?php } while ($row_tournaments = mysql_fetch_assoc($tournaments));
} else {
?>
<div id="noresults"><p>Sem torneios</p></div>
<?php
}
?>

Everything is looped fine but that part:

<script type="text/javascript">
function giveThatInputAValue(){
var elem = document.getElementById("search");
elem.value = "<?php echo $row_tournaments['city']; ?>";
/*document.forms["form"].submit();
*/}
</script>

Its not looping it only shows the first record why is it?

Any help would be appreciated.

1 个答案:

答案 0 :(得分:0)

在您的js文件中,您可以检查文本框的值是否等于您想要的城市名称。如果是,那么提交表格。将功能附加到a tag

function SubmitRightCity(){
   if(document.getElementById('search').value == "citynamehere"){
        document.forms["form"].submit();
   }
}

in html

<a onClick="submitRightCity()">City</a>

如果您希望a taginput提供一个值,则可以在标记中附加一个函数,一旦单击该链接,该函数将为您的输入提供值。

<a onClick="giveThatInputAValue()"> City</a>

在javascript文件中你会有

<script>
    function giveThatInputAValue(){
         var elem = document.getElementById("search");
         elem.value = "My city value";

         //if you want to submit the form right after, you add the line below
         document.forms["form"].submit();
    }
</script>