Dany
Dany

Reputation: 41

On click submit go to an url from field

I have a simple form:

<form class="form-contact-warp form-calc-ship cb-form" action="javascript:redirect();">
  <input class="form-control" required="" id="textBox" type="text" name="code" size="15"  placeholder="USI-TECH affiliate ID">
  <button type="submit" class="btn-main-color btn-block"> Create page</button>
</form>

If I put in input field the text daniel, I want to append a link.

Example:

I put daniel in and I click submit. I want to appear below the link www.example.com/daniel and the text This is your link.

Thanks

Upvotes: 3

Views: 102

Answers (1)

Rasclatt
Rasclatt

Reputation: 12505

I think the best way to do this is to use jQuery or JavaScript by itself:

<form class="form-contact-warp form-calc-ship cb-form" action="" method="post">
  <input class="form-control" required="" id="textBox" type="text" name="code" size="15"  placeholder="USI-TECH affiliate ID">
  <button type="submit" class="btn-main-color btn-block"> Create page</button>
</form>
<div id="response"></div>
<!-- add jquery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(function(){
    $('form').on('submit',function(e){
        e.preventDefault();
        $('#response').html('This is your link:<br />http://www.example.com/'+encodeURI($('#textBox').val()));
  });
});
</script>

Here is a jQuery demo found at jsFiddle

Here is the same thing, only it changes when you type.

If you want to use purely PHP, you need to check something has been submitted:

<form class="form-contact-warp form-calc-ship cb-form" action="" method="post">
  <input class="form-control" type="text" name="code" size="15"  placeholder="USI-TECH affiliate ID">
  <input type="submit" class="btn-main-color btn-block" value="Create page" />
</form>

<?php
# Check if there is a post
if(!empty($_POST['code'])) {
    # You want to make sure you remove possible html and covert the string to a url.
    echo 'This is your link:<br />http://www.example.com/'.urlencode(trim(strip_tags($_POST['code'])));
}

Upvotes: 1

Related Questions