0

I had a problem with my auto-fill input form, with PHP MySQL and jQuery of course. So I have the select option form and some 'disabled' textboxes. And I want when I select the one of the option, the textboxes is auto-fill with value from the database

So there is my whole code :

PHP (Database connection) - I put it above my <html> code

<?php
    $host = 'localhost';
    $user = 'root';
    $pass = '';
    $db = 'db_mydatabase';

    mysql_connect($host, $user, $pass);
    mysql_select_db($db);

    $user = '';
    $query = mysql_query("SELECT * FROM tb_payment") or die (mysql_error());
?>

HTML

<select name="student" class="input-data">
    <option value="">-- SELECT STUDENT --</option>
    <!-- Do loop data from database -->
    <?php while ($row = mysql_fetch_object($query)): ?>
        <option value="<?php echo $row->id_user ?>">
            <?php echo $row->name ?>
        </option>
    <?php endwhile; ?>
</select>
<br />
<table border=1>
    <tr>
        <th>ID</th>
        <td><input type="text" class="output-id" disabled /></td>
    </tr>
    <tr>
        <th>Name</th>
        <td><input type="text" class="output-name" disabled /></td>
    </tr>
    <tr>
        <th>Total Payment</th>
        <td><input type="text" class="output-total" disabled /></td>
    </tr>
</table>

jQuery :

$(document).ready(function(){
    $(".input-data").on("change", function(){
        <?php $id_user = "$(this).val()"; ?>

        <?php $data = mysql_query("SELECT * FROM tb_payment WHERE id_user = '$id_user'") or die (mysql_error()); ?>
        <?php while($row = mysql_fetch_object($data)): ?>
            $(".output-id").val(<?php $row->id_user ?>);
            $(".output-name").val(<?php $row->name ?>);
            $(".output-total").val(<?php $row->total ?>);
        <?php endwhile; ?>
    });
});

But when i try to select the option, the values are wouldn't appear. Can someone help me?

6
  • What if you remove this extra < in the 4th line of the jQuery part? Commented Feb 10, 2016 at 2:06
  • Do you have error reporting on? This should be causing issues while (row - mysql_fetch_object($query)):.. Commented Feb 10, 2016 at 2:06
  • Thanks @PhiterFernandes , removed already Commented Feb 10, 2016 at 2:17
  • @chris85 It is no error that reported but when I open my firebug, the java script code its just $(document).ready(function(){ $(".input-data").on("change", function(){}); }); Commented Feb 10, 2016 at 2:26
  • Are you looking at PHP error logs? Commented Feb 10, 2016 at 2:30

2 Answers 2

3

What about creating new page name process.php contain:

require_once "connectDB.php";

header('Content-type: application/json; charset=utf-8');

if(isset($_POST['one'])){
$json = array();
$id =  trim($_POST['one']);
$query = "SELECT id, name, total FROM tb_payment WHERE id_user = ?";
$statement = $databaseConnection->prepare($query);
$statement->bind_param('s', $id);
$statement->execute();
$statement->bind_result($nId, $nName, $nTotal);
while ($statement->fetch()){
   $user=array('id'=>$nId,'name'=>$nName,'total'=>$nTotal);
    array_push($json,$user);
}
echo json_encode($json, true);

 }

And the Jquery:

$(document).ready(function(){
    $(".input-data").on("change", function(){
            var id = $(".input-data").val();
            var data = 'one=' + id;
            $.ajax({
                type: "POST",
                url: "process.php",
                data: data,
                dataType: 'json',
                success: function (data) {
                    if (data) {
                        for (var i = 0; i < data.length; i++) { //for each user in the json response
                            $(".output-id").val(data[i].id);
                $(".output-name").val(data[i].name);
                $(".output-total").val(data[i].total);
                        } // for

                    } // if
                } // success
            }); // ajax
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

You have to print

 $(".output-id").val(<?php print $row->id_user ?>);
 $(".output-name").val(<?php print $row->name ?>);
 $(".output-total").val(<?php print $row->total ?>);

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.