Forum Moderators: open
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?
if (mysql_fetch_row($result)) { # Data saves successfully
echo '<script type="text/javascript">
alert(\'Changes Successfully Made\');
window.location = "'.$originatingpage.'";
</script>';
exit;
}
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.
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.
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.