数据库组合框填充输入框,然后在新浏览器窗口中打开URL

时间:2017-08-25 06:42:12

标签: php jquery mysql database combobox

我一直在四处搜寻,我观看了几个视频,但是我无法得到我想要的结果。

  1. 我正在尝试使用php通过数据库填充 webname 组合框。在数据库中,我有3个字段: webid webname weburl
  2. 当我从下拉列表中选择一个项目时,我想要一个输入框来显示相应 webname 的网址。
  3. 按一次按钮会在单独的浏览器窗口中打开显示的网址。
  4. 我必须使用组合框填充正确但我无法在更改下拉列表时显示正确的URL。

    我的Index.php

    <body id="top">
    
    <?php
        $cn=mysql_connect("dbHost","dbUser","dbPW") or die(mysql_error());
        mysql_select_db("dbName",$cn) or die(mysql_error());
        $sql = "SELECT * FROM tblName ";
        $result = mysql_query($sql);
    ?>
    <select id="loads" name="Loads" size=1 style="position:absolute;left:133px;top:10px;width:81px;height:22px;z-index:1;">
    
    <?php
        while($ri = mysql_fetch_array($result))
        {
            echo "<option value=" .$ri['webname'] . ">" . $ri['webname'] . "</option>";
        }
        echo "</select> "  
    ?>
     <input type="text" id="ShipperName" name="ShipperName" value="">
    
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>   
     <script>
    
      $(document).ready(function(){ /* PREPARE THE SCRIPT */
    
        $("#loads").change(function(){ /* TRIGGER THIS WHEN USER HAS SELECTED DATA FROM THE SELECT FIELD */
    
          var WebName = $(this).val(); /* STORE THE SELECTED LOAD NUMBER TO THIS VARIABLE */
    
          $.ajax({ /* START AJAX */
    
            type: "POST", /* METHOD TO USE TO PASS THE DATA */
            url: "action.php", /* THE FILE WHERE WE WILL PASS THE DATA */
            data: {"WebName": WebName}, /* THE DATA WE WILL PASS TO action.php */
            dataType: 'json', /* DATA TYPE THAT WILL BE RETURNED FROM action.php */
            success: function(result){
    
              $("#ShipperName").val(result.shippername);
            /*  $("#ShipperAddr").val(result.shipperaddr); */
            }
    
          }); /* END OF AJAX */   
    
        }); /* END OF CHANGE #loads */
    
      });
    
    </script>
    </body>
    </html>
    

    my action.php

    <?php
    
      if(!empty($_POST["WebName"])){
    
        /* RE-ESTABLISH YOUR CONNECTION */
        $con = new mysqli("dbHost","dbUser","dbPW","dbTable");
    
        /* CHECK CONNECTION */
        if (mysqli_connect_errno()) {
          printf("Connect failed: %s\n", mysqli_connect_error());
          exit();
        }
    
        /* PREPARE YOUR QUERY */
        $stmt = $con->prepare("SELECT weburl, webid FROM tblName WHERE webname = ?");
        $stmt->bind_param("i", $_POST["WebName"]); /* PARAMETIZE THIS VARIABLE TO YOUR QUERY */
        $stmt->execute(); /* EXECUTE QUERY */
        $stmt->bind_result($shippername, $shipperaddress); /* BIND THE RESULTS TO THESE VARIABLES */
        $stmt->fetch(); /* FETCH THE RESULTS */
        $stmt->close(); /* CLOSE THE PREPARED STATEMENT */
    
        /* RETURN THIS DATA TO THE MAIN FILE */
        echo json_encode(array("shippername" => $shippername, "shipperaddr" => $shipperaddress));
    
      } /* END OF IF NOT EMPTY loadnumber */
    
    ?>
    

1 个答案:

答案 0 :(得分:0)

我认为您希望用URL填充文本框,但是您将ShipperName分配给文本框,如下所示:

$("#ShipperName").val(result.shippername);

这应该是:

$("#ShipperName").val(result.weburl);