【问题标题】:How to fill three text fields by selecting from first drop down menu using MySql in PHP如何通过在 PHP 中使用 MySql 从第一个下拉菜单中选择来填充三个文本字段
【发布时间】:2015-09-30 11:59:36
【问题描述】:

我在一个网站上工作,我必须在其中输入用户详细信息,包括用户名、地址、电话和邮政编码。

所有细节都存储在MySql中。

因此,在表单中,如果我从下拉菜单中单击用户名,那么其他三个文本字段会自动填充相应的数据。

类似的东西,如果用户名被选择为 id 13,那么地址和其他字段可能有这样的东西,[insert address into field, where name=id13]

类似的东西,我不确定它是如何工作的。

注意:我想在知道第一个选项的情况下自动填充其他三个字段。其他三个选项与第一个选项存储在同一数据库中

到目前为止我的代码

<!-- START Presonal information -->
<fieldset class="col-md-6">
    <legend>Shipper Data :</legend>

    <!-- Name -->
    <div class="form-group">
        <label class="control-label" >SHIPPER<span class="required-field"></span></label>                               
        <select type="text" name="Shippername" id="Shippername" class="form-control">
            <option value="0">Select</option>
            <?php
                $sql=mysql_query("SELECT * FROM customer");
                while($row=mysql_fetch_array($sql)){
                    echo '<option value="'.$row['Shippername'].'">'.$row['Shippername'].'</option>';
                }
            ?>
        </select>      

        </div>
                                <div class="row" >
                                <div class="col-sm-6 form-group">
                                    <label  class="control-label">ADDRESS<span class="required-field"></span></label>
                                    <input type="text"  name="Shipperaddress" id="Shipperaddress"class="form-control"  autocomplete="off" required>
                                </div>

                                <div class="col-sm-3 form-group">
                                    <label  class="control-label">PHONE</label>
                                    <input type="text" class="form-control" name="Shipperphone" id="Shipperphone" autocomplete="off" required>
                                </div>

                                <div class="col-sm-3 form-group">
                                    <label class="control-label">PIN CODE</i></label>
                                    <input type="text" name="Shippercc" id="Shippercc"class="form-control"  maxlength="20" placeholder="" autocomplete="off" required>
                                </div>
                            </div>  

【问题讨论】:

  • 请澄清您的问题,您是要自动填充字段,还是要询问如何将数据插入数据库?
  • 我想在知道第一个选项的情况下自动填充其他三个字段。其他三个选项与第一个选项存储在同一数据库中。
  • 您需要使用 ajax 来实现这一点。
  • 你能指导我吗?
  • 不,有很多关于如何使用 AJAX 的指南,你不是第一个想要这样的东西的人。

标签: php jquery html mysql dropdown


【解决方案1】:

在评论中检查 jfiddle

//JQuery

$('#Shippername').change(function() {
    selectedOption = $('option:selected', this);
    $("#Shipperaddress").val( selectedOption.data('address') );
    $("#Shipperphone").val( selectedOption.data('phone') );
    $("#Shippercc").val( selectedOption.data('zipcode') );
});

//php

<select name="Shippername" id="Shippername">
    <option value="0">Select</option>
    <?php 
        $sql=mysql_query("SELECT * FROM customer");
        while($row=mysql_fetch_array($sql)) {
    ?>
            <option value="<?php echo $row['Shippername'];?>" data-address="<?php echo $row['Shipperaddress'] ?>" data-phone="<?php echo $row['Shipperphone'] ?>" data-zipcode="<?php echo $row['Shippercc'] ?>"><?php echo $row['Shippername'];?></option>
    <?php   
        }
    ?>
</select>
<div class="row" >
    <div class="col-sm-6 form-group">
        <label  class="control-label">ADDRESS<span class="required-field"></span></label>
        <input type="text"  name="Shipperaddress" id="Shipperaddress"class="form-control"  autocomplete="off" required>
    </div>
    <div class="col-sm-3 form-group">
        <label  class="control-label">PHONE</label>
        <input type="text" class="form-control" name="Shipperphone" id="Shipperphone" autocomplete="off" required>
    </div>
    <div class="col-sm-3 form-group">
        <label class="control-label">PIN CODE</i></label>
        <input type="text" name="Shippercc" id="Shippercc"class="form-control"  maxlength="20" placeholder="" autocomplete="off" required>
    </div>
</div>

【讨论】:

    【解决方案2】:

    一般来说,您可能希望采用的实现目标的方法可能类似于以下半伪代码。请注意,这未经测试,因此您可能必须看看会发生什么 - 数据库调用不会按原样工作,因此您需要对其进行调整,但希望它能让您了解如何处理这个问题:

    <?php
        @session_start();
    
        if( $_SERVER['REQUEST_METHOD']=='POST' ){
            /* 
                Process the ajax request here to return data from db
                The returned data is then used by your ajax callback to
                build / populate fields in your form.
    
                And yes, this should use PDO or prepared statements to avoid
                mailicious users trying to hijack your site ( sql injection )
            */
    
            $id=$_POST['id'];
            $sql='select * from `customer` where `id`="'.$id.'"';
            $results=$db->query( $sql );
            $json=json_encode( $results );
    
            /* the json data is used by "cbgetdata(r)" */
            echo $json;
    
    
            exit(); 
        }
    
        /* Include your files and do whatever else needs done */
    
    ?>
    
    <html>
        <head>
            <title>Fred Flintstone didn't know how to write code</title>
            <script>
                function getdata(e){
    
                    var http=new XMLHttpRequest();
                    var oSel=document.getElementById('betty');
                    var id=oSel.options[ oSel.options.selectedIndex ].value;
    
                    var headers={
                        'Accept': "text/html, application/xml, application/json, text/javascript, "+"*"+"/"+"*"+"; charset=utf-8",
                        'Content-type': 'application/x-www-form-urlencoded',
                        'X-Requested-With': 'XMLHttpRequest'
                    };
    
                    http.onreadystatechange=function(){
                        if( http.readyState==4 && http.status==200 ) cbgetdata.call( this, http.response );
                    };
    
                    http.open( 'POST', document.location.href, true );
                    for( header in headers ) http.setRequestHeader( header, headers[ header ] );
                    http.send( 'id='+id );
                }
                function cbgetdata( r ){
                    /* callback function that does the form field completion etc */ 
                    var json=JSON.parse( r );
                    alert( json );
                }
            </script>
        </head>
        <body>
            <form name='bert' id='bert'>
                <select id='betty' onchange='getdata(event)'>
                    <optgroup label='select a user'>
                        <option value=1>Fred
                        <option value=2>Wilma
                        <option value=3>Barney
                    </optgroup>
                </select>
    
                <input type='text' id='tf1' name='tf1' />
                <input type='text' id='tf2' name='tf2' />
            </form>
        </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-23
      • 2021-09-30
      • 2016-04-02
      • 2012-12-06
      相关资源
      最近更新 更多