简体   繁体   中英

How to redirect page using $_SERVER['REQUEST_URI']

I'm building a page that has a form that's submitting to my database. The form needs to post to the current page to process, but since I'm using this same form on many pages, I would like a way to tell the form to use the user's current page to submit to. I know the code below can get this information, it's what I'm using to send url data to my database for another use.

$_SERVER['REQUEST_URI']

I'm just not sure how to use that within the code that loads the page. I suppose I could write something that will pull out the submitted url info that was just submitted to my database and then plug it in, but is there a way to just use the above code to do it? That seems easier, but when I tried plugging it in, it fails and just gives me a "404 not found" error, so I know I'm not doing it correctly.

It just needs to go in the simple form post action code below:

<form action="comment_box.php" method="post">

I've looked up similar questions, but I couldn't find anything that worked with what I needed, I just got more 404 errors. Any advice would be great, thanks!

If your posting to the same page the user is on you can do

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

Leaving the form action blank it will post to what ever the page the form is on.

Not sure what language your using but I assume php you then use

 <?php
 if($_SERVER['REQUEST_METHOD'] == "POST") {
 // form submit button was pressed your php code here
 } else {
 // no form submit button was press do nothing maybe show form html code here
 }
 ?>

or what your trying to achieve

$url = $_SERVER['REQUEST_URI'];

Then in PHP

<form action="<?php echo $url; ?>/comment_box.php" method="post">

for example you have a following code

<input type="submit" name="click_me" value="Login">

then in the php, you have to check like

<?php 
  if(isset($_POST,$_POST['click_me']) && $_POST['click_me']="Login"){
     //do your stuff
  }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM