1

I have created a form in html with dropdown list box with static values inside a php file, now i need to populate those dropdown with data from mysql database. i have written a function in php to retrive data from the mysql table. since i am new to php i don't know how to call a php function from html. is it possible to call a php function like calling a JavaScript function.

Here is my HTMl code.

 <div id="machinelog">
    <form id="intermediate" name="inputMachine" method="post">

    <select id="selectDuration" name="selectDuration"> 
      <option value="1 WEEK" >Last 1 Week</option>
      <option value="2 WEEK" >Last 2 Week </option>
      <option value="3 WEEK" >Last 3 Week</option>
    </select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;


    <select id="selectMachine" name="selectMachine"> 
        <option value="M1" >Machine 1</option>
        <option value="M2" >Machine 2</option>
        <option value="M3" >Machine 3</option>
    </select>  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;


<input id="Button" class="button" type="submit" value="Submit" />
</form> 
</div>

I need to take all machine names form mysql and populate the "selectMachine" drop down list box.

My php function is

  function selectMachine()
  {
    $strQuery = "select id, machine
           from rpt_machine
           order by machine";

    $machineResult = mysql_query($strQuery);

    while($arrayRow = mysql_fetch_assoc($machineResult)) {
      $strA = $arrayRow["id"];
      $strB = $arrayRow["machine"];

  }

2 Answers 2

2

Why you are creating functions? simply do it like this

<?php
$query = "SELECT * FROM ...."; //Write a query
$data = mysqli_query($connect, $query);  //Execute the query
?>
<select>
<?php
while($fetch_options = mysqli_fetch_array($data)) { //Loop all the options retrieved from the query
?>
 //Added Id for Options Element 
<option id ="<?php echo $fetch_options['id']; ?>"  value="<?php echo $fetch_options['name']; ?>"><?php echo $fetch_options['name']; ?></option><!--Echo out options-->

<?php
}
?>
</select>
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Mr.Alien, your code solves my problem of populating the options with dynamic values from MsqDb. I have added Id for Options Element which is UUID of MySql, To Retrieve Values Based on That UUID. How to access that dynamic id in javascript. I can't use getElementById, since i requires the Id to be specified.
0

You could write a function similar to this one:

function display_dropdown($id){
  //We need to get the dropdown option from the database
  $sql = 'SELECT machine_name FROM your_table';
  $result =   mysql_query($sql) or die ('Query in display_dropdown failed:'. mysql_error());
  echo '<select name="'.$id.'" id="'.$id.'">';
  while($row = mysql_fetch_array($result,MYSQLI_ASSOC)){
    echo '<option value="'.$row{machine_name}.'">
            '.$row{machine_name}.'</option>';
  }
  echo '</select>';
  echo '</td>';
}

To learn how to make and call php functions go here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.