Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to insert PHP values into Javascript?
    text
    copied!<p>Javascript:</p> <pre><code>var counter = 1; var limit = 5; function addInput(divName){ if (counter == limit) { alert("You have reached the limit of adding " + counter + " inputs"); } else { var newdiv = document.createElement('div'); newdiv.innerHTML = " &lt;br&gt;&lt;select name='vehicle[]' id = 'vehicle'&gt;&lt;option value = ''&gt;Vehicle "+ (counter + 1) +"&lt;/option&gt;&lt;option value = '.$brand.' '.$name.'&gt;'.$brand.' '.$name.'&lt;/option&gt;"; document.getElementById(divName).appendChild(newdiv); counter++; } } </code></pre> <p>PHP/HTML:</p> <pre><code> &lt;script type = "text/javascript" src="js/addinput.js"&gt;&lt;/script&gt; &lt;form name="form1" method="POST" action="services.php" onsubmit="return valid()"&gt; &lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;center&gt; &lt;table class="form" border=1&gt; &lt;tr&gt; &lt;td class="head" colspan="2" &gt;Select Vehicle:&lt;/td&gt; &lt;/tr&gt; &lt;tr &gt;&lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2" class="info"&gt; &lt;div id="dynamicInput"&gt; &lt;br&gt;&lt;select name = "vehicle[]" id = "vehicle1"&gt; &lt;option value = ""&gt;Vehicle 1&lt;/option&gt;'; include_once "vehicledbconnect.php"; $queryveh = mysql_query("SELECT * FROM vehicletbl"); while($fetch_2 = mysql_fetch_array($queryveh)) { $brand = $fetch_2['vehbrand']; $name = $fetch_2['vehname']; echo '&lt;option value = "'.$brand.' '.$name.'"&gt;'.$brand.' '.$name.'&lt;/option&gt;'; } echo '&lt;/select&gt;'; echo '&lt;input type="button" value="Add another vehicle" onClick="addInput(\'dynamicInput\');"&gt;&lt;/div&gt;'; </code></pre> <p>Hi. Is it possible to insert PHP values in a javascript? I have a program here that if the customer click the submit button (echo '), a new drop-down form will appear. And I want the drop down form to contain all of the values of the query ($queryveh = mysql_query("SELECT * FROM vehicletbl");). In my default drop-down form, all values of the query are shown. Please help me guys. I am desperate for an answer. Javascript is my weakness. Thanks a lot.</p> <p>edit:</p> <pre><code>newdiv.innerHTML = " &lt;br&gt;&lt;select name='vehicle[]' id = 'vehicle'&gt;&lt;option value = ''&gt;Vehicle "+ (counter + 1) +"&lt;/option&gt;" + "&lt;?php include 'vehicledbconnect.php'; $queryveh = mysql_query('SELECT * FROM vehicletbl'); while($fetch_2 = mysql_fetch_array($queryveh)) { $brand = $fetch_2['vehbrand']; $name = $fetch_2['vehname']; &lt;option value = '.$brand.' '.$name.'&gt;'.$brand.' '.$name.'&lt;/option&gt; }?&gt;"; </code></pre> <p>Can this be the solution? I've tried but it's not working, if this can be the solution, maybe there's only something wrong with my code here.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload