Retrieve PHP Variable with jQuery

Discussion in 'PHP' started by philpbvc3232, Sep 2, 2010.

  1. #1
    Hi,
    I'm somewhat new to jQuery/JavaScript, so any help would be greatly appreciated.
    I have a few textboxes that need to be populated when a user clicks a hot spot on an image map. Each hot spot has an alt tag representing the ID number of the object I need to retrieve from a database.
    To retrieve the info from the DB, I have a small PHP script that is called with jQuery's .post() function, something to the effect of:
    JavaScript Syntax (Toggle Plain Text)

    1.
    $.post($('#myForm').attr('action'),formInput, function(data){ ... });

    $.post($('#myForm').attr('action'),formInput, function(data){ ... });
    My question is how do I use jQuery to get and manipulate the data that the PHP function retrieves from the DB?

    Thanks in advance for any assistance.
     
    philpbvc3232, Sep 2, 2010 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    supposed that you are using get to call server side

    $.get('yourpage.php', function(){});

    on yourpage.php
    print out all the values you needed BUT in json_encode form

    - put all values into an array,
    $arr = array('value1'=>'somevalue', etc...);
    echo json_encode($arr);

    on the function $.get('yourpage.php', function(){});
    just use eval to decode the json encoded array

    $.get('yourpage.php', function(data){
    eval(data);
    });

    it will now set a private array variable IN js form

    alert(value1);
     
    bartolay13, Sep 2, 2010 IP
  3. dakshhmehta

    dakshhmehta Active Member

    Messages:
    220
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    85
    #3
    U can use AjaxRequest.js which are availlable on Internet. Just search on Google and you will find it.
    Its easy to create dynamic application with that Jquery Class. All the best..:)
     
    dakshhmehta, Sep 2, 2010 IP
  4. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #4
    So, so far you are able to retrieve the data through a click and now you want that information to write into a box ?

    $box_id_name = the textarea box where you want to write
    $the_results = the data from the click

    It should write into the boxes, untested though.

    
    <div onclick="javascript:document.getElementById('$box_id_name').value='$the_results'">
    Code (markup):
     
    MyVodaFone, Sep 3, 2010 IP