0

How to change the get method url in php. Here is my code

<form method="get" action="offers.php">
  Name :<input type="text" name="name"><br>
  Subject : <input type="text" name="sub"><br>
  <input type="submit" value="Submit">
</form>

and in offers.php file code

<?php
  $name = $_REQUEST['name'];
  $subject = $_REQUEST['sub'];
?>

After giving the input values shows url "http://localhost/htaccess/offers.php?name=raj&sub=kumar" but i want to show the url is http://localhost/htaccess/raj/kumar. How to solve this issue. how to convert this url. Here raj and kumar are input given values not for default.

narendra
  • 9
  • 2
  • 11
  • If you want the form to go to that url, after you have setup rewrite rules to handle the URL like that, you would need to use JavaScript to change the form action on form submit. This isn't something automatically handled by the browser as it isn't a standard thing to be done. It is purely cosmetic. – Jonathan Kuhn Dec 27 '16 at 17:55

2 Answers2

1

Hmm, you have a couple of things going on. First, to get rid of the ?name=raj&sub=kumar string on your URL

Use POST not GET

<form method="POST" action="offers.php">
  Name :<input type="text" name="name"><br>
  Subject : <input type="text" name="sub"><br>
  <input type="submit" value="Submit">
</form>

Next, you have a whole other world of questions in using rewrite rules to make offers.php change to raj/kumar. Try a few of these links to get you there:

The basic premise is this:

offers.php will still be requested until you change your form to look like this:

<form method="POST" action="raj/kumar">
  Name :<input type="text" name="name"><br>
  Subject : <input type="text" name="sub"><br>
  <input type="submit" value="Submit">
</form>

but that won't work until you htaccess and mod_rewrite working for you to "rewrite" that raj/kumar url to be "handled" by offers.php

Community
  • 1
  • 1
WEBjuju
  • 5,797
  • 4
  • 27
  • 36
  • They will need to use JS to modify the action of the form (or just `location.href` to the path). What they are looking for is to change the url to something like `/{name}/{sub}/`, not just change the URL to a static `raj/kumar` or change from GET to POST. – Jonathan Kuhn Dec 27 '16 at 18:01
  • raj and kumar is giving input values – narendra Dec 27 '16 at 18:01
0

Change get to post.

Change $_REQUEST to $_POST

Also, put the $_POST data within a conditional, like:

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
  $name = $_POST['name'];
  $subject = $_POST['sub'];
} else {
  // form was not posted
}
Brad
  • 12,054
  • 44
  • 118
  • 187