javascript in php

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!

Moderator: General Moderators

Post Reply
dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

javascript in php

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

?>
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

missing action in the form but that is not necessary, you can also use javascript to set the action location as well.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

Post 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??
Morthy
Forum Newbie
Posts: 4
Joined: Tue Apr 12, 2005 1:49 pm

Post 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>";
dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

Post 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?
Morthy
Forum Newbie
Posts: 4
Joined: Tue Apr 12, 2005 1:49 pm

Post by Morthy »

I don't know javascript that well, but maybe one has support for changing the location of pop up windows.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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
Post Reply