PHP array that communicates with Javascript

Asked

Viewed 162 times

0

I have this program that puts the selected in accordance with the value:

<!--inicio if option = selected-->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
    // frase que desejo localizar (pega o value do option )
    var frase      = "minAlarme4_analogico00",
        localizado = null;
    // loop que percorre cada uma das opções
    // e verifica se a frase da opção confere com o
    // valor de fase que está sendo procurado
    // valor de fase que está sendo procurado
    $('#Linha5 option').each(function() {
      // se localizar a frase, define o atributo selected
      if($(this).attr('value') == frase) {
        $(this).prop('selected', true);
      }
    });
});
        </script>

<!--Final if option = selected-->

But to generate these "values" I have a program that does this. Note: Unfortunately I can’t post this source code and I don’t even have access to it.

But I have the following content in the code "values" that I have access to. "value" looks like this value=\"%s\"%s>%s. Example:

printf("<option value=\"%s\"%s>%s</option>\n", $nomeScript, $extra, $descricaoScript);.

How to create a PHP array that communicates with Javascript?

(Previously I posted a question saying I needed otherwise, but as the data comes from the database I think I need the array to be in PHP to pass the values to Javascript).

  • 1

    Can you explain the final goal of this strategy better? What do you want to do with it? Because if you just want to convert a PHP array to use in javascript, just use the function json_encode php native.

  • @Gabrielgartz, hi there. It is the following I had a php page that pulled the various contents of the database and generated several tables, but all tables have the same content what differs 1 from the others is the following "Value" and the first sentence that starts. So I did the following:1° I created a copy script;

  • @Gabrielgartz Continuing:2° I made a script to change the "selecteds" so I got a sentence in each table conforming to the code above. Ate ai td well, but when I showed it by my tutor he said, you can’t pass the "Values" like that. I want you to do the following create an array and make php communicate with javascript to pass the "values" dynamically. Unfortunately I don’t know how to explain it better, because it’s my first contact with programming and I’m having a lot of trouble as you might have noticed..

  • 1

    @lost, Not Duplicate, on the other post I wanted a javascript array that communicates with php, but I thought better and finish. If I want to get the database data in the array, I need to make the php array communicate with javascript.

  • 1

    What I still don’t understand is what the purpose of the PHP array that you will pass to Javascript, type you have already generated the options and already know how to select, what this array that comes from PHP will give information to your javascript? What is her purpose?

  • @Gabrielgartz, Hello So he wants me to take the values values dynamically. I tmb can’t understand why he wants this :(

  • 1

    Taking values dynamically is something very vague, there needs to be a purpose to it, it seems you are listing the options and selecting one and this should imply in the next action of your application which is to download the corresponding table and display on the page. If you are downloading all the tables and just hiding the ones that are not being used, maybe what he wants is for you to download only the one that is being displayed. But this is a guess because the description as a whole is very vague.

  • @Gabrielgartz, so he wants the following that I take the phrases dynamically that will appear in the first row of the table and make an array with them.

  • Jessi your case goes a little further than converting a PHP array to Javascript, because maybe that’s not even necessary at this stage of your application, what he wants is for you to search the data on the server side, then deliver the minimum necessary (value) pro client and when it selects an "option" load the rest of the data.

  • I don’t know what your structure is like in PHP, but it is likely that this data is coming from the database and returning a dictionary, you will have to make a new dictionary from the original containing only the relevant data. As your knowledge is limited I think that until the formal explanation becomes complex, the easiest would be to expose your code on the server side so that we can help you.

  • 1

    It’s Jessi, it’s really hard to understand where you’re going with this. Gabriel has a good chance, but I don’t know if he’s right. Can you explain the broader context of your task? Are you typing something into a field and marking a value in select as it was typed? If it is, strange that, because selects are usually to choose a value from a list, not typing.

Show 7 more comments

1 answer

1

From what I understand, you want to send a PHP array for Javascript to understand.

Netse case you could use the function json_encode() to send a JSON for Javascript.

Example:

echo json_encode(["a" => 10, "b" => 20, "c" => 30])
#vai imprimir {"a": 10, "b": 20, "c": 30}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.