Attach href to a button

Asked

Viewed 2,083 times

0

In a locker I have this link:

 <a href="chk-gerente.php?nro_pergunta=<?php  echo $nro_pergunta; ?>">Próximo</a>

And that button:

 <button  type="submit"  class="btn btn-default" >REPONDER</button>

Doubt: This is a system of questions, which list the question. The next link brings the new question and the answer makes the GET method by making the Insert in the bank. Is it possible to pass the next link inside the button by doing one thing? type,when I click answer he would do the Insert and soon after would bring the next question ? Something like this:

 <button a href="chk-gerente.php?nro_pergunta=<?php  echo $nro_pergunta; ?>" 

type="submit"  class="btn btn-default" >REPONDER</button>

I’ve tried that way and it didn’t work.

  • when you have a button, embed it in a form: <form action=YOUR_URL method=get> <input type=Hidden name=nro_pergunta value=<? php echo $nro_pergunta; ? > <button...> </form> see also: http://stackoverflow.com/questions/14461658/are-button-html-tags-outside-of-a-form-valid

  • Perfect, all right , Thank you.

3 answers

0


You can use a Jquery (or Javascript) to make the link opening standard with the href in button

    $("button").click(function() {

      if ($(this).attr("href")) {
        alert('Irá redirecionar para ' + $(this).attr("href"));
        window.location.href = $(this).attr("href");
      }

    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button href="chk-gerente.php?nro_pergunta=1" type="submit" class="btn btn-default">REPONDER</button>

However for your specific case you can create a input hidden and then redirect the person.

For example:

HTML:

<form action="" method="get">
<input type="hidden" name="proxima" value="<?= $nro_pergunta ?>" />
<button type="submit"  class="btn btn-default" >REPONDER</button>
</form>

PHP:

<?php

if(isset($_GET['proxima'])){

//...

$pergunta = (int)$_GET['proxima'];
// Número da pergunta (se não for número remove o (int)!)

header('location: chk-gerente.php?nro_pergunta='.$pergunta);
// Redireciona    
}

?>

That way you’ll get nro_pergunta of hidden and will redirect to the chosen URL.

0

Thank you guys for the tips,I tested and gave you the right ones, but the one that looked exactly like I needed was: @hgfdgd :

when you have a button, embed it in a form: <form action=YOUR_URL method=get> <input type=hidden name=nro_pergunta value=<?php echo $nro_pergunta; ?> <button...> </form> see also:

Thank you all more. Hug.

-2

Puts the function <a> between him. Solved my problem

<a><button></button></a>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.