简体   繁体   中英

Form fields updating values from mysql on selecting a value in drop down

I am developing a page in PHP. It has a select drop down(user names) and a form below containing user details. On selecting a particular user from drop down, details of selected user should be populated in form below.
Am beginner to PHP.
How to do this? Any simple solution

You need to put dropdown in a form. On change event of dropdown just post form on same URL with get method. Check the paramenter in url and fetch data and display.

<html>
<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $('#userid').change(function(){
                $('#getUserData').submit();
            });
        });
    </script>
</head>
<body>
    <form name='getUserData' id='getUserData' action='#' method='GET'>
        Select User : <select id='userid' name='userid'>
            <option value='1'>Lokendra</option>
            <option value='2'>Amit</option>
            <option value='3'>Nitin</option>
            <option value='4'>Rishabh</option>
        </select>

    </form>
    <?php
        $userArray=array(
                    1   =>  'Lokendra',
                    2   =>  'Amit',
                    3   =>  'Nitin',
                    4   =>  'Rishabh',
                );
        $postedData=$_REQUEST;
        // Fire your select query here and diplay data 

        if(isset($postedData['userid'])){
            echo "Selected User name =>".$userArray[$postedData['userid']];
        }
    ?>
</body>
</html>

Dont forget to accept this answer if helps :)

If you want to make it an AJAX request.
Follow the steps
Lets assume you make your post requests to user.php

<form id='getDetails' type='post' action='user.php'>
     <select id='users'>
        <option>AMAN</option>
        <option>ABHAY</option>
     </select>
</form>
<script>
$(document).ready(function(){
    $('users').on('change', function(){
      var userVal = $('users option:selected').text();
      $.post('user.php',{user:userVal},function(data){
          console.log(data);
          //Populate the form by using data variable 
          //which contains the data you need
      });
    });
});
</script>

Make sure to include jQuery

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM