-5

Listen, I've been stressing for 5 hours this will be helpful if you guys could understand and help me

I need a simple PHP code that when I input a text, and then press submit it redirects to my website with the text that I entered in the box, so basically i want it to write the Text that I put, into the URL (Example: www.site.com/[name]) will be kinda like www.site.com/rob.

This is my form HTML Code

<form name="form" method="post">
<input type="text" id="text_box" name="text_box" placeholder="Username"><br>
<button name="submit" class="button buttonlol">Search</button>

If you guys could figure this out for me, it'll be very helpful thank you.

Qirel
  • 25,449
  • 7
  • 45
  • 62
  • 2
    This format, StackOverflow, isn't for "asking for code". We'll gladly help troubleshoot, but questions asking for code are off-topic, as they generally are too broad to answer. There are examples in the PHP manual which may be of use to you, http://php.net/manual/en/tutorial.forms.php – Qirel Dec 16 '16 at 11:56
  • If you want to have the URL in the format like `site.com/word` you'll have to use .htaccess. If you would be okay with a `site.com/script.php?word=YourWord?` then it'll be quite simple with PHP. But as Qirel said, we're here to help you with YOUR code, not to write code for you. – Twinfriends Dec 16 '16 at 11:59
  • You could use Javascript to intercept the form submission and the use it to redirect otherwise with php you could look at something like: http://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php – Rwd Dec 16 '16 at 11:59
  • @Please Help Nice user name :) – TIGER Dec 16 '16 at 12:10

2 Answers2

1

What you want is to use a form method that puts the search request into the address bar, and then an .htaccess file that re-writes the URL and redisplays in the format you desire.

Change your form HTML code to use a GET request -- that will write the search into the address bar.

<form name="form" method="get">

This will put the name-value pairs in the form into the URL instead of posting them invisibly in the background.

Next, look into using an .htaccess file to re-write that URL as desired.

References:

https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/

http://www.noupe.com/development/htaccess-techniques.html

cssyphus
  • 37,875
  • 18
  • 96
  • 111
1
if(isset($_POST['text_box']))
{
    $username = $_POST['text_box'];
    header("location: http://example.com/$username");
}

You can try this.

I will explain what it does:

  • It checks if $_POST['text_box'] is set.
  • If it is set, it will declare a variable $username to the post.
  • Then, the header location will send you to the desired link, and you can include you're variable with it.
Mitch
  • 1,173
  • 1
  • 10
  • 31