User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 391,922 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,704 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 461 | Replies: 11
Reply
Join Date: Feb 2007
Posts: 55
Reputation: adaykin is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
adaykin adaykin is offline Offline
Junior Poster in Training

Echoing PHP variables in JavaScript

  #1  
Jul 16th, 2008
Hello, I want to be able to echo a redirect url, based on a few variables. The content type of this php script is set as Header("content-type: application/x-javascript"); here is my code:

$row = mysql_fetch_row($result);	
echo "window.location = \"http://myurl.com/test/".$row[0]."/index.php?name=".$row[0]."\";";

Right now the url just outputs the url as text, but it doesn't go there. I am calling this script from a form, it is called when the user posts the submit button.
Last edited by adaykin : Jul 16th, 2008 at 4:50 pm.
My Website <-- check out my site!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2007
Location: Arkansas
Posts: 347
Reputation: buddylee17 will become famous soon enough buddylee17 will become famous soon enough 
Rep Power: 2
Solved Threads: 68
buddylee17's Avatar
buddylee17 buddylee17 is offline Offline
Posting Whiz

Re: Echoing PHP variables in JavaScript

  #2  
Jul 16th, 2008
Is there any reason that you are redirecting with JavaScript instead of php's header function?
Lost time is never found again.
- Benjamin Franklin
Reply With Quote  
Join Date: Feb 2007
Posts: 55
Reputation: adaykin is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
adaykin adaykin is offline Offline
Junior Poster in Training

Re: Echoing PHP variables in JavaScript

  #3  
Jul 16th, 2008
Yeah I have to first get the url from database, which is based on the current page.
My Website <-- check out my site!
Reply With Quote  
Join Date: Nov 2007
Location: Arkansas
Posts: 347
Reputation: buddylee17 will become famous soon enough buddylee17 will become famous soon enough 
Rep Power: 2
Solved Threads: 68
buddylee17's Avatar
buddylee17 buddylee17 is offline Offline
Posting Whiz

Re: Echoing PHP variables in JavaScript

  #4  
Jul 16th, 2008
Try echoing a meta refresh:
  1. $row = mysql_fetch_row($result);
  2. echo'<meta http-equiv="refresh" content="0;url=http://myurl.com/test/'.$row[0].'/index.php?name='.$row[0].'">';
Lost time is never found again.
- Benjamin Franklin
Reply With Quote  
Join Date: Nov 2007
Location: Arkansas
Posts: 347
Reputation: buddylee17 will become famous soon enough buddylee17 will become famous soon enough 
Rep Power: 2
Solved Threads: 68
buddylee17's Avatar
buddylee17 buddylee17 is offline Offline
Posting Whiz

Re: Echoing PHP variables in JavaScript

  #5  
Jul 16th, 2008
OR
  1. $row = mysql_fetch_row($result);
  2. echo'<script type="text/javascript">';
  3. echo "window.location = 'http://myurl.com/test/".$row[0]."/index.php?name=".$row[0]."/';";
  4. echo'</script>';
Lost time is never found again.
- Benjamin Franklin
Reply With Quote  
Join Date: Jun 2008
Location: Delhi
Posts: 119
Reputation: vicky_rawat is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 15
vicky_rawat's Avatar
vicky_rawat vicky_rawat is offline Offline
Junior Poster

Re: Echoing PHP variables in JavaScript

  #6  
Jul 17th, 2008
Hi,
Buddy is right,

You should always remember to
enclose your javascript code inside <script language=javascript></script>
block, in order to let browser know that this is a javascript code ant a sentence.
Vivek Rawat
Keep solving complexities.
Reply With Quote  
Join Date: Feb 2008
Location: Gurgaon India
Posts: 64
Reputation: nikesh.yadav is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 9
nikesh.yadav's Avatar
nikesh.yadav nikesh.yadav is offline Offline
Junior Poster in Training

Re: Echoing PHP variables in JavaScript

  #7  
Jul 17th, 2008
hi,

   1.
      $row = mysql_fetch_row($result);
   2.
      echo'<meta http-equiv="refresh" content="0;url=http://myurl.com/test/'.$row[0].'/index.php?name='.$row[0].'">';

warning : if u use this:
use this in head tag

bye
Help as an alias

I think programming is great................
http://www.nikeshyadav.com
Reply With Quote  
Join Date: Feb 2007
Posts: 55
Reputation: adaykin is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
adaykin adaykin is offline Offline
Junior Poster in Training

Re: Echoing PHP variables in JavaScript

  #8  
34 Days Ago
Originally Posted by vicky_rawat View Post
Hi,
Buddy is right,

You should always remember to
enclose your javascript code inside <script language=javascript></script>
block, in order to let browser know that this is a javascript code ant a sentence.


No that's incorrect. If you look at the original post, my header is:

Header("content-type: application/x-javascript");

I figured out what I was doing wrong though, my database wasn't being altered correctly when I did my update functions I got it working now though, thanks guys!
My Website <-- check out my site!
Reply With Quote  
Join Date: Sep 2005
Posts: 641
Reputation: digital-ether has a spectacular aura about digital-ether has a spectacular aura about 
Rep Power: 5
Solved Threads: 38
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Practically a Master Poster

Re: Echoing PHP variables in JavaScript

  #9  
34 Days Ago
Originally Posted by adaykin View Post
No that's incorrect. If you look at the original post, my header is:

Header("content-type: application/x-javascript");

I figured out what I was doing wrong though, my database wasn't being altered correctly when I did my update functions I got it working now though, thanks guys!


JavaScript isn't executed if you just download the file. That is, if you add the Mime-Type that tells the browser is javascript, and send it the content, it will not execute it.
It is only executed inside HTML, that is, when the browser finds an embedded script in HTML, it will assume it must be executed.

So you need a HTML file, with embedded <script> tag.



You can also just use the PHP header() function as mentioned.
$row = mysql_fetch_row($result);	
header("http://myurl.com/test/".$row[0]."/index.php?name=".$row[0]."/";
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote  
Join Date: Feb 2007
Posts: 55
Reputation: adaykin is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
adaykin adaykin is offline Offline
Junior Poster in Training

Re: Echoing PHP variables in JavaScript

  #10  
33 Days Ago
Javascript will execute the code. Just try putting an alert, where the header type is
Header("content-type: application/x-javascript"); you will see :-)

Like I said before a header WOULD NOT WORK since I have to pull information from a database first. You MUST put a header at the top of the page.
My Website <-- check out my site!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Other Threads in the PHP Forum

All times are GMT -4. The time now is 8:16 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC