1

What's the best way to display a success message after redirecting to same page? I've been thinking about doing that with javascript but maybe there's a way to do this with Php? The user submit from profile.php and gets redirected to same page. I'd like to grab a variable... Can I concatenate after $_SERVER['HTTP_REFERER']? Whats the best approach?

here a snippet of code: query.php

$stmt->execute() or die(mysqli_error($db)); 

if($stmt){
 // echo "Data Submitted succesfully";
  header('Location: ' . $_SERVER['HTTP_REFERER']);
exit;
  }
$stmt->close();
$db->close();
}
Sebastian Farham
  • 815
  • 2
  • 13
  • 27

4 Answers4

1

You can use sessions. Just start session, save message in global array $_SESSION, then in profile.php check if $_SESSION with your key is set and it isn't empty show it. After it you can unset your key in $_SESSION.

query.php

<?php
session_start();
//your code
if($stmt) {
    $_SESSION['myMessage'] = 'Some message';
    //your code
}
//rest of your code

profile.php

<?php
session_start();
//your code
if(isset($_SESSION['myMessage']) && $_SESSION['myMessage'] !== '') {
    //display message or do with it what you want
}
//rest of code
Wolen
  • 874
  • 6
  • 15
1

You could skip the session, and pass a url query parameter as a code or the message.

$stmt->execute() or die(mysqli_error($db)); 

if($stmt){
 // echo "Data Submitted succesfully";
  header('Location: ' . $_SERVER['HTTP_REFERER'] . '?message=success1');
exit;
  }
$stmt->close();
$db->close();
}

Then have code that checks for $_GET['message] ...etc

Perspective
  • 642
  • 5
  • 12
  • I was wondering... Is there a difference between `'?message=success1'` and `'&message=success1'` in terms of link? – Sebastian Farham Feb 26 '17 at 02:47
  • @SebastianFarham Yes. `?` indicates the **start** of a query string, which would be followed by a variable name, an `=`, and then its value. Subsequent variables are appended to the query string with an `&`, like so: `?var=value&var2=value2&var3=value3...`. – Bytewave Feb 26 '17 at 03:50
0

If you're processing your form in the same page, then you don't have to do any redirection. The solution to achieve the desired result would be like this:

  • Put your form processing code at the very top of your PHP script i.e. profile.php page.
  • Use a boolean variable to hold the status of ->execute() statement, and use that same variable at later point of your code.

So the code would be like this:

// Declare a boolean variable at the beginning
$status = false;

    // your code
    $status = $stmt->execute(); 
    $stmt->close();
    $db->close();
}

if($status){
    // Data submitted succesfully
}else{
    // Data couldn't get submitted
}
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
0

If you want to process the form at the same file, you don't need to redirect again to the same page.

As mention by the other answer, you process the form at the top of the page.

To display a message after success or failure, you store the message in a variable. Later with the from you echo the message variable if it is set.

// Check if form was submitted

  if(isset($_POST['submit'])) { // I name the submit button "submit"
      // process the form

      if ($stmt->execute()) {
          $message = "<div> Success </div>";
      } else {
          $message = "<div> Failed </div>";
      }
  }

// Display The form and the message if not empty

  if (! empty($message)) {
      echo $message;
     } 

// Form 
Hamoud
  • 1,909
  • 9
  • 13