简体   繁体   中英

PHP URL redirect using header

I'm trying to use one update_form.php page to handle 3 separate but identical areas of a site. So for example:

I have 3 data pages: data1.php, data2.php, data3.php . All link to update_form.php. Once update_form.php is submitted and new data is stored in database, what code would I use to redirect back to corresponding data page.

If I'm updating data1.php , after I submit the update form, I want to go back to that page. Need to do this using PHP and MYSQL. Any help is appreciated.

Here is the code I am working with from Dreamweaver. Can you help me edit to do what I mentioned above. Currently I can only redirect to data1.php no matter what form I enter from:

$updateGoTo = "data1.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
  }

One way to do it would be using Ajax. You simply post your data to update_form.php and update the page accordingly. You won't have to deal with redirects.

For instance, with jQuery it would be similar to this:

$.post("update_form.php", { field1: "value1", field2: "value2" })
  .done(function(data) {
    // update your page here
  });

If you don't want to go that route, what you can do is pass the URL of the page along with the other form params. You can add it as an hidden input in your form.

<form action="update_form.php" method="POST">
  <input type="hidden" value="[URL]" name="redirect_url">
  <input type="text" value="OTHER VALUE">
  ...
</form>

Then on your server, you can retrieve the URL in $_POST['redirect_url'] and do:

header('Location: ' . $_POST['redirect_url']);

Just make sure to sanitize the redirect_url or this can be a security hole :)

By using php/mysql simply just use header(); as follows

example take data1.php has below code

 <?php session_start();  // session value to check the return msg from update_form.php
  if(!empty($_SESSION['msg']){ 
      echo  '<div style="">'.$_SESSION['msg'].'</div>;     
       unset($_SESSION['msg']); // destroy msg after being visible to user 
     }  ?>
<html>
<form action="update_form.php">
  <input type="text" name="FullName" pattern="" />
  <input type="submit" value="update" />
 </html>

then in update_form.php you will have the following code.

<?php  session_start();
  //Run your DB connection, eg i use mysqli

if(!empty($_POST)){

   $name = mysqli_real_escape_string($link,$_POST['FullName'];
   //run your update query successful
    $msg = "Data updated successfully..";
      }
 else{ 
    $msg = "Updation of data encounter error plz try again..";
         }
 $_SESSION['msg'] = $msg; // variable to return msg to user in other page 
 header("location: data1.php"); //this functin require page reload, if you know jquery is the best opt for this

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