0

I'm lost with this. First, I made an array in php (reason? Fetch all values of a column into an array, to use later) and "save" like this:

echo '<input type="hidden" name="hidden[]" id="hidden[]" value="'.$to_update.'">';

Then, I made a select with some values and chars, also constructed with php. Like this:

echo '<option value="'. $id .'">'. $desc .'</option>';

The fetched values are correct, $id $desc and $to_update have the value I require.

Then, in php/html I have this code:

 <select name="articulo" id="articulo" onchange="myFunc(this.value)">
 <input name="updated" id="updated" type="text"  />

So, when calling in js is like this (same html, after body tag):

<script >
function myFunc(val) {
  var id = document.getElementById("updated");
  var valor = document.getElementsByName("hidden");
  id.value = valor[val].value;
}
</script>

So, for example, if in select Is 1, 2 and 3 values; one, two and three options, I want to change the input text called updated to another values, like a, b and c.

Is it a better way to do it, or just to fix some code?

Dr Rob Lang
  • 6,659
  • 5
  • 40
  • 60
Epistomai
  • 455
  • 1
  • 6
  • 16
  • 2
    `id="hidden[]"` is not legal. `id`'s have to be unique. So you should change that and then, yes, use the `name` attribute instead – RiggsFolly May 17 '17 at 15:05
  • so, I use name="hidden[]"? And how I access values from javascript? – Epistomai May 17 '17 at 15:06
  • Its an array, so use it like an array – RiggsFolly May 17 '17 at 15:09
  • Possible duplicate of [How to pass variables and data from PHP to JavaScript?](http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript) – mickmackusa May 18 '17 at 01:27
  • First, I've read that post. Second, I didn't want to use AJAX, or many code. Third, not only worked for me, also I want it that way. – Epistomai May 18 '17 at 16:02

1 Answers1

1

I've solved my issue by luck.

I used a push into an array in the PHP with this:

echo "<script>
         var pausecontent = new Array();
         pausecontent.push('".$to_update."');
      </script>";

and then in JavaScript fetch/retrieve/access the array like this:

var valor = pausecontent[val - 1];
id.value = valor;

Works like a charm, without need of another hidden tag.

Pang
  • 9,564
  • 146
  • 81
  • 122
Epistomai
  • 455
  • 1
  • 6
  • 16