PHP AJAX: retrieve data from mysql and show them in form

Rocx

I am trying to create a autofill form. I have a drop down menu and some form fields like text boxes. When user click on drop down menu options Ajax call initiate and it will check data in Mysql database. if record found then it will display data in relevant text box.

My code is working fine but i am not able to return data from mysql database.

<script type="text/javascript">
$(function() {
    $("#autofill").change(function() {
        var data1= $('option:selected', this).text();
                        $.ajax({
                    type: "GET",
                    url:"autofill.php",
                    cache: false,
                    dataType: 'json',
                    data: 'action1=' + data1,
                    beforeSend: function() { 
                        $("#validation-errors").hide().empty(); 
                    },
                    success: function(data) {
                        if(data.success == false)
                            {
                                var arr = data.errors;
                                $.each(arr, function(index, value)
                                {
                                    if (value.length != 0)
                                    {
                                        $("#validation-errors").append('<div class="alert alert-danger"><strong>'+ value +'</strong><div>');
                                    }
                                });
                                $("#validation-errors").show(); 
                                 $('html, body').animate({scrollTop: $("#validation-errors").offset().top}, 2000);
                                btn.button('reset');                            
                            } else {
                                data = JSON.parse( data );
                               $('#title').val('data.title');
                                $('#total').val('data.total');
                                 $('html, body').animate({scrollTop: $("#features-left-image").offset().top}, 2000);
                            }
                    },
                    error: function(xhr, textStatus, thrownError) {
                        alert('Something went to wrong.Please Try again later...');
                        btn.button('reset');
                        alert(thrownError);
                    }
                });             
                return false;
            });

    });
</script>

autofill.php

<?php 
require_once('include/db.php');
if(isset($_GET['action1'])){
$search = strip_tags(trim($_GET['action1'])); 
$search = strtolower($search);
$search="%".$search."%";

$result = $mysqli->prepare("SELECT * FROM posting WHERE title LIKE ?");
$result ->bind_param("s", $search);
$result->execute();
$result->store_result(); 
//$result->bind_result($title);
//$data[]=$result;

echo json_encode($data);
}

?>

As per my understanding problem is around.

//$result->bind_result($title);
//$data[]=$result;

echo json_encode($data);

Please advise me to fix this issue.

Thanks

Vishnu Nair

There are many issues in the logic,

$result = $mysqli->prepare("SELECT * FROM posting WHERE title LIKE ?");
$result ->bind_param("s", $search);
$result->execute();
$result->store_result(); 
//$result->bind_result($title);
//$data[]=$result;

echo json_encode($data);

You will get multiple columns from your SELECT statement and you're binding just one column in your bind_result($title) that will throw a warning, and also you're not fetching your data which will actually extract the data from result set and bind it to your variable.

Second, if you want to populate the field you would expect one result set right?(judging from your javascript) your query with LIKE clause might probably return more than one result, you will have to modify your query so that you can get a unique result.

Now lastly, what you're trying to achieve in your PHP code can more easily done with PDO rather than mysqli, one of the reasons being, you can easily fetch the result set with fetch_all function and json_encode it directly, mysqli has fetch_all too but I believe that is only compatible with the latest versions.

If you want to keep using MySQLI the solution would be this,

$result->bind_result($title, $total, ...); //bind all your columns here
$data = [];

while($stmt->fetch()){
$data['title'] = $title;
.
.
.
//fetch all the columns you need likewise.
}

echo json_encode($data);

I'd still suggest that you should migrate to PDO, which will help you to do all this stuff with much more ease, read more about PDO here: http://php.net/manual/en/class.pdostatement.php

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Drop down form dissapearing when using AJAX to get PHP file to retrieve data from MySQL db

From Dev

Grab complex data from mysql and show them using php

From Dev

Trouble Using jquery Ajax and a php script to retrieve data from mysql

From Dev

retrieve multiple data from various queries with ajax php mysql

From Dev

How to retrieve echoed data from php when posting form data via jquery ajax?

From Dev

Can't retrieve AJAX data from PHP

From Dev

How to retrieve data from MySQL PHP

From Dev

Retrieve data dynamically from Mysql into php page

From Dev

How to retrieve large data from mysql to php?

From Dev

Sending radio button value using AJAX to get PHP file to retrieve data from MySQL

From Dev

PHP - Login Form to Show Data From Database

From Dev

PHP - Login Form to Show Data From Database

From Dev

Using input from form as a variable to retrieve data from a MySQL database

From Dev

Retrieve data from mysql and display in the form of ascii text table in browser

From Dev

How to retrieve data from multiple tables using a PHP form?

From Dev

How to retrieve data from multiple tables using a PHP form?

From Dev

retrieve data using Ajax and mysql

From Dev

retrieve data using Ajax and mysql

From Dev

PHP: Show data from mysql database in table

From Dev

retrieve data from database and show them in bar chart using MPAndroidChart in android

From Dev

mysql, php retrieve data from multiples table mysql

From Dev

PHP MYSQL data count retrieve from mysql gone wrong

From Dev

Retrieve data from SQL table using PHP or AJAX?

From Dev

Can't retrieve data with AJAX from Bootstrap tabs generated with PHP

From Dev

Retrieve data from php

From Dev

Retrieve data from php

From Dev

php for adding data to mySQL from POST form

From Dev

Read data from mysql php jquery ajax

From Dev

Retrieve duplicates and show them

Related Related

  1. 1

    Drop down form dissapearing when using AJAX to get PHP file to retrieve data from MySQL db

  2. 2

    Grab complex data from mysql and show them using php

  3. 3

    Trouble Using jquery Ajax and a php script to retrieve data from mysql

  4. 4

    retrieve multiple data from various queries with ajax php mysql

  5. 5

    How to retrieve echoed data from php when posting form data via jquery ajax?

  6. 6

    Can't retrieve AJAX data from PHP

  7. 7

    How to retrieve data from MySQL PHP

  8. 8

    Retrieve data dynamically from Mysql into php page

  9. 9

    How to retrieve large data from mysql to php?

  10. 10

    Sending radio button value using AJAX to get PHP file to retrieve data from MySQL

  11. 11

    PHP - Login Form to Show Data From Database

  12. 12

    PHP - Login Form to Show Data From Database

  13. 13

    Using input from form as a variable to retrieve data from a MySQL database

  14. 14

    Retrieve data from mysql and display in the form of ascii text table in browser

  15. 15

    How to retrieve data from multiple tables using a PHP form?

  16. 16

    How to retrieve data from multiple tables using a PHP form?

  17. 17

    retrieve data using Ajax and mysql

  18. 18

    retrieve data using Ajax and mysql

  19. 19

    PHP: Show data from mysql database in table

  20. 20

    retrieve data from database and show them in bar chart using MPAndroidChart in android

  21. 21

    mysql, php retrieve data from multiples table mysql

  22. 22

    PHP MYSQL data count retrieve from mysql gone wrong

  23. 23

    Retrieve data from SQL table using PHP or AJAX?

  24. 24

    Can't retrieve data with AJAX from Bootstrap tabs generated with PHP

  25. 25

    Retrieve data from php

  26. 26

    Retrieve data from php

  27. 27

    php for adding data to mySQL from POST form

  28. 28

    Read data from mysql php jquery ajax

  29. 29

    Retrieve duplicates and show them

HotTag

Archive