Forum Moderators: coopster

Message Too Old, No Replies

how to pass a removable message

problems with js independence

         

Skier88

7:37 pm on Sep 14, 2010 (gmt 0)

10+ Year Member



My goal is simple enough, but I'm having trouble realizing it. After a user completes an action (say, submitting a review) I want to redirect them to the relevant page (in this case probably the review page, NOT a redirect page) with an short alert displayed (in this example, "Review successfully submitted"). I want to implement two methods of closing the html box - hiding it with javascript (without reloading), or a link to a non-alerting version of the url as a fallback.

This would be simple enough, but I also want the javascript to change the url to something that won't display the alert, so that if the user refreshes or links to the page after closing the alert it won't pop up again. As far as I know the only way to do this is with window.location.hash, but php can't see this and therefore wouldn't create the alert in the first place. Relying on js to display the alert is not an option.

Thanks for taking a look. Any suggestions?

onlinetraining

6:54 pm on Sep 15, 2010 (gmt 0)

10+ Year Member



After the user completes the form submission, you can redirect him/her to a page of your choice say "review.php".
You could have:
header('location: review.php?sb=ok');

in the PHP file processing your review submissions.

In the review.php file, within the body area, you can have a box where the confirmation message would be displayed.

<?php if(isset($_GET['sb']) && $_GET['sb']=='ok'): ?>

<script language="javascript" type="text/javscript">
<!--
function HideAlert(){
document.getElementById("alertbox").style.display="none";
clearTimeout(delai);
}

var delai=setTimeout(HideAlert, 5000);
-->
</script>

<div id="alertbox" style="display:block;">Review successfully submitted</div>
<?php endif; ?>

Feel free to create a style for #alertbox in your CSS stylesheet. Just make sure the display property is set to block.

Skier88

7:21 pm on Sep 15, 2010 (gmt 0)

10+ Year Member



Thanks for the reply, but there's one part that I'd like to fix. Let's say the user submits the review and is redirection to review.php?sb=ok, and after 5 seconds the prompt hides itself: if they then copy the link or refresh the page, the alert will show up again.

But that if statement syntax is useful - I had been using really long echo statements before.

rocknbil

4:25 pm on Sep 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why do you need to redirect at all? Or use Javascript in this way?

<form method="post action="submit-something.php">


if ($_POST) { output_form(null); }
else {
$errors=null;
$errors = check_your_data();
if ($errors) { output_form($errors); }
else {
process_form();
output_response();
}
}
//
function output_form($errorlist=null);
// Here you compose the form, and
if ($errorlist) { $form .= "<ul>$errorlist</ul>"; }
// continue with form
header("content-type:text/html");
echo $form;
exit;
}
//
function check_your_data(); {
$data_errors=null;
// do data checks, if error,
// compile them like so
// $data_errors .= "<li>The email field is required.</li>";
return $data_errors;
}
//
function process_form() {
// update database, send emails, etc.
// No return value unless you need
// more error trapping
}
//
Output your response, no redirect
function output_response () {
header("content-type:text/html");
echo '<p>Success</p>';
exit;
}


You'll have to open a template to output in the two two output functions but this would be a more solid app and easier to maintain.

Skier88

7:47 pm on Sep 17, 2010 (gmt 0)

10+ Year Member



Thank you for your reply. I know that what you suggest is a standard and effective approach; however, were I using the website I would find it more helpful to view the alert in a html popup on the page I just created or modified, and I don't think I'm alone. So if it is possible that is what I would like to implement.

holyhttp

8:41 pm on Sep 17, 2010 (gmt 0)

10+ Year Member



By HTML popup do you mean a pop-up window, a javascript alert box or something else. The solution proposed by "Onlinetraining" can be quickly adapted to display a popup window on the same page the form was submitted from or the new page the user is redirected to.

To answer the refresh issue you pointed out above, you have say:
<?php if(isset($_GET['sb']) && $_GET['sb']=='ok')&& (!isset($_COOKIE['norefresh']): ?>

setcookie('norefresh', 'ok' time()+3600, '', '', 0);

<script language="javascript" type="text/javscript">
<!--
function HideAlert(){
document.getElementById("alertbox").style.display="none";
clearTimeout(delai);
}

var delai=setTimeout(HideAlert, 5000);
-->
</script>

<div id="alertbox" style="display:block;">Review successfully submitted</div>
<?php endif; ?>

Skier88

9:15 pm on Sep 17, 2010 (gmt 0)

10+ Year Member



By HTML popup I mean a div styled to overlay the body and be "closeable". I realize that changing the method of displaying the alert is trivial; what I'm trying to figure out is how to display it only once without redirecting.

Thanks for the cookie tip - I'll probably end up using some variant of that. But I'm still hoping there is some way to do this with just the URL - I'm hoping that part of the URL does not refresh the page when changed, and is accessible by both the client and server.

I'm also considering submitting whether the user has JavaScript enabled with the review, then relying on it (to prevent display on refresh, not to display in the first place) if they do.