Skip to content
Advertisement

Populate decoded data into dropdown

I am having 2 files, one to fetch ajax and to decode it. the other (page2.php) is modal popup in which I want to populate the received data from page1.php.

page1.php is below which returns invoice numbers perfectly.

$newArr = array();
$decoded = array();
$decoded = json_decode($result, true);
foreach($decoded ['invoices'] as $result) {
    $newArr = $result['invoiceno'];
    echo $newArr; //JUST FOR DEBUGGING
}

page2.php is a modal where I try to get the received data and populate into a dropdown.

function sort_(){
    global $newArr;
    $output='';
    $output.= '<option value = '.$newArr.'>'.$newArr.'</option>';
    return $output;
}

I know I am not looping the array to sort. I have tried different ways like below which didn’t work.

function sort_(){
    global $newArr;
    global $result;
    global $decoded;

    $decoded= json_decode($result, true);
    $output='';
    $output.= '<option value = "Select INO">Select Select INO</option>';
    foreach($decoded['invoices'] as $result) {
        $output.= '<option value = "'.$newArr.'">"'.$newArr.'"</option>';
    }
    return $output;
}

The error that I am getting in developer console is;

<b>Warning</b>:  Trying to access array offset on value of type null in <b>C:xampphtdocsorderpage2.php</b> on line <b>17</b><br />
<br />
<b>Warning</b>:  foreach() argument must be of type array|object, null given in <b>C:xampphtdocsorderpage2.php</b> on line <b>17</b><br />

where I am stuck?

Advertisement

Answer

it was an easy fix.

page1.php had

global $decoded;
$decoded= array();
$decoded= json_decode($result, true);

page2.php

function sort_(){
        global $decoded;
        global $result;
        $_invoiceReturn='';
        $_invoiceReturn .= '<option value = "Select Invoice">Select Invoice</option>';
        foreach($decoded['invoices'] as $result) {
            $invoice_number = $result['invoice_number'];
            $_invoiceReturn .= '<option value = "'.$invoice_number.'">'.$invoice_number.'</option>';
        }
        return $_invoiceReturn;
    }

I am looping the invoices on page2.php based retrieving page1.php JSON. I decoded the json so it becomes a normal PHP array and looping through.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement