DagelijksGamer
DagelijksGamer

Reputation: 21

How do I output php code in a js innerhtml?

I have a dropdown field and a text field in my form. Whenever a user presses a button, I want it to show both fields twice. I'm doing the js part with an innerHTML and I want to output the php include to the file with the code for both fields. Now for some reason, I can't output php code with innerHTML.

Can someone explain why and how I can solve it. The file with the code for the fields has PHP and sql in it, so I can't just put the code in "" after the innerHTML.

function addInput()
{
    var boxName='textBox'+countBox; 
    document.getElementById('stops').innerHTML+="<?php include'arrivalField.php;'?>";  
    countBox += 1;
}

When I press the button with the code like this, nothing happens. If I change the innerHTML="" thing to "Hello, world" it does work. It's specifically because it's PHP.

arrivalField.php contains

<div class='dropdown'><label for='pwd'>Vehicle:</label><select name='vehicle' id='vehicle' class='form-control'>
        <?php
                //For each car that user has, add it to the top of the dropdown.
                $fetchCars = 'SELECT idCar, brandName, modelName FROM car WHERE idAcc = \''.$_SESSION[UserID].'\'';
                $cars = $conn->query($fetchCars);

                if(!empty($cars)){
                    $row = mysqli_fetch_array($cars);
                    foreach ($cars as $car) {
                    echo '<option class=\'dropdown-item\' value=\'Car\'.$car[idCar].\'>'.$car[brandName].' '.$car[modelName].'</option>';
                    }
                }
            ?>
            <option class='dropdown-item' value='Bicycle'>Bicycle</option>
            <option class='dropdown-item' value='Long-distance bus'>Long-distance Bus</option>
            <option class='dropdown-item' value='City bus'>City bus</option>
            <option class='dropdown-item' value='Plane'>Plane</option>
            <option class='dropdown-item' value='Train'>Train</option>
            <option class='dropdown-item' value='Foot'>Foot</option>
            <option class='dropdown-item' value='Subway'>Subway</option>
            <option class='dropdown-item' value='Tram'>Tram</option>
        </select>
    </div>
    <br>

Upvotes: 0

Views: 69

Answers (1)

Lyudmila
Lyudmila

Reputation: 71

  • First of all, I guess you not fully understand, that PHP executes strictly before javascript, and in your situation, you try to generate js code from PHP. Therefore it's very important to put this code in the right place. I mean, for example, if you put this code to the .js file it will not be preprocessed with PHP (with default server configuration). It may be essential.
  • Secondly, it's very important to understand, that the output of your PHP code must be specially prepared to will be compatible with js syntax. For example, if your PHP code outputs double quote (") it may be a problem, so you will get js syntax error. You can check this by using the client js debugger.

Upvotes: 1

Related Questions