javascript array communicating in php

Asked

Viewed 240 times

2

I need to create a array in send your information to a page .

How can I do that?

Next, I had a pg in that carried a lot of data from the bank. So to improve her performance, I created a script in which copied the contents of the tables. But each table has one option and each option has a selected, so I need each option start as your specific phrase, the same was the whole pg in . For that I made the script below:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
    // frase que desejo localizar
    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>

But (minAlarme4_analogico00") is the name of a sensor and I have like 1500. So I need an array to communicate with the so it takes the names of the sensors.

  • 1

    Do you need it to be something dynamic, like AJAX or do you want to send information from a form using POST or GET and when updating the page update this data? Are you using jQuery or prefer a pure JS response?

  • 1

    I don’t get it, I could explain it better?

  • Do you know code to handle Arrays in Javascript? http://imasters.com.br/artigo/21197/javascript/entendendo-arrays-nojavascript/

  • Hello Gartz, I need something dynamic. D

  • I asked this question the other day, maybe this link will help you : http://answall.com/questions/7667/como-enviar-um-array-via-post-para-um-controller-php

  • 1

    @user5946, you need to use ajax for this. What is the purpose of this communication, explain the problem better and we can give you a way.

Show 1 more comment

1 answer

2

You can use the JSON.stringify(array) in Javascript to encode the array and send it to PHP, where vc uses $array = json_decode($_POST['dados']); to read the variable passed.

If you want to accomplish something dynamically, you should use AJAX, what would you get into something like this:

In the JS

meuArray = ??? ;
var arrayCodificado = JSON.stringify(meuArray);
$.ajax({
    type: "POST",
    url: "script.php",
    data: { dados: arrayCodificado }, 
    cache: false,
    success: function(){
        alert("Feito!");
    }
});

In PHP

$meuArray = json_decode(stripslashes($_POST['dados']));
foreach($meuArray as $d){
    echo $d;
}
  • Deactivate the magic_quotes_gpc in your PHP.

  • Good morning. Krash what exactly would go in this line of code? (myArray = ??? ;)

Browser other questions tagged

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