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

    Join Date
    Nov 2000
    Location
    Vancouver, Canada
    Posts
    29
    Rep Power
    0

    JavaScript to PHP (or mySQL)


    I am using a quiz program coded in JavaScript. I include it in one of my PHP pages. It writes the score of the test to a flat file in the cgi-bin.

    Is there a way to take data created by JavaScript and either manipulate it with PHP or dump it directly into a mySQL database?

    Brian
  2. #2
  3. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Location
    Seattle
    Posts
    5
    Rep Power
    0

    Use the redirect thingy


    strAddress= "http://www.yourpage.com/info.phpblah=strInfo&blah2=strInfo2"

    where strInfo/strInfo2 are the variables you need to pass

    locaction.href = strAddreses


    try that, I dunno if It'll work, but it might.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Location
    London, England
    Posts
    251
    Rep Power
    15
    I'm not entirely clear what you're trying to do. However, I would assume your quiz script involves submitting a form. If so, you can use JavaScript to setup whatever variables you need with an onSubmit handler, then use a PHP script to process the result. If necessary, create empty type="hidden" form fields to store your values.

    PHP can only pass variables when requesting a script, so unless you're linking to a script and can pass variables in the URL as Jerome suggested or you're submitting a form, then PHP can't help you. But you're probably doing one of these, so no problem.
    Please don't visit my lame personal website, www.webhamster.co.uk. Half the time it doesn't even work!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Location
    Melbourne, Australia
    Posts
    5
    Rep Power
    0
    Following your suggestion the following should work no problem:

    function deleteProduct(form)
    {
    var ID = form.prodID.value;
    if(confirm("Do you really want to delete this product?"))
    window.location.href("admin_prod.php?delete=1&prodID=ID");
    else
    {
    alert("Thought so 8)" + ID);//testing if ID works
    }
    }

    However, it work OK within the JavaScript function where alert window shows "Thought so 8) 111969" but PHP gets only 'ID'
    (here is an extract from an error message):

    "DELETE FROM Products WHERE ProductID=ID"

    Is there anything else to it?

    Regards

    biss
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2000
    Location
    Vancouver, Canada
    Posts
    29
    Rep Power
    0

    PHP to Java??


    Thank you! Thank you!

    I really appreciate this help. I was able to get the result I needed.

    Now that I can move a variable from Java to PHP is there a way to move a variable from PHP to Java? The data that I need to process would be in the url.

    Brian
  10. #6
  11. yet another member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Posts
    262
    Rep Power
    14
    Hi!

    Is your JS in an external file? If not, you can do the same thing (using url data) the other way around.

    JS reads from the url with the object location.search

    Code:
             var input = unescape(location.search.substring(1))
             if (input) {
               // Divide long string into array of name/value pairs.
               var srchArray = input.split("&")
               var tempArray = new Array();
               for (i = 0; i < srchArray.length; i++) {
                 // Divide each name/value pair temporarily into a two-entry array.
                 tempArray = srchArray[i].split("=");
                 // Use temp array values as index identifier and value.
                 results[tempArray[0]] = tempArray[1];
               }
             }
    I myself took this from 'The Javascript Apostle'. Thanks to them!!

    A different approach for this would be making php set a cookie (void is shooting me for this ) containing the data and have JS read from that cookie then. This would even work without reloading the JS page if it stayed in a diffrent frame in the meantime....
    I'm not recommending the use of cookies, though... people might not allow cookies and your gimmicks will look broken in their browsers.


    Greetings,

    Atrus.
  12. #7
  13. No Profile Picture
    Gödelian monster
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 1999
    Location
    Central Florida, USA
    Posts
    2,307
    Rep Power
    62
    It is exceedingly easy to get a variable from PHP to Javascript:

    <script language="Javascript">
    jsvariable = "<?php echo $phpvariable; ?>";
    </script>


    Remember, though, if the PHP variable is a string with line breaks, you need to escape that string, since a javascript string can only be on one line.

    example:
    <?php

    $phpvariable1 = "Hello, this string has\n two lines";
    $phpvariable2 = "This one
    has
    3 lines";

    $phpvariable3 = "Hello, this string will \\n escape line breaks \\n in PHP so that Javascript can handle it.";
    $phpvariable4 = addcslashes($phpvariable1, "\n\r");

    ?>
    <html>
    <head>
    <script language="Javascript">

    jsvariable1 = "<?php echo $phpvariable1; ?>";
    jsvariable2 = "<?php echo $phpvariable2; ?>";
    jsvariable3 = "<?php echo $phpvariable3; ?>";
    jsvariable4 = "<?php echo $phpvariable4; ?>";

    </script>
    </head>
    </html>

    The above script would work for jsvariable3 and jsvariable4, but not for 1 and 2. The output to the browser would be:

    <html>
    <head>
    <script language="Javascript">

    jsvariable1 = "Hello, this string has
    two lines";
    jsvariable2 = "This one
    has
    3 lines";
    jsvariable3 = "Hello, this string will \n escape line breaks \n in PHP so that Javascript can handle it.";
    jsvariable4 = "Hello, this string has\n two lines";

    </script>
    </head>
    </html>

    If you want a more sophisticated way to send variables back and forth between Javascript and PHP, you could take a look at the WDDX functions, which are supported by PHP, and which provide a way to serialize variables and arrays from one environment to another. Using WDDX, you should be able to transport a whole array from PHP to Javascript and vice-versa.
    The real n-tier system:

    FreeBSD -> PostgreSQL -> [any_language] -> Apache -> Mozilla/XUL

    Amazon wishlist -- rycamor (at) gmail.com

IMN logo majestic logo threadwatch logo seochat tools logo