简体   繁体   中英

How do I give a value to an href as its done to a submit?

I named a submit and an href and when I test the conditions submit works but the href does not and there is no error displayed . Please any help will be appreciated Bellow is the code

<form action="test.php" method="post"/>
<a href="#" name="amhref" >Href</a>
<input type ="submit" name="button"  value="button">
</form>
<?php
if(isset($_POST['amhref'])){
echo ("<h1>I am href </h1>");
}else if(isset($_POST['button'])){
echo ("<h1>I am the button</h1>");
}
?>

a link <a> is not sent with a form. so any data you would like to pass should be done through the href field. this is done with a get method, so please note the $_GET

<form action="test.php" method="post"/>
<input type ="submit" name="button"  value="button">
</form>
<a href="?amhref" name="amhref" >Href</a>
<?php
  if(isset($_GET['amhref'])){
    echo ("<h1>I am href </h1>");
  }else if(isset($_POST['button'])){
    echo ("<h1>I am the button</h1>");
  }
?>

Using an <input> control to achieve this. I have used a hidden control.

<input type="hidden" name="id1" value="hello">

You can't do with an <a> tag

tag not element of form type so that you cant pass to in form submite. If you want to pass keep amhref value in input type text or Hidden.

You can pass value with href with GET method also.

<form action="test.php" method="post"/>
<input type ="submit" name="button"  value="button">
</form>
<a href="?amhref" name="amhref" >Href</a>

<?php
  if(isset($_GET['amhref'])){
    echo ("<h1>I am href </h1>");
  }else if(isset($_POST['button'])){
    echo ("<h1>I am the button</h1>");
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM