Forum Moderators: open

Message Too Old, No Replies

Simple alert box after successful MySQL save

before exiting the site.

         

salewit

7:50 pm on Sep 12, 2009 (gmt 0)

10+ Year Member



I know zilch about JS. I'm looking for a simple alert box to pop up after a successful MySQL save operation, and BEFORE a PHP header command. Here's the code.


if (mysql_fetch_row($result)) { # Data saves successfully

#### I want to inject a JS alert box HERE that simply says "Changes Successfully Made" or something like that.

header ("Location: ".$originatingpage);
exit;
}

As you can see, there is nothing to trigger the alert box. No submit buttons, no body tags, etc. Is this doable?

whoisgregg

8:07 pm on Sep 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's impossible to send a header() after you've already sent output of any kind to the client. That said, you could use javascript to do your redirect:

if (mysql_fetch_row($result)) { # Data saves successfully
echo '<script type="text/javascript">
alert(\'Changes Successfully Made\');
window.location = "'.$originatingpage.'";
</script>';
exit;
}

rocknbil

8:45 pm on Sep 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I may, I think the approach here is what's wrong. I see this a lot with PHP coders: do a function, redirect to another PHP script.

Why can't you put it all in one PHP script/page, no redirect, no alert?


(doctype)
<html><head><title>test</title></head>
<body>
<?php
if (isset($_POST['submitForm'])) {
$errors = '';
$errors = check_input_data();
if ($errors!= '') { $content = form_output($errors); }
else {
// process data
$content = '<p>Successfully added</p>';
// concatenate whatever's in the "location" page
// to $content here
}
}
else { $content = form_output(); }
echo $content;

function form_output($err) {
$form = '<form method="post" action="this-script.php">';
if ($err) {
$form .= '<p style="font-weight:700; color:#ff0000;">' .
' Please correct the following errors:</p><ul>' .
$err . '</ul>';
}
// proceed with contcatenating $form
$form .= '<p><label for="email">Email</label>' .
' <input type="text" name="email" id="email" value="' . $_POST['email'] . '"></p>';
// etc.
$form .= '</form>';
return $form;
}

function check_input_data() {
$dataErrors = '';
// do all your data cleansing and validation here
// concatenate errors into a list, that is, you can
// report all errors as a list instead of dying on the
// first one. Example:
// if ($_POST['email'] == '') {
// $dataErrors .= '<li>The email field is required.</li>';
// }
return $dataErrors;
}
?>
</body>
</html>

- No Javascript
- No redirect
- Success message
- Error response, if there is one, bringing back to form, no cheesy go back links
- all in one file.

salewit

9:11 pm on Sep 12, 2009 (gmt 0)

10+ Year Member



Thanks w-

r- Yes you're absolutely right, and I have learned to do that as my skills have progressed. This particular situation is the modification of a pretty large website that is already in place where they wanted a new function to allow users to edit their user settings. So I built a page called settings.php, which can be reached from any page and *thought* the slick thing to do would be to return the user to the page they were on before they came in to change their settings. But how to let the user know that their setting change was successful before taking them back to the page they were on? If any of that makes sense.

eelixduppy

9:38 pm on Sep 12, 2009 (gmt 0)




return the user to the page they were on before they came in to change their settings.

Just keep track of what the referring page was, and when it's time to redirect them back, you'll have it right there. Shouldn't be a problem to pass that referring page along each time you want to go to another page before redirecting back.