views:

146

answers:

3

I have a page where users submit a form, and it goes to a separate PHP script. After the script is done, it header() redirects the user back to the page they were on. Now what I want to do is if there is an error or certain conditions in the script, redirect the user to the same page but display a Javascript alert once they get there as a warning message. I could append some variables to the URL and check for that with $_GET but I figure there is probably an easier way... perhaps with some POST data, or something like that? Thanks

+2  A: 

You can do this all on the server side by using the session:

  1. "Show form" php script creates the form and returns it to the user.

  2. User fills it out and submits it to another php script "receive script" which receives the form data, and notices an error, missing data, etc.

  3. The "receive script" stores the error msg in the session (as item err) and redirects to the 'show form' script.

  4. The "show form" script (same as in step 1) actually does more than create the form. It also:

    • looks in the session to see if it has an item 'err', an error msg. In step 1 there wasn't. But now there is. So the php script creates the form, along with a div that shows the error msg to the user.
    • Resets the session's 'err' item to nil.
    • The php script could also include javascript in the page which would make the error msg disappear after a while or be shown as a popup, etc.

ps. The above flow is how rails handles forms and redisplay of the form.

Update: Thanks to @zod for pointing out that I wasn't clearing the err item in the session.

Larry K
That's not a bad idea. I could definitely use sessions. I'm not sure I'd do it that exact way, but it could still work. Thanks. I'm still open to more ideas though.
RobHardgood
if you use session . At what point you are unsetting the session. You just want to show an alert message on error. For this why you should use session
zod
if you dont unset the session the error value is there in session till it expires!
zod
+1  A: 

If an error is encountered, store the error state to a $_SESSION array and then redirect the browser to the original page. Have a script on the original page to check if an error state is set. If yes, trigger a javascript alert or whatever handling you want to have.

And at the common footer template (or at the footer of original page), check and clear the errors array, so it doesn't persist when the user moves to other pages or reloads the current page.

Example:

processor.php

<?php
if($something == $iswrong){
    $_SESSION['errors']['error5301'] = 1;
    session_write_close();
    header("Location: http://www.example.com/originalpage.php");
    exit;
} ?>

originalpage.php

<!-- Header -->
<?php
if(isset($_SESSION['errors']['error5301']) && $_SESSION['errors']['error5301'] == 1){ ?>
    <script type="text/javascript">
        alert('Something is not correct!');
    </script>
<?php } ?>

<!-- Some page content -->
....
.....
..
......

<!-- Footer -->
<?php
if(isset($_SESSION['errors'])){
    unset($_SESSION['errors']);
} ?>

Hope that helps.

Nirmal
Looks like Larry K posted his answer parallely! We are talking the same thing anyway.
Nirmal
@Nimal yep! This is more like I think I'll do, based on Larry's idea which I read first. One problem is that I have other sessions going on that I don't want to destroy, but I'd like to destroy any error sessions so users don't keep getting errors... is that what unset() does? If I run it on the page below where it checks for it, it will still display my alert message and then destroy the session, right? Thanks, this is a very useful post.
RobHardgood
We are not using `session_destroy()` which will just destroy the entire session cookie. Instead, we use `unset()` to clear just the error stack that is part of the current session. And the idea of maintaining the errors as a stack is that you can store as many error states as you want and trigger the appropriate events on the original page. It would come handy when you are dealing with forms: `error5301` can be an empty first name, `error5304` can be an invalid email, and so on. To answer your query, yes, the page will display the alert and then clear the error state.
Nirmal
Could you explain how a stack works? I've never encountered it... even though it's in the name of this site. From what I understand, it essentially saves multiple variables within the same session variable?
RobHardgood
I just say a mutidimensional array as "stack" :-) Sorry if I confused you.
Nirmal
I have edited the answer to refer to them as array.
Nirmal
A: 

first page

<script>
onsubmitfunction()
{

document.getElementByid('errorid').value=1;

}
</script>
<form name='' id='' onsubmit="javascript:onsubmitfunction();">

<input type='hidden' id='errorid' value=''>
</form>

    In destination.php

    <?php
    if($_POST['error']==1)
    {
    ?>
    <script language='javascript'>

    alert('Errrrrorrrr');
    </script>

    <?

    }

    ?>

This is enough for your question.

As per my understanding

zod
I said in my post that I considered using this method, but I'd rather use something more invisible. Thanks anyway though.
RobHardgood
on submiting the form am setting value of a hidden field and posting. in the destination page am checking the post variable. Will this work for you . Check the synatxes
zod