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

    Join Date
    Feb 2018
    Posts
    11
    Rep Power
    0

    Passing Array Values Back to Parent


    Is it possible to pass array values back to the parent window? In this example I would like to have the results in the array generated by the query to be called upon from the parent page. If possible the entire array would be useful also. I understand the results can be displayed on the child page therefore showing up on the parent but I can not call upon the array from the parent window.

    Parent window HTML:
    Code:
    <div id="itemDesc"></div>
    
    <td width="3%">1</td><td><input name="actual[]" id="actual[]" type="text" placeholder="Quantity" /></td>
    	  <td><input class="linkTextBox" type="text" name="itemID[]" onchange="showItemDesc(this.value,1)" onclick="showItemID(this.value)"></td>
        <td><input name="target[]" id="itemDesc" type="text" value="<?php echo $itemDesc[0]; ?>" /></td>
    Parent window javascript:
    Code:
    function showItemDesc(str,lineNo) {
    var xhttp;    
      if (str == "") {
        document.getElementById("itemDesc").innerHTML = "";
        return;
      }
    	document.getElementById("itemDesc").style.display = "block";
      xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
          document.getElementById("itemDesc").innerHTML = this.responseText;
        }
      };
      xhttp.open("GET", "orderItemDesc.php?q="+str+"&lineNo="+lineNo, true);
      xhttp.send();
    }

    Child page: (orderItemDesc.php)
    Code:
    $mysqli = new mysqli(connection details);
    
    $itemID=$_GET['q'];
    
    echo "this is the item id passed. ".$itemID."<br>";  //working as expected
    
    $query = "SELECT * FROM item WHERE item_id = '$itemID'";
    
    $result = mysqli_query($mysqli, $query);
    
    while($row = mysqli_fetch_assoc($result)) {		
    echo "this is in the query loop ".$row['desc1_item']."<br>";
    $itemDesc[] = $row['desc1_item'];    // this is the array i would like to call on in the parent page
    	
    }
    
    //echo $itemDesc[0]; //i realize this will give me the first record generated in the query but it is only visible.
  2. #2
  3. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,270
    Rep Power
    4193
    Return your data as a JSON structure, then parse it in your handler.
    Code:
    $row = mysqli_fetch_assoc($result);  //Don't use a loop for queries that return one result.
    echo json_encode($row);
    Code:
    if (this.readyState == 4 && this.status == 200) {
       var row = JSON.parse(this.responseText);
    }
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2018
    Posts
    11
    Rep Power
    0
    But there will be many results. That is why I would like to build the array from the loop in the file named orderItemDesc.php, $itemDesc[] = $row['desc1_item'], and then call upon the values from the array using the index in the parent page. $itemDesc[0], $itemDesc[1], etc...
  6. #4
  7. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,834
    Rep Power
    9646
    So do that. The point of kicken's reply was not that you have to use $row but that you need json_encode() in PHP and JSON.parse() in Javascript.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2018
    Posts
    11
    Rep Power
    0
    Okay. So I think I understand. I know this is decode but it seems that parse logic has similarities.

    This is an array that will be built in the orderItemDesc.php page:
    Code:
    $json = '{"name":"John", "age":30, "city":"New York"}';
    And then I can call from the array on the parent page as such:
    Code:
    $obj = json_decode($json);
    	
    print $obj->{'name'}; // John
    	
    print $obj->{'age'}; // 30
  10. #6
  11. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,834
    Rep Power
    9646
    "Similarities", sure. But why bother with similarities when you could deal with the actual process you need to use?

    1. Build your array in PHP
    2. Output the json_encode()d version so the AJAX receives it
    3. In your Javascript, use JSON.parse() to decode it to an array
    4. Do whatever you want with the array
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2018
    Posts
    11
    Rep Power
    0
    I think I am making this way harder than it is. I feel like I fully understand step one through three.

    I have used the following in my javascript to test the function jason_encode is working on the child page :
    Code:
    document.getElementById("someID").innerHTML = this.responseText;
    And I see that it is passing the array back to the parent as such:
    Code:
    {"item_id":"0HN0250CZ","desc1_item":"1\/4-20 HX NUT ZC","desc2_item":"7\/16 ACF .212-.226THK","xref_item":"0HN0250CZ","listprice_item":"0.0125","pricematrix_item":"0HN"}
    And I am using JSON.pars as kicken pointed out how to decode the array.

    But I am stuck at step 4. I can not get anything to give me a result outside of javascript. I have seen so many tutorials and general how to's that I am all kinds of mixed up. It seems to me that echo $row["item_id"]; or echo $row[0]; or echo $row->{'item_id'}; would work but it does not.
  14. #8
  15. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,834
    Rep Power
    9646
    What do you mean "outside of Javascript"? What you originally described was a problem of getting data from PHP to Javascript through AJAX. Is that not what you're trying to do?
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2018
    Posts
    11
    Rep Power
    0
    Originally Posted by requinix
    What do you mean "outside of Javascript"? What you originally described was a problem of getting data from PHP to Javascript through AJAX. Is that not what you're trying to do?

    Yes I am looking for the array to be passed back to the parent and then return the values from that array any where on the page I wish.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2018
    Posts
    11
    Rep Power
    0
    In a simplistic way to explain:

    $cars = array("Volvo", "BMW", "Toyota"); // array generated on the child page amusing using the php function json_encode and array passed back to the parent page, in the javascript using JSON.parse

    echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . "."; // now being able to call the values from the array anywhere on the parent page I want to
  20. #11
  21. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,270
    Rep Power
    4193
    After you get your data back from the ajax request and decode it, then you use javascript to update the page in whatever way you need.
    Code:
    var row = JSON.parse(this.responseText);
    document.getElementById('description').textContent = row.desc1_item;
    document.getElementById('price').textContent = row.listprice_item;
    //...
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2018
    Posts
    11
    Rep Power
    0
    Originally Posted by kicken
    After you get your data back from the ajax request and decode it, then you use javascript to update the page in whatever way you need.
    Code:
    var row = JSON.parse(this.responseText);
    document.getElementById('description').textContent = row.desc1_item;
    document.getElementById('price').textContent = row.listprice_item;
    //...
    That is what I was afraid of. There will be many rows with ID's of "description" and or "price" That is why I wanted to use the key in the array. I could associate the first value to $array[0] to the first row and the second array[1] value to the second row etc.

    This is what I am wanting to accomplish but I now do not think I am going down the right path:

    there could be hundreds of rows

    qty item id desc
    row 1) 100 item entered by user desc to auto populate when item id onchange event
    row 2) 50 item entered by user desc to auto populate when item id onchange event
    row 3) 500 tem entered by user desc to auto populate when item id onchange event

IMN logo majestic logo threadwatch logo seochat tools logo