-1

I cant seem to add a php function / variable into my javascript. I need it to set options into a dynamic select form I have in javascript.

I tried "" inside the javascript which everyone recommended.

<?php
function options(){
include "connection.php";
$sql="SELECT DISTINCT PartID, PartName FROM parts";
$result = mysqli_query($conn,$sql);
if ($result) {
while ($row=mysqli_fetch_assoc($result)){
    echo "<option value='{$row['PartID']}'>{$row['PartName']}</option>";
}}}

?>



<script>
$(function(){
$('p#add_part').click(function(){
counter_part += 1;
$('#container').append(
'<strong>Part No.'+ counter_part + '</strong><br />');
$('#container').append(
            '<select><option>Select a Part</option>((((((HERE))))) 
 </select>');
  });
  });
  </script>

The whole function does not work in relation to the methods I keep trying. When ever I add a '' inside the script to add a variable inside the option, the whole function stops working. However its possible to add a counter variable. Btw I have some other functions above the script etc that was irrelevant.

userpyth
  • 45
  • 2
  • 7

3 Answers3

0

You can try storing it in an HTML element in PHP, then you can read it from JavaScript and then delete it from JavaScript. You can also set it at the bottom of the page and set the text color the same as the background color so you can't see it. I don't know much about PHP, but I do for JavaScript and HTML, so, sorry but I can only add the JavaScript and HTML.

HTML:

<span style="background-color:white; color:white;" id="phpVariable"></span>

JavaScript:

var element = document.getElementById("phpVariable"); // Get the element
var phpString = element.textContent; // Get a string
var phpInteger = parseInt(element.textContent) // Get an integer
var phpFloat = parseFloat(element.textContent) // Get a float
element.parentNode.removeChild(element); // Delete the element

For the PHP, you just have to append the HTML element above to the bottom of the page. If you're background is a different color, then you can just set both the background color and the text color to the background color of the page. Doing this will make the text invisible by just looking at it until the JavaScript picks it up and deletes the element.

VirxEC
  • 998
  • 10
  • 22
0

Just echo your JavaScript function with PHP, and you can easily insert function variables with string concat. In example echo 'alert("'.$var.'"); or something like this!

zygsid
  • 31
  • 5
0

I'm not sure that is what you want, but you can use PHP values of variables in your JS with simple echo to text. I just edited your to work. Declaring options into $options variable and return it in function is giving possibility to write them into JS with echo. You could do it with $options directly, without options() function.

PHP code works only once, then your echo values counted as hardcoded text in html file. Use this only if you need to get value once.

<?php
function options(){
  include "connection.php";
  $sql="SELECT DISTINCT PartID, PartName FROM parts";
  $result = mysqli_query($conn,$sql);
  if ($result) {
    $options = '';
    while ($row=mysqli_fetch_assoc($result)){
      $options .= "<option value='".$row['PartID']."'>".$row['PartName']."</option>";
    }
  }
  return $options; 
}

?>



<script>
$(function(){
  $('p#add_part').click(function(){
    counter_part += 1;
    $('#container').append(
    '<strong>Part No.'+ counter_part + '</strong><br />');
    $('#container').append(
            "<select><option>Select a Part</option><?php echo options() ?></select>");
  });
});
</script>