Alexis Lozano
Alexis Lozano

Reputation: 31

Access to a PHP array element from Javascript function

I am trying to set some fields of a form with the values of an array.

I have an array like this:

Array
    {
        'elementID1' => Array
                           {
                               'id1' => 'some value',
                               'id2' => 'some value',
                               'id3' => 'some value',
                               ...
                           }
        'elementID2' => Array
                           {
                               'id1' => 'some value',
                               'id2' => 'some value',
                               'id3' => 'some value',
                               ...
                           }
        ...
    }

The array is filled with the response of a webservice to whom I sent some parameters with a form. Once I filled the array, I draw a table and iterate over the array.

<table width = "100%" border = "1">
    <tr>
        <th>Header 1</th>
        <th>Header 2</th>
        <th>Header 3</th>
        <th>View details</th>
    </tr>
    <?php
        foreach($array as $id => $detail)
        {       
            echo "<tr>";
                echo "<td>";
                    if(array_key_exists('id1', $detail))
                        echo $detail['id1'];
                echo "</td>";
                echo "<td>";
                    if(array_key_exists('id2', $detail))
                        echo $detail['id2'];
                echo "</td>";
                echo "<td>";
                    if(array_key_exists('id3', $detail))
                        echo $detail['id3'];
                echo "</td>";
                echo "<td>";
                    echo "<a href = \"javascript:equisYe('" . $id . "')\">View more</a>";
                echo "</td>";
            echo "</tr>";
        }
    ?>
</table>

Now, here comes the problem, as you can see there is a fourth column called View details, this column is filled with a hyperlink that calls a javascript function with the elementID value of the specific row as parameter.

A little more below I have the script:

<script>
    function equisYe(id)
    {
        var equis = "<?php echo $array['" + id + "']['IncidentID']; ?>";
        alert(equis);
    }
</script>

When I click on the hyperlink doen't happen anything, I think the problem has something to do with the special characters inside the quote punctuations. In my browser, when I inspect one of the hyperlinks, it shows the following message inside de scripts tags:

function equisYe(id)
{
    var equis = "<br />
    <b>Notice</b>:  Undefined index:  + id +  in <b>
";
    alert(equis);
}

That means, for some reason, the strings aren't concatenating, and if I delete a 'p' from php tag in the equis var like this:

var equis = "<?ph echo $array['" + id + "']['IncidentID']; ?>";

Then the alert shows this:

"<?ph echo $array['10']['IncidentID']; ?>"

So, it works when it doesn't have the php tags.

Upvotes: 1

Views: 343

Answers (5)

MattioniX
MattioniX

Reputation: 1

You can use data attribute to pass the $id to js, always using the DOM.

Check this: https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes

Then use a click event in "view details" to get the data from element and trigger the function, this way you will be abble to work with it

Upvotes: 0

James
James

Reputation: 22247

Everyone is correct in that you can't simply call PHP functions from Javascript without performing some kind of HTTP request.

Since you already have the "IncidentID" in php while you're generating the page, you can simply write it to the page.

instead of

echo "<td>";
echo "<a href = \"javascript:equisYe('" . $id . "')\">View more</a>";
echo "</td>";

do

echo "<td>";
echo "<a href = \"javascript:alert('" . $detail['IncidentID'] . "')\">View more</a>";
echo "</td>";

Upvotes: 0

Derek
Derek

Reputation: 112

The other answer is correct, but not explaining it at all (or even politely). The PHP stuff runs before your Javascript stuff does. You'll never get this to work this way, unfortunately. If it was the other way around, it might work, but this isn't going to work the way you have it.

The only way to have something like this working is to have the ID value chosen in advance and have PHP set something in Javascript. Javascript can never set a PHP value simply by virtue of PHP being run prior to the page being served and Javascript being run after the page is served.

Upvotes: 1

Deepak anand
Deepak anand

Reputation: 67

try to use <? ?> instead of <?php ?>

Upvotes: 0

Johan
Johan

Reputation: 118

dude... javascript executes on client(browser) and php on server; you CANNOT do that

Upvotes: 0

Related Questions