form text to be assigned as link

Asked

Viewed 47 times

1

I would like to know How to define the link through a form ex: I have a field to be filled, which is filled in this field you open on the link if 1234 is filled in, the link has to be www.exemplo.com.br/1234, if 12424 is filled in, the link you open has to be www.exemplo.com.br/12424

  • Why the PHP tag? What should be done in PHP?

  • 1

    If any answer solved your problem mark it as accepted. See how in https://i.stack.Imgur.com/evLUR.png and see why in https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079

  • Another post that may help you get better answers in your next questions is https://answall.com/help/mcve

2 answers

1

You can use the jquery to do this, I did it running but I believe it will work:

You put this script in your index.php or create a file .js (don’t forget to remove the tag <script> and </script> for this) and reference in your index which also works.

<script>
$(document).ready(function () {
  $("#botao").click(function () {
    var pegalink = document.getElementById('link').value;
      window.open("teste2.php/" + pegalink);
        });
 });
</script>

And add that to your index.php:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" name="link" id="link">
<input type="button" id="botao" name="botao">

And then just change the link to window.open for the page you want to assign the values in the URL(enviado.php, for example) and my values that are in the fields of script and inputs by yours that works normal.

1

Concatene the value of the input with the variable Url.

    $(function () {
        $('#button').on('click', function () {
            var text = $('#text');
            var Url='www.exemplo.com.br/';
            text.val(Url + text.val() );  
            //para funcionar corretamente add http:// aqui ou na var URL
            window.open("http://"+text.val());  
        });
    });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" value="" id="text" style="width: 250px;" />
    
<button type="button" id="button" name="button">Enviar</button>

Browser other questions tagged

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