0

I am not sure whether I am asking a dumb question again or not but because I am really a newbie in javascript and php so do hope that someone can help me out on this.

The problem I am facing now is I now have a PHP array named "$horse_info" which consists of all info of horses. Now I will like to display selected horse info once at a time. I was told to use javascript so that it does not need to query from server each time. But how should I do that?

Here is my code and output:

<td> Horse </td>
    <td colspan=2>
        <select onChange='goto(this.options[this.selectedIndex].value)'>
        <?php foreach ($horse_info as $h_list) { ?>
            <option<?php if($h_list->HID==$horse_id){?> selected ="selected"<?php } ?>><?php echo $h_list->HNAME;?></option>
        <?php } ?>
        </select>
    </td>
</tr>


<tr>
    <td> Date Last Meeting </td>
    <td colspan= 2>  </td> <!-- this is where I will need to show last meeting of each horse once at a time. -->

1 Answer 1

3

Why don't you output the php array as a javascript object using json_encode? It will convert the array to a JS object.

Assuming that you are using inline PHP, you could do something like this in your script tag.

var jsObj = <?php echo json_encode($arrayName);?>;
Sign up to request clarification or add additional context in comments.

11 Comments

Hi, Can you show me an example? I am not too sure what do u mean.. =( Sorry for the inconvenience caused.
I added a snippet to point in the right direction. This would convert your array to a javascript object.
OK, thanks. So it should be sth like: <style> var jsArray = <?php echo jsonencode($horse_info)?> </style> Is it? Btw, since I will have to display it on my table. So can I have it in a function?
no, style is for your css rules. <script type="text/javascript"></script> is for your script.
oh ya, sry, i mean <style>.LOL. So can I put it this way? <script type="text/javascript"> function get_last_meeting() { var jsObj = <?php echo jsonencode($arrayName);?>; } </script> Is it possible?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.