1

How to pass variable values into url. Here is my code

<?php
      if(isset($_POST['submit'])){
       echo $v=$_POST['n'];
      } ?>
<form method="post" action="/user/<?php echo $v; ?>">
<input type="text" name="n">
<input type="submit" name="submit" value="Submit">

After giving input value and click on submit shows in url:

if input value is raj after click on submit in url i want to display

/user/raj

narendra
  • 9
  • 2
  • 11

2 Answers2

2
  1. The requirement can be achieved by using javascript.

    <form id="myForm" method="post" action="" onsubmit="return false">
    <input type="text" name="n">
    <input type="submit" name="submit" value="Submit">
    </form>
    

    using jquery

    $("#myForm").on("submit",function(){
     var val=$("input[name='n']").val();
     $(this).attr("action","<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>/user/"+val);
      $(this).submit();
      })
    
geekbro
  • 1,293
  • 12
  • 13
  • 1
    when the submit is clicked the form will look fro a user directory so to prevent that change the action to be : `$(this).attr("action","/user/"+val);` – Masivuye Cokile Nov 24 '16 at 10:59
0

Use method="get" in your form tag.

Stephan
  • 594
  • 7
  • 17
  • in url shows user/?n=raj&submit=Submit i want to display /user/raj. – narendra Nov 24 '16 at 10:41
  • @narendra u will need to re write the url with .htaccess – Masivuye Cokile Nov 24 '16 at 10:43
  • my file name is 36a07d684f5d8e0a9b572b74bf9cb1c101ac3337.file.header.html then how to write this code. – narendra Nov 24 '16 at 10:46
  • @narendra Based on your code, when you load the page $_POST is empty so your action will be /user/ . after this, when you write 'raj' in input box and post the form, $_POST['n'] contains raj and action will be 'user/raj/' – Stephan Nov 24 '16 at 10:58