Reputation: 21
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
Reputation: 71
Upvotes: 1