1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP Javascript variable/value transfer

Discussion in 'PHP' started by kaeesh, May 23, 2005.

  1. roy.laurie

    roy.laurie Peon

    Messages:
    51
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #21
    Heh, a response to this that probably wasn't widely accepted 5 years ago: Use JSON.
    SEMrush
     
    roy.laurie, Feb 9, 2010 IP
    SEMrush
  2. Devonknows

    Devonknows Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #22
    I know this is an old thread but what i what im trying to do is below, and i was wndering if someone could help me :)

    I have a PHP documents that retrieves xml data at request of $_GET for example:
    /index.php?app=testradio&module=jsphp&get=currentDJ

    now when i access it through the web browser but im trying to build a JS autoupdater wich will pull the data from the php file and use getelementbyID to update the section on the page?

    Im not entirely sure how to do this and i was wondering if someone could help? im not sure how to pull the data into a variable in JS.

    Any Help would be appreciated :)

    Thanks

    Dev
     
    Devonknows, Apr 7, 2010 IP
  3. alacka

    alacka Member

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #23
    Founded a good article on your question "javascript variable to php variable", here is a link to the website:
    http://freephpskr.com/php-and-javascript/20-php-and-javascript/86-passing-javascript-variable-to-php.html
     
    alacka, Oct 30, 2010 IP
  4. ghosty

    ghosty Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #24
    i have this javascript code
    
    	echo '<!DOCTYPE html PUBLIC >
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link title="main" href="css/main.css" rel="stylesheet" type="text/css" />
    <link title="main" href="css/ofert.css" rel="stylesheet" type="text/css" />
    <link title="main" href="css/cos.css" rel="stylesheet" type="text/css" />
    <script language="javascript">
    function add(idp) {
    	alert(idp);
    	';
    $id = $_GET['id'];
    $resulta = mysql_query("SELECT * FROM produse WHERE id =".$id);
    $rowa = mysql_fetch_array($resulta);
    $id1 = $rowa['nrcrt'];
    $idc = $_SESSION['userid'];
    $den = $rowa['den'];
    $pretu = $rowa['pret'];
    $prett = $rowa['pret'];
    mysql_query("INSERT INTO cos (id, idp, denp, cant, pru, prt) VALUES ( '$idc' , '$id' , '$den' , 1 , '$pretu', '$prett');");
    echo '
     }
    </script>
    <title>'.$title.'</title>
    </head>
    <body oncontextmenu="return false" ondragstart="return false" >
    <center>';
    
    Code (markup):
    the code work ok untill insert the php part to it
    what am i doing wrong?
     
    ghosty, Jan 16, 2011 IP
  5. onefalldown

    onefalldown Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #25
    Be careful with security issues when passing a PHP variable to Javascript.
    Check the "Atribute Context" section of this article smashingmagazine.com/2011/01/11/keeping-web-users-safe-by-sanitizing-input-data/
     
    onefalldown, Jan 16, 2011 IP
  6. moshxsoft

    moshxsoft Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #26
    it's work, thank you.
     
    moshxsoft, Feb 17, 2011 IP
  7. christalmighty

    christalmighty Guest

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #27
    So, I didn't read all the way to the end of this thread and this solution might've already come up. If not, here ya go: var myVar='string'; document.write("<a href='blah.php?string="+myVar+"' />click</a>");

    Still a pain in the ass if you want automation, but better than nothing.
     
    christalmighty, Oct 3, 2011 IP
  8. afreaz

    afreaz Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #28
    but tht creates a text field.. wht if i just want to send the variable from javascript to php
     
    afreaz, Jul 13, 2012 IP
  9. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #29
    Afreaz, plain old AJAX does that. But whether it will work depends on what you want to do.

    PHP runs on the server and creates the page. Then it sends the entire page, Javascript included, to the user. If you expect that sending a Javascript variable to PHP will change the existing page before it's sent (yes, some people have asked how to do that, even though it's lifting yourself up by the bootstraps you don't have yet), it won't work.

    You need to send the data to a PHP page that creates some data and sends it back to the Javascript that started the whole thing (the "A" in AJAX stands for asynchronous), so it can write that data to the existing page.

    There are AJAX tutorials all over the web. (If you don't mind the overhead, jQuery makes AJAX almost trivial, but I'd suggest learning how to do it manually first, so that you understand what's happening.)
     
    Rukbat, Jul 13, 2012 IP