PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
anyone know why this code which ive written doesnt work?? basically what im trying to do is after a submit button is pressed,using a bit of javascript code,an alert box is shown to say that submit has been pressed. this works fine. however say if i want to redirect to a new page after submit has been pressed i need to include a header after the javascript and the i location of the new page. with this included it fails for some reason. here is the code:
<?php
//need this otherwise the header wont work
ob_start();
echo "<form method = 'post'>
<input type = 'submit' name = 'submit' value = 'submit'>
</form>";
if(isset($_POST['submit']))
{
echo "<script language = 'javascript'>alert('You have pressed submit and are now being directed to another page')</script>";
header("location:somepage.php");
}
?>
Why inform the user when has submit has been pressed ?
Surely the action is to be noted, not the submit button pressed. (I only want relevant information when using the web, and I want to avoid key presses which don't do anything).
Your information has been saved...
Your information has been updated...
Your information failed to be updated because XYZ...
In these instances you could store the message in a session variable, redirect the page if necessary (normally only on DB insert or login). All pages look at the session variable if $_POST is empty and if necessary output it before making it empty.
<?php
//need this otherwise the header wont work
ob_start();
echo "<form method = 'post'>
//some field allowing user to update information
<input type = 'submit' name = 'submit' value = 'submit'>
</form>";
if(isset($_POST['submit']))
{
//some functions that update some value in the database
$result = updatedatabase($_POST['value']);
if($result != "successfull")
{
echo "<script language = 'javascript'>alert('The database update failed')</script>";
exit;
}
else
{
echo "<script language = 'javascript'>alert('You have successfully updated information')</script>";
header("location:somepage.php");
}
}
?>
this should hopefully give you more of an idea of what im trying to do. If the database update fails the javascript will run and the alert box will show. however if the update is successful the page will just be re directed to the page specified in the header without showing the javascript alert box it should show. is there anyway of making this work? the method you descibed about using sessions sounds a bit complicated. is there anyway of making it work by using code within the script? I thought it may be something to do with output buffering perhaps??
ok thanks for that, got it working! just one question for you: whats the difference between location.replace() and window.location(). they seem to perform the same function?