1

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?

Banana
  • 4,010
  • 9
  • 33
  • 49
  • how are you getting $_REQUEST['status'] ??? – chirag ode Sep 21 '13 at 10:41
  • $_REQUEST holds _incoming_ parameters. So if you are not using AJAX, then you send the form “normally”? Then the browser will try to _display_ the response, so it should be a normal HTML document. – CBroe Sep 21 '13 at 10:42
  • 1
    use $_SESSION instead of $_REQUEST – Farnabaz Sep 21 '13 at 10:42
  • @chiragode On the page that my script redirects to I do `if (isset($_REQUEST['status'])) echo $_REQUEST['status'];` – Banana Sep 21 '13 at 10:47
  • @Ganesh What I meant is that I do not want to append get parameters to the url that my script redirects to after executing. – Banana Sep 21 '13 at 10:50
  • @JanaBanana I think http://stackoverflow.com/questions/2320069/jquery-ajax-file-upload this link shall help your case – Ganesh Babu Sep 21 '13 at 10:56
  • How is the form sent ? Using action and POST ? If you can't use Ajax-style uploading how about having some kind of callback ? In short page calls script, script calls page. You could redirect using header. You could do a check using file_exists or something along those lines. – James P. Sep 21 '13 at 11:09
  • @JamesPoulson I'm already doing all of this, but how do you suggest I return the status? – Banana Sep 21 '13 at 12:06
  • @JanaBanana There is that possibility of doing a POST request using curl. Apache needs to be set up for it though so you need to check your phpinfo. http://davidwalsh.name/curl-post – James P. Sep 21 '13 at 14:53
  • Or if you need to go back to the page then save the return. Either in a session variable or in a database. Note that the thing to recognize here is that HTTP is stateless ;) – James P. Sep 21 '13 at 14:55

3 Answers3

2

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']; 
giorgio
  • 10,111
  • 2
  • 28
  • 41
  • What I meant is that I do not want to append get parameters to the url that my script redirects to after executing. My script end in `header('Location: example.com/sth')` and on this url I want to know whether or not the script was executed successfully. – Banana Sep 21 '13 at 10:49
  • This looks like the best solution. – Banana Sep 21 '13 at 12:06
0

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

Janak Prajapati
  • 896
  • 1
  • 9
  • 36
0

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..!!';
chirag ode
  • 950
  • 7
  • 15