douzhi1919 2016-08-07 06:26
浏览 20

如何获得选定的选项值?

I tried to get selected option value in PHP, and I wanted to compare between selected value and database value such as seat id in PHP. However, it causes undefined error. Can you check my code?

<script type="text/javascript">
function transfer(){ 
 var pix = $("#pix option:selected").val(); 
    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() {
        var sel = $("#pix option:selected").val();
   $.get("p.php?pix="+ sel );
     // syntax: $.post(URL,data,callback);
       $.get("p.php",  function(dataFromtheServer) {
       $("#result").html(dataFromtheServer);
     });             
    }
  </script>
   <body>
  <div id="a" style="text-align:center;">
      <?php
  $servername = "";
  $username = "";
  $password = "";
  $dbname = "";

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

  $sql = "SELECT * FROM seat" ;            

  $result = mysqli_query($conn, $sql) or die("Query: ($sql) [problem]");  
  ?>
   <form name="abc"  method="get">
    <select name="select" id="pix">
        <?php

            if (mysqli_num_rows($result) > 0) 
             {   
            while($row = mysqli_fetch_assoc($result)) 
              {                                     
                echo "<option value='".$row[seat_id]. "'>" .$row[seatNumber].  "</option>";
              }
          } 
           mysqli_close($conn);    
        ?>
    </select>
    <input type="button" value="click" onclick="transfer();">    
     <input type="button" value="click" onclick="ajaxWay()">    
    <input type="text" name="test" id="text"> 
    <aside id="result">i will put the empty space from database</aside>
</form>

p.php

<?php
$servername = "***.compute-1.amazonaws.com";
$username = "***";
$password = "***";
$dbname = "***";

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

$sql = "SELECT * FROM seat";
$result = mysqli_query($conn, $sql) or die("Query: ($sql) [problem]");
$fields = mysqli_num_fields($result);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
   // display("<table border=1><th>firstname</th><th>lastname</th><th>items</th>","
");

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

        if($_GET['pix'] == $row['seat_id']){

        if($row['seatStatus'] == 1) 
                {
                    echo "available" ;
                }       
                else
                {

                    echo "unavailable";
                }    
                }
    }

  } else {
       echo "0 results";
  }
  mysqli_close($conn);    

?>
  • 写回答

2条回答 默认 最新

  • doujing1156 2016-08-07 06:29
    关注

    this is a wil guess but it looks like there are 2 GET requests, on with the parameter and the other without - the callback function appears to be working with the call with no parameters so perhaps it should look more like:

    $.get("p.php?pix="+ sel,  function( dataFromtheServer ) {
        $("#result").html( dataFromtheServer );
    });
    

    I don't know if the following will help at all but I can't help feeling that the ajax function looks peculiar.

    <html>
        <head>
            <title></title>
    
            <script type="text/javascript">
                function transfer(){ 
                    var pix = $("#pix option:selected").val(); 
                    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() {
                    var sel = $("#pix option:selected").val();
                    console.info( 'send ajax request: p.php?pix=%s', sel );
    
                    $.get("p.php?pix="+ sel,  function(r) {
                        console.info('response: %s',r );
                        $("#result").html( r );
                    });             
                }
            </script>
        </head>
        <body>
            <div id="a" style="text-align:center;">
            <?php
    
                $servername = "";
                $username = "";
                $password = "";
                $dbname = "";
    
                $conn=new mysqli($servername, $username, $password, $dbname);
                $sql='select * from `seat`;';
                $result = $conn->query( $sql );  
            ?>
            <form name="abc" method="get">
                <select name="select" id="pix">
                    <?php                                   
                        if( $result ){
                            while( $rs=$result->fetch_object() ){
                                echo "<option value='{$rs->seat_id}'>" . $rs->seatNumber;
                            }
                        }
                        $conn->close();    
                    ?>
                </select>
                <input type="button" value="click" onclick="transfer();">    
                <input type="button" value="click" onclick="ajaxWay()">    
                <input type="text" name="test" id="text"> 
                <aside id="result">i will put the empty space from database</aside>
            </form>
        </body>
    </html>
    
    
    /* p.php */
    $servername = "ec2-52-201-212-193.compute-1.amazonaws.com";
    $username = "s2016b_user21";
    $password = "s2016b_user21";
    $dbname = "s2016b_user21";
    
    /* alternative sql */
    $conn = new mysqli( $servername, $username, $password, $dbname );
    $sql='select `seatStatus` from `seat` where `seat_id`=?;';
    $stmt=$conn->prepare( $sql );
    $stmt->bind_param( 'i', $_GET['pix'] );
    
    if( $stmt ){
    
        $stmt->execute();
    
        $stmt->bind_result( $status );
        $stmt->fetch();
    
        echo $status==1 ? 'available' : 'unavailable';
    }
    
    $conn->close(); 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line