Search code examples
phpsuperglobals

PHP_SELF not working if URL has $_get value?


My page url is something like http://localhost/coursera/instructor/course.php?id=1&name=web%20development&section=overview

and on that page i have a form like

<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">       
 <div class="form-group">
  <textarea class="form-control" placeholder="What will students need to know or do before starting this course? Who is your target student? At the end of my course, students will be able to..." rows="8" required="" name="description"></textarea>
</div>                                   
 <button type="submit" class="btn btn-info btn-fill btn-block" name="overview">Save</button>  
</form> 

But the problem is <?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> and <?php echo $_SERVER["PHP_SELF"];?> are not returning the same url with all the $_Get values.

Anyway to make form return to same page bacause I can't hardcode $_Get Values.

Thanks.

Edit:

print_r($_SERVER["PHP_SELF"]); 

is showing /coursera/instructor/course.php but I want http://localhost/coursera/instructor/course.php?id=1&name=web%20development&section=overview any way to do it?


Solution

  • Update 1:

    I want that when I submit the form i should return to same url but it is not

    You can simply keep the action attribute empty in case you want the form to return to the same page.

    <form method="post" action="">
    

    Update 2:

    While it's working, according to the W3C documentation it's not a valid value.

    The action and formaction content attributes, if specified, must have a value that is a valid non-empty URL potentially surrounded by spaces.


    Please notice, PHP_SELF doesn't return the query string (?x=y&z=d).Just the dir and the file name.

    PHP_SELF

    The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/foo/bar.php would be /foo/bar.php. The FILE constant contains the full path and filename of the current (i.e. included) file. If PHP is running as a command-line processor this variable contains the script name since PHP 4.3.0. Previously it was not available.