-1

I want to get information from a database, and I want to put it in selectbox as option. I tried to do it but I could not not put it what is my mistake?(db can connect I just delete server name ) I am not sure how I can put db rows in selectbox as option. therefore, I think my code has a problem.

p.php

  <?php
   // Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}      

$sql = "SELECT * FROM test" ;           

$result = mysqli_query($conn, $sql) or die("Query: ($sql) [problem]");

$row = mysqli_fetch_assoc($result); 

    if (mysqli_num_rows($result) > 0) {   


    while($row = mysqli_fetch_row($result)) {

       display("<option value=$row[seat_id]>",$row[seatnumber]."\n"); 

  }
    display ("</select>", "\n"); 
} else {
    echo "0 results";
}

mysqli_close($conn);    

 function display($tag , $value) {
    echo $tag . $value ;
}
 ?>  

p.html

<html>
<head>
    <meta charset="utf-8">
    <link href="" rel="stylesheet" type="text/css" />
    </head>
    <script type="text/javascript">
  function transfer(){ 
   var pix = document.getElementById('pix').value; 
      document.abc.test.value =pix; 
    } 
  </script>
       <script                  
    src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js">      

 </script>
 <script>
  function ajaxWay() {
      // syntax: $.post(URL,data,callback);
           $.get("p.php",  function(dataFromtheServer) {
       $("#result").html(dataFromtheServer);
   });             
  }
  </script>
    <body>
    <div id="a" style="text-align:center;">
   <form name="abc"  method="get" action="p.php">
  <select id='pix' onchange='ajaxWay()'>
 <input type="button" value="click" onclick="transfer();">
 <input type="text" name="test" id="test"> 
  </form>
  </div>

 </body>
 </html>
david
  • 45
  • 4
  • Open developer's console and check errors, response from server. – u_mulder Aug 03 '16 at 07:33
  • This post seems confused. Is p.php where the form is being submitted to, or what you want to generate the options with? It doesn't make sense to do both things (submit form and generate option list) with the same "p.php" code. – Hayden Eastwood Aug 03 '16 at 08:25

1 Answers1

0

If your main problem is that you are not able to embed options into your HTML, try something like this:

<html>
<head>
    <meta charset="utf-8">
    <link href="" rel="stylesheet" type="text/css" />
    </head>
    <script type="text/javascript">
  function transfer(){ 
   var pix = document.getElementById('pix').value; 
      document.abc.test.value =pix; 
    } 
  </script>
       <script                  
    src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js">      

 </script>
 <script>
  function ajaxWay() {
      // syntax: $.post(URL,data,callback);
           $.get("prefinal.php",  function(dataFromtheServer) {
       $("#result").html(dataFromtheServer);
   });             
  }
  </script>
    <body>
    <div id="a" style="text-align:center;">
   <form name="abc"  method="get" action="p.php">
        <?php
        // Create connection
        $conn = mysqli_connect($servername, $username, $password, $dbname);
        // Check connection
        if (!$conn) {
            die("Connection failed: " . mysqli_connect_error());
        }      
        $sql = "SELECT * FROM test" ;           
        $result = mysqli_query($conn, $sql) or die("Query: ($sql) [problem]");
        ?>
        <select id='pix' onchange='ajaxWay()'>
            <?php
            $row = mysqli_fetch_assoc($result); 
            if (mysqli_num_rows($result) > 0) 
            {   
                while($row = mysqli_fetch_row($result)) 
                {
                    echo '<option value="' . $row[0] . ">' . $row[0] . '</option>';
                }
            } 
            mysqli_close($conn);    
            ?>
        </select>
        <input type="button" value="click" onclick="transfer();">    
        <input type="text" name="test" id="test"> 
    </form>
  </div>

 </body>
 </html>

Note that in the above I am using your code from p.php to generate the actual content for the select options. The form should NOT submit to p.php, but to some other script that will process the form and perform the required actions. I would help more if I knew more about what you were trying to achieve!

Hayden Eastwood
  • 928
  • 2
  • 10
  • 20