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.

echo the return value from a javascript call

Discussion in 'PHP' started by lost, Dec 2, 2005.

  1. #1
    in my php code, i have a call to a javascript method:

    
          echo '<script>getPartNoValues();</script>';       
    
    PHP:
    the javascript method getPartNoValues() just returns the value of a member function:
    
      function getPartNoValues()
      {
          return partnoValues;
      }
    
    Code (markup):
    what i need to do is get the value of partnoValues and use it in my php, how would i do that??
     
    lost, Dec 2, 2005 IP
  2. mika

    mika Active Member

    Messages:
    136
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #2
    This is not possible since PHP code is executed on the server side and before the JavaScript code, which runs on the client side.
     
    mika, Dec 2, 2005 IP
  3. lost

    lost Peon

    Messages:
    144
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    ok, i still can't seem to get my head around that
    could you help me then
    how would i go about retrieving a value that was created on the javascript side and use it in the php side??

    is that possible?
     
    lost, Dec 2, 2005 IP
  4. lost

    lost Peon

    Messages:
    144
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    or is there any way that i could place a sql statement in my javascript code???

    in my javascript code, i have a variable that i need to load into the database??
     
    lost, Dec 2, 2005 IP
  5. kc3

    kc3 Peon

    Messages:
    857
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hm... I know that you could use a variable declared in the url of a php file. For instance making a javascript open a php file with say something like ?variable=variable1&2nd_variable=variable2 attached to it's URL.

    You could make the javascript code forward to a php page with that URL than have the php code store the variable and forward back or whatever else you want.

    This would allow you to do anything with variable you want in php including putting it into a mysql database. Reading a variable you could make a php script to read it than send it back to the javascript.

    Example-
    1. myjava.js fills in the variables $first_name with Kenneth and $last_name with Cluck
    2. myjava.js takes the browser to process.php?first_name=$first_name&last_name=$last_name
    This is what the url looks like
    3. process.php processes the file and stores it in a mysql database and forwards to an HTML page with this code
     
    kc3, Dec 2, 2005 IP
  6. philej

    philej Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I would be able to give a more relevent answer if you explain what you are trying to accomplish with this script.

    Are you trying to generate a page based on variables in the client side javascript? If this is the case you can pass variables via the query string ( script.php?variable1=X&variable2=Y ).

    something like this might work for your application:
    
    <script>
    location.href="script.php?getPartNoValues="+getPartNoValues(); 
    </script>
    
    Code (markup):
    -phil
     
    philej, Dec 2, 2005 IP
  7. lost

    lost Peon

    Messages:
    144
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Ok on the JavaScript side i have a variable that is called 'partNoValues' that stored the values of partNo. for that variable i have a get/set function for convenience, a function to set the variable and a function to get the variable value...see below:

    
      function setPartNoValues(pnoValues)
      {
         partnoValues = pnoValues;
      }
    
      function getPartNoValues()
      {
          return partnoValues;
      } 
    
    Code (markup):
    i have a button called save on my form.
    now on the php side, i have a catch for when the save button is pressed

    
      if(isset($_POST['savebutton']))
    
    PHP:
    in this loop i need to take the values from the 'partNoValues' variable and store them into mysql database.

    how can i access the javascript variable in php is the question???
     
    lost, Dec 5, 2005 IP
  8. philej

    philej Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Before the save button is pressed you could store your pnoValues in a hidden form variable and then they would be available in the $_POST array. If pnoValues is an array, you could do something like:
    
    
      function setPartNoValues(pnoValues)
      {
         partnoValues = pnoValues;
         //convert partNoValues to a string and save in a hidden form variable. 
         document.form.hiddenPartNoValues.value = partnoValues.join(","); 
      }
    
    
    Code (markup):
    and then on the php side:

    
    if(isset($_POST['savebutton'])) 
    {
       //$part_numbers will be an array of part numbers
       $part_numbers = explode(",",$_POST['hiddenPartNoValues']); 
    }
    
    Code (markup):
    If you want to pass anything more complex than a list of ID numbers I would look into SOAP. This would allow you to pass complex data types easily.
     
    philej, Dec 5, 2005 IP
  9. DangerMouse

    DangerMouse Peon

    Messages:
    275
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Not sure if this'll help...

    If you just want to get the data out of the page - not return/print any values - you can call your php script inside an image inside some javascript like this:

    
    document.write('<img border="0" alt="" src="script.php?'+jsvars+'">');
    
    Code (markup):
    just put a redirect to a nice image on your server when the PHP script's done its thing ;)

    script.php
    
    $x = partNoValues
    $blah...
    $blah...
    $URLredirect = "/some/nice/image.gif"
    header ("Location: $URLredirect");
    
    Code (markup):
     
    DangerMouse, Dec 5, 2005 IP