0

i have a JavaScript here embedded in a PHP script "echo". when i use the document.getElementById('list_subjects').selectedIndex in alert box it gives the result but when i use it in a php array variable. it says undefined index. i did (int)$index to convert it a integer but its output only is always zero. whats the problem here? Thank You!

<?php

echo "<script> function subject_name(){" . ($index = "(document.getElementById('list_subjects').selectedIndex)") . ";document.getElementById('subj_name').innerHTML = '" . ($list_options_name[(int)$index]) . "';} </script>";

?>
GautamD31
  • 28,552
  • 10
  • 64
  • 85
Kevin Karl Leaño
  • 1,630
  • 3
  • 15
  • 20

1 Answers1

0
$index = "(document.getElementById('list_subjects').selectedIndex"

The above line will NOT store the selectedIndex in the value of $index, PHP is a server-side scripting language and JS is client side. The above line is invalid.

The values should be accessed/passed using a ajax call.

Tutorial : http://www.w3schools.com/ajax/

Madhavan Malolan
  • 719
  • 6
  • 24