SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [ASK] passing php value to jquery ajax

    Hi,

    I am new to Jquery and want to know why this script is not working.


    getfield.php :
    Code:
    $data['value1'] = "this is value 1";
    $data['value2'] = "this is value 2"; 	
    
    echo "$data[value1] <|> $data[value2]";
    the jquery script:
    Code:
    $.get('getfield.php',
      function(result){	
    	alert("Data Loaded: " + result);
      });
    
      //split the returned value
      var extracted=result.split("<|>"); 					
      $("#val1").text(extracted[0]);
      $("#val2").text(extracted[1]);
    the alert window showing : (as expected)

    Code:
    Data Loaded: this is value 1 <|> this is value 2
    but if I change the getfield.php so value1 and value2 is get from a record of a database :

    Code:
    .. //get record from database, where $rec is an array of a record
    $data['value1'] = $rec[name];
    $data['value2'] = $rec[address];
    
    echo "$data[value1] <|> $data[value2]";
    the alert window showing :

    Code:
    Data Loaded: <|>
    I want to fill the result to :

    Code:
     <div id="val1"></div> 
     <div id="val2"></div>
    I found a solution using getJSON but my php version is 5.1 and json is not installed.

  2. #2
    SitePoint Enthusiast TriLLi's Avatar
    Join Date
    Feb 2010
    Location
    BiH, Bugojno
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    call your file in browser not trough jquery, so u can see if there are any errors.

    No reason for your way to not work.
    http://www.wiseblog.info
    Programmers don't die, they just GO SUB without return.

  3. #3
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I try call the getfield.php through browser and a correct string is displayed, either direct variable value or get value from database.

    Code:
    this is value 1 <|> this is value 2
    
    Jhon Doe <|> United States
    this is confuse me, browser result is correct but callback from $.get returning invalid.

    help please..

  4. #4
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This problem is solved.
    It was my mistake, passing value with $_GET.

    Thanks Trilli for the idea.

  5. #5
    SitePoint Zealot
    Join Date
    Jun 2010
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should be using JSON as interchange format rather than some user-defined string return value that you have to parse using odd constructed separators for data separation.
    There's also low-level implementation in jQuery for remote calls called $.ajax so you should look into that one rather than using $.get or $.post for the task you're set out to do.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •