0

I have a form that i would like to submit details when an option is selected from the database.

The mysql database table :-

USERS contains fields like [email],[age],[name].

I want to be able to populate the other input fields values when one field is selected from the menu.

        <form>
        User
        <select name="user" id="user">
          <option>-- Select User --</option>
          <option value="Mark">Mark</option>
          <option value="Paul">Paul</option>
          <option value="Hannah">Hannah</option>
        </select>

        <p>

          Age 
          <input type="text" name="age" id="age">

        </p>
        <p>
          Email 
          <input type="text" name="email" id="email">
        </p>
        </form>

How do i acheive this using jquery or javascript.

1 Answer 1

2

Please Try this,

on your HTML page:

write this to your html page,

<script>
$(document).ready(function(){
    $('#user').on('change',function(){
        var user = $(this).val();
        $.ajax({
            url : "getUser.php",
            dataType: 'json',
            type: 'POST',
            async : false,
            data : { user : user},
            success : function(data) {
                userData = json.parse(data);
                $('#age').val(userData.age);
                $('#email').val(userData.email);
            }
        }); 
    });
});
</script>

in getUser.php:

getUser.php

<?php

$link = mysqli_connect("localhost", "user", "pass","mydb");

$user = $_REQUEST['user'];    
$sql = mysqli_query($link, "SELECT age,email FROM userstable WHERE name = '".$user."' ");
$row = mysqli_fetch_array($sql);

json_encode($row);die;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.