Prashant Kumar
Prashant Kumar

Reputation: 249

Need to pass a variable in anchor tag

I am very new to php, I am having a problem passing data from textbox to a php variable to use it in an anchor tag. Below is the code i am using.

<form id="searchform" action="fetchvalues.php" method="get">
  <input name="q" id="q" type="text" />
  <input name="searchbutton" id="go" type="submit" value="" />
</form>

I want to pass value from searchbutton to anchor tag

<a href="http://www.example.com/?q=var" target="_blank">Hello"</a>

Upvotes: 0

Views: 5460

Answers (3)

Sunil Pachlangia
Sunil Pachlangia

Reputation: 2071

You can do like this

    <?php
        if(isset($_REQUEST['searchbutton']))
        {
    ?>
     <a href="http://www.example.com?q=<?php echo $_REQUEST['q']?>" target="_blank">Hello</a>
     <?php 
    }

?>

Upvotes: 0

atmd
atmd

Reputation: 7490

If you are using jquery in your project and want to do this on the front end, you can do:

<a id="someLink" href="http://www.example.com/?q=var" target="_blank">Hello"</a>

$('#someLink').attr('href', "http://www.example.com/?q=" + $('q').val());

With php you'd only be able to set the entered value of q on a post. (meaning when someone submits the form)

i.e.

<a href="http://www.example.com?q=<?php echo $_POST['q']; ?>" target="_blank">Hello"</a>

IF you need to populate the link href without a page refresh, you'll need to use javascript, if you want it to be populated after a form post, you can use php.

Be aware though that the link would need to be on the page set in your forms action attribute to populate the link

You should be aware that you take precautions when echoing out form submissions, however the level of questions suggests you've got more to learn before that. (No offense intended)

Upvotes: 2

AVM
AVM

Reputation: 592

<form id="searchform" action="fetchvalues.php" method="get">
<input name="q" id="q" type="text" />
<input name="searchbutton" id="go" type="submit" value="" />
</form>

On your fetchvalues.php page

 <?php
    $val = $_GET['q'];
 ?>

then

<a href="http://www.example.com?q=<?php echo $val; ?>" target="_blank">Hello</a>

Upvotes: 0

Related Questions