Page 1 of 1

javascript in php

Posted: Tue Apr 12, 2005 8:35 am
by dannymc1983
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:

Code: Select all

<?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");
}

?>

Posted: Tue Apr 12, 2005 9:01 am
by neophyte
Problem: echo javascript and then adding a header statement. That's my guess. This should produce a headers already sent error...

Use javascript to change locations. window.location().

Hope this helps

Posted: Tue Apr 12, 2005 9:11 am
by phpScott
missing action in the form but that is not necessary, you can also use javascript to set the action location as well.

Posted: Tue Apr 12, 2005 9:23 am
by CoderGoblin
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.

Posted: Tue Apr 12, 2005 11:31 am
by dannymc1983
the code i provided is just a simplified version of a script i have created to update a database. ill try to elaborate on it here:

Code: Select all

<?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??

Posted: Tue Apr 12, 2005 2:01 pm
by Morthy
Why not use javascript instead of a php header?

Code: Select all

echo "<SCRIPT language=\"javascript\">";
echo "alert('You have successfully updated information')";
echo "location.replace('somepage.php')";
echo "</SCRIPT>";

Posted: Tue Apr 12, 2005 3:01 pm
by dannymc1983
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?

Posted: Tue Apr 12, 2005 3:05 pm
by Morthy
I don't know javascript that well, but maybe one has support for changing the location of pop up windows.

Posted: Tue Apr 12, 2005 3:08 pm
by Chris Corbyn
Morthy wrote:I don't know javascript that well, but maybe one has support for changing the location of pop up windows.
No...

window.location => is an object
window.location.replace() => is a method

But yes... they do do the same thing if you just want a redirect.

Also, replace() can take a regexp as an argument. Therefor you can do some celever things with it ;-)

But lets say you wanted JS to know what the URL is.

I hope you see what I mean

Code: Select all

document.write(window.location); //Works
document.write(window.location.replace()); //What?

Posted: Tue Apr 12, 2005 5:15 pm
by Burrito
actually you can just use location = for the page that you're on.

moreover, you can use location to direct other pages (child, parent, other frames) to different sites/pages.

ex:

Code: Select all

<script>
function openIt(){
winz = window.open('sample.php','blah','width=400,height=400');
winz.focus();
}
</script>

<a href=&quote;javascript:openIt()&quote;>open</a><br>
<a href=&quote;javascript:winz.location = 'http://www.yahoo.com'&quote;>go</a>
the same thing could be done from the "child" page using opener.location etc.

power stuff is JS :P