Thread: PHP + JS

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    65
    Rep Power
    15
    I am developing a page using PHP, but now i need javascript to do some extras function like onclick. So, i need to know a varaible set used in PHP can pass to JS or not.....
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    24
    Rep Power
    0
    Why not use your PHP to write your javascript, for instance:

    <?

    $someVar = "this";
    $someVar2 = "that";

    $strJS = "<script language="JavaScript">nn";
    $strJS .= "var myArray = new Array();n";
    $strJS .= "myArray[0] = $someVar;n";
    $strJS .= "myArray[1] = $someVar2;n";
    $strJS .= "alert("First val from PHP: " + myArrayp[0]);n";
    $strJS .= "</script>nn";

    ?>

    This is pretty simple, but note i didnt test this code so i probably forgot a or a semi somewhere so cut me some slack on it.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    24
    Rep Power
    0
    P.s.

    I guess it would help to add this line:

    echo($strJS);

    Hope i was at least a little help.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    65
    Rep Power
    15
    Thanks for reply.

    How about i want a value passing from javascript to php...like

    <script=javascript>
    var str ="This is a test";
    </script>

    and i print it using php like,

    echo $str;

    will it done????
  8. #5
  9. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15

    <<
    <script=javascript>
    var str ="This is a test";
    </script>

    and i print it using php like,

    echo $str;
    >>


    if you are writing the <<var str="This is a test">> in side java script then it will consider this as a java script variable.
    so you can't call like $str.

    that means this java script values will be going to the client browser.

    Right now 'str' javascript variable is not carrying any value.This variable will be activated only in client side.

    So you can't get that java script variable value in server side.

    if you want to get the same value in php also,then you will have to assain this value directly to php variable.




    ------------------
    SR -
    shiju.dreamcenter.net

    "The fear of the LORD is the beginning of knowledge..."
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    33
    Rep Power
    15
    All previous advice was correct - you can't combine a server-side language with a client-side one. I have only one thing to advise to you - pass your variable to a href which leads to your php file, in the form (for example) myfile.php3?lang=eng + 'MyVariable'. In an anchor, you have to combine it with <a href="javascript:....."
    This is an example of how I combine Javascript and PHP to make a page modification script in two languages displayed in two frames:

    <script>
    function load(url) {
    top.frames[1].location.href=url + '&lang=ru';
    top.frames[2].location.href=url + '&lang=eng';
    }
    </script>

    <select name="select" ONCHANGE="load(this.options[selectedIndex].value)">
    <option value="text1.php3?cat=aboutme">Welcome page</option>

    This script is in the first frame and controls the two frames below - they display the same page in two languages. You don't have to write a function for your case. I am not very experienced in Javascript, but you may even ask on the javascript forum on devshed how to do this.
    Good luck

Similar Threads

  1. Replies: 2
    Last Post: August 18th, 2004, 10:45 PM
  2. How to see the Memory used by a PHP script?
    By Pardall in forum PHP Development
    Replies: 4
    Last Post: February 13th, 2004, 03:54 PM
  3. PHP with UnixODBC
    By coder4hire in forum PHP Development
    Replies: 2
    Last Post: January 20th, 2004, 01:14 PM
  4. Passing string from PHP to Javascript
    By ka8oad in forum JavaScript Development
    Replies: 4
    Last Post: January 17th, 2004, 03:12 PM
  5. UnixODBC + PHP + Apache + Empress
    By coder4hire in forum Database Management
    Replies: 0
    Last Post: January 15th, 2004, 01:41 PM

IMN logo majestic logo threadwatch logo seochat tools logo