Results 1 to 8 of 8

Thread: Creating Dynamic PHP Link in.js file

  1. #1
    Join Date
    Nov 2006
    Posts
    41
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation Creating Dynamic PHP Link in.js file

    So here is the issue: I am redesigning a site for an organization.The site is bilingual - English and French. The menu at the top and the left side is an HTML page on it's own. All the pages main body of the site call in the menu part with a line of PHP. Here is an example image:



    Here is the problem: The button in the menu to get to the French part of the site needs to go to the same page in French that the person was reading in English instead of just going to the home page for the French side. I.E - someone was reading www.site.com/en/sexytopic.html and they click on the French button in the menu; it needs to take them to www.site.com/fr/topicsexy.html instead of just www.site.com/fr/home.html. It needs to go to the French version of every page that someone is reading, and vice versa for French to English.


    Someone in another forum gave me the following advice:

    In the beginning of your "sexytopic.html" document (I'm asuming you've set it up to be parsed by php) insert the following code:

    Code:
     <?php 
     $address = __FILE__; 
     ?>
    ...then wherever you want the link to go (even in other included files) add:

    Code:
    <?php 
     echo str_replace('/en/', '/fr/', $address); 
     ?>

    - this replaces all occurrences of "/en/" in "$address" with "/fr/" - to get the opposite effect, swap the first two variables around!
    I would get the following error:

    Access forbidden!
    You don't have permission to access the requested object. It is either read-protected or not readable by the server.

    If you think this is a server error, please contact the webmaster.

    Error 403

    localhost
    02/20/07 10:39:15
    Apache/2.2.3 (Win32) DAV/2 mod_ssl/2.2.3 OpenSSL/0.9.8d mod_autoindex_color PHP/5.2.0
    And the address bar would show:

    http://localhost/en/<?php&#37;20echo%20str_replace('/en/',%20'/fr/',%20$address);%20?>
    my .htaccess has the parsing code in it...

    AddType application/x-httpd-php .php .html .htm
    So I don't know why it's doing this. The menu button is part of a DHTML menu, and the actual place where the link for the button is is in a .js file for the menu.

    Code:
    stm_aix("p0i6","p0i1",[2,"","../graphics/buttons/en/francais1.gif","../graphics/buttons/en/francais2.gif",98,30,0,"<?php echo str_replace('/en/', '/fr/', $address); ?>"]);
    Is this what is causing the problems? doing a plain link on a page gets the address bar to display http://www.wnstudios.ca/home/eday201.../New_Page.html after you click the switchy link on http://www.wnstudios.ca/test/en/New_Page.html.
    Last edited by eday_2010; 02-21-2007 at 07:47 PM.

  2. #2
    Join Date
    Nov 2006
    Posts
    41
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Maybe I should simplify my post:

    I need some PHP code that will change the path from www.stuff.com/en/morestuff to www.stuff.com/fr/morestuff, that will work from within a DHTML Menu (inside a javascript file).

  3. #3
    Join Date
    Oct 2006
    Posts
    110
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I don't think it can be contained in a JScript file. Save it as a .PHP file. Otherwise, that might work.

    By the way, do you have user accounts set up in a database on your server? If you do, then you could conceivably have a Language setting for each user, which they could switch on any page using a select tag and be redirected to the (language) version of that page, and have the Language setting on their account changed, and possibly have the text entries (maybe even with everything on the page being string variables or being in frames/iframes) replaced with different-language pages. But I'm still not sure exactly how the code would go, I'm not too experienced in PHP.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    PHP can output Javascript just as well as HTML.
    Code:
    <?php header('Content-Type: text/javascript'); ?>
    
    // somefile.js.php
    
    var v = "<?php addslashes($some_var); ?>";
    Include it as you would anything else:
    Code:
    <script type="text/javascript" src="somefile.js.php"></script>
    Shotgun Ninja, don't confuse JScript and Javascript. JScript is IE's implementation of ECMAScript; JavaScript is Netscape's (with which most other implementations are compatible). Javascript or JavaScript is often used to mean the combination of both.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Nov 2006
    Posts
    41
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Shotgun, they are not using a database here. They simply want to redesign their site. They aren't very web saavy, so I don't want to stray too far from what they have now. All I want to do is have the menu be a separate html file that is included in all the body pages so that if they need to make a change to a button, they only have one or two file to change, instead of changing it on every page like its set up now.

    Nothing people are suggesting seems to work, though I still very much appreciate the advice . I asked someone I have worked with in the past who knows PHP like it was his hand about it and he's making an example of how to do it. He seems to be able to do anything, so I am sure he can get it going.

    However, I am still open to any ideas people have. Ideas are great

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    http://localhost/en/<?php&#37;20echo%20str_replace('/en/',%20'/fr/',%20$address);%20?>
    This sounds to me as if your server doesn't have PHP set up correctly.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Yup, if a server doesn't have PHP set up (or incorrectly) it will try to parse it as text/html.
    - Mike

  8. #8
    Join Date
    Nov 2006
    Posts
    41
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    PROBLEM SOLVED! Here is how it was done:

    In the menu file this code was put:

    Code:
    <?php 
    	/** Take care of all scripting variables before hand... */
    	// Is there French in the Script Name? If so, replace a copy of it with /en/
    	if ( strstr('/fr/', $_SERVER["SCRIPT_NAME"] ) ) {
    		$swapLanguageURL =  str_replace('/fr/','/en/', $_SERVER["SCRIPT_NAME"]);
    		$currentLang = 'french';		
    	}  else {	// It must be English, so lets swap it with /fr/
    		$swapLanguageURL = str_replace('/en/','/fr/', $_SERVER["SCRIPT_NAME"]);
    		$currentLang = 'english';
    	}
    	
    ?>
    Then in the button this was put:
    Code:
    a href="<?=$swapLanguageURL;?>" ...
    WOOOOOOOOT!!!!
    Last edited by eday_2010; 02-26-2007 at 03:25 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •