Banana
Banana

Reputation: 4218

How to return success info from script in PHP

I have a page that sends a form to a PHP script. I would like to somehow return success information to the page when the script executes. Now I can't use jQuery AJAX requests since my form submits files to the server and I would like to avoid appending get parameters to the url if possible.

I tried doing this:

$_REQUEST['status'] = 'File uploaded successfully';

But when I check whether this variable is set on my page, I get false.

What is the best practice to do this?

Upvotes: 1

Views: 1605

Answers (3)

chirag ode
chirag ode

Reputation: 960

you can do like this...try it

header('Location: welcome.php?status=File Uploaded Successfully..!!');.

and then display success message using

<?php if(isset($_REQUEST['status'])) echo $_REQUEST['status'];
 header('Location: welcome.php');. ?>

or you can simply do this

header('Location: welcome.php?status=1');.
<?php if(isset($_REQUEST['status']=='1')) echo 'File Uploaded Successfully..!!';

Upvotes: 0

giorgio
giorgio

Reputation: 10202

First of all, you say I would like to avoid appending get parameters to the url. If that's your only concern, you can use jQuery.post(), no query parameters whatsoever.

But if you explained it correctly, it's just a normal form posted to a normal php script. Isn' t it? In that case, just echo out the success/failure info:

$succes = do_something($_POST['my_posted_var']);
if($success) {
    echo 'Yeah! It worked!';
} else {
    echo 'This is disappointing...';
}

Or redirect the user to your thank you page.

header('Location: '. thanks.php);

EDIT Ok got it (see comment). In that case, use sessions. You can transfer data between requests without juggling with parameters of some sort. Please read this. Summarized you could do something like below:

=execution_script.php=
session_start();
// do your stuff
$_SESSION['status'] = 'succes'; // or failure for that matter
header('Location: status_view.php');

.

=status_view.php=
session_start();
echo 'Status: ' . $_SESSION['status']; 

Upvotes: 2

Janak Prajapati
Janak Prajapati

Reputation: 886

1). you can rewrite url like "index.php?msg=$success" at your front page retrive this message by $_REQUEST['msg'].

2). you can use session flash messages that most of MVC frameworks are using try to google it abt it you will get more information about it

Upvotes: 0

Related Questions