1

I would like to be able to insert html code from javascript, which inserts php code that contains an array. What I do is get in the variable $ teams an array of teams. And then I go through the array with a foreach to set the values ​​in the select options

I have done the following but it does not work.

document.getElementById('selectTeam').innerHTML = '<select class="form-control"><?php $teams = ControllerTeam::ctrTeam(); foreach ($teams as $key => $value) { echo '<option value="'.$value["id"].'">'.$value["name"].'</option>';}?></select>';
  • 1
    what does "does not work" mean? it does not change the content? the content it changes to is not correct? – cornel.raiu 11 hours ago
  • instead of the PHP code, try to hardcode an <option value="123">Aha</option> and see if that works – cornel.raiu 11 hours ago
  • @cornel.raiu The console throws the following error: login.js: 78 Uncaught TypeError: Can not set property 'innerHTML' of null. I have tried with a simpler example and it works. I think the problem is the combination of double and single quotes in the php part inside the innerHTML – Damian Ricobelli 11 hours ago
  • does this work? document.getElementById('selectTeam').innerHTML = '<select class="form-control"><option value="123">Aha</option></select>'; – cornel.raiu 11 hours ago
  • @cornel.raiu Yes. Now the simplest examples work. For now the problem is in the php part inside the inner, I think I'm sure it's because of the double and single quotes – Damian Ricobelli 11 hours ago
2

First problem is that, probably, the content is loaded after the JS so the selector returns null hence the error in the OP code. Cannot set property 'innerHTML' of null

That means, you should only execute the JS code on window.load or any other similar event that ensures the fact that the HTML is loaded before trying to execute.

Now, for the other issue in the comments, if you rewrite the PHP code like this :

<?php
$teams = ControllerTeam::ctrTeam();

$options_html = '';
foreach ($teams as $key => $value) { 
    $options_html .= '<option value="' . $value["id"] . '">' . $value["name"] . '</option>';
}?>

And in the JS - make sure it loads on window.load or similar -

document.getElementById('selectTeam').innerHTML = 
    '<select class="form-control"><?php echo $options_html ?></select>';

This makes the code more readable and helps you debug easier.

0

There is a typo in your code. Please change it to:

document.getElementById('selectTeam').innerHTML = '<select class="form-control"><?php $teams = ControllerTeam::ctrTeam(); foreach ($teams as $key => $value) { echo '<option value="'.$value["id"].'">'.$value["name"].'</option>';}?></select>';
  • The code you have given me also contains a typing error. I have put it in my project and it gives error – Damian Ricobelli 11 hours ago
  • I think the problem is double and single quotes – Damian Ricobelli 11 hours ago
  • 2
    why don't you prepare the options string outside the JS and only add a variable there. The code will be much more readable. – cornel.raiu 11 hours ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.