Board index   FAQ   Search  
Register  Login
Board index php forum :: HTML JavaScript

please help me

Javascript coding ..

Moderators: macek, egami, gesf

please help me

Postby vrpvrp » Tue Mar 06, 2012 4:12 am

hi,please help me.i am trying to print an alert box using the following code,but the page is redirecting to myaccount.php before the java script execution.
here is the code:
print "<script type=\"text/javascript\">";
print "alert('request sent')";
print "</script>";

if i do the following,i am able to print the javascipt alert.

print "<script type=\"text/javascript\">";
print "alert('request sent')";
print "</script>";
seems the php gets exectingbefore the javascript code.
i want to print alert box ,then the page should be redirected..please help me!
New php-forum User
New php-forum User
Posts: 10
Joined: Mon Mar 05, 2012 12:54 am

Re: please help me

Postby landi » Thu Mar 22, 2012 12:06 pm

The first 3 print statements write some javascript to your browser. It will be executed when the browser decides, and since it doesn't look like a valid HTML page, it may not be executed at all.

The header is a command to the browser to change pages. The only surprising thing is that usually you can't output anything (via print) before sending a header(), or the header doesn't work. Maybe you have some buffering...

My suggestions: use javascript to alert() and then redirect:
a) a write a proper HTML page (maybe you just didn't show this for clarity)
b) Use the javascript as you have it, without the header() function
c) Add this to the javascript:
Code: Select all
document.location = "myaccount.php"; // javascript redirection

Then the redirect will happen after the alert.

BTW, you know you don't have to use print to output javascript? e.g. these two lines of php produce the same output:
Code: Select all
print <script type="text/javascript\">";

?>   <script type="text/javascript"> <? 

Hope that helps
- A
New php-forum User
New php-forum User
Posts: 245
Joined: Thu Mar 15, 2012 3:59 pm

Return to JavaScript

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.