how to embed javascript in php using this code

Discussion in 'JavaScript' started by paul174, Sep 9, 2010.

  1. #1
    hello i am new to JavaScript and i need help with this code of comments system, the idea is to set the page id same as the folder name which is a page id on smarty
    example : my pages looks like http://domain.com/video/1245/...
    i have this javascript which print the page id 1245 like this
    <SCRIPT LANGUAGE="JavaScript">pathis=location.pathname;
    
    new_array=pathis.split("/");
    
    subdirectory=new_array[2]; // 4 is a guess, try others
    
    document.write(subdirectory);</SCRIPT>
    Code (markup):
    and my php looks like this :
    <?php
    $page_id = 1; // for example
    include("comments/comments_show.php");
    include("comments/comments_form.php");
    ?> 
    PHP:
    so here i want instead of page_id=1 to use the script code above to use my folder name 1245 ..

    any help plz ?
     
    paul174, Sep 9, 2010 IP
  2. wab

    wab Member

    Messages:
    57
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #2
    your javascript:
    
       document.location.href = "myphpfile.php?id="+subdirectory;
    
    Code (markup):
    And your php file:
    
    <?php
    $page_id = $_GET["id"]; //Get the id passed in the url here
    include("comments/comments_show.php");
    include("comments/comments_form.php");
    ?>
    
    PHP:
     
    wab, Sep 10, 2010 IP
  3. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #3
    Hi! Try This Method.

    <?php

    $id = '
    <script>
    document.write("007");
    </script>
    ';

    $page_id = $id;

    print $page_id;

    ?>
     
    HungryMinds, Sep 10, 2010 IP
  4. paul174

    paul174 Greenhorn

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #4
    thank you very much ,worked perfectly as i wanted ,here is what i did :
    <SCRIPT LANGUAGE="JavaScript">document.location.href = "index.php?module=video&id="+subdirectory;</SCRIPT>
    <?php
    $page_id = $_GET["id"]; //Get the id passed in the url here
    include("comments/comments_show.php");
    include("comments/comments_form.php");
    ?> 
    PHP:
     
    paul174, Sep 10, 2010 IP