Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7659019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:16:38+00:00 2026-05-31T13:16:38+00:00

I am trying to create an html form that uses AJAX to load database

  • 0

I am trying to create an html form that uses AJAX to load database entries from a mysql database as the user types, so it suggests them to the user.

I am new to AJAX and I am kind of stuck with something that probably is quite simple for more experienced people.

I have this html text input:

<form>
    <input  type="text" id="f" />
</form>

This jQuery script:

$(function(){
ajaxTest();
})

function ajaxTest(){
$('#f').keyup(function(){
    $.ajax({
        url: 'getit.php',
        dataType: 'json',
        data: {'data' : this.value},
        success: function(response){
            for(var i = 0; i < (response.rows*2); i = i+2){
            alert('The name is ' + response[i] + ' ' + response[i+1])
            }
            }
        })
    })
}

And this php script:

//connect to database
//get variables an sanitize
$parameter = $_GET['data'];
$parameter = mysql_real_escape_string( $parameter );

//prepare and run query
$query = "SELECT email , surname , address FROM users WHERE email LIKE '$parameter%'";
$result = mysql_query($query);

//start preparing response
$seires = mysql_num_rows($result);
$response = array('rows'=>$seires);
while($row = mysql_fetch_array( $result ) ) {
    array_push( &$response , $row['onoma'] , $row['epwnimo'] );
    }
echo json_encode($response);

This gets my job done, but I am wondering:

1) Am I using any bad practices?

2) Is there a smarter way to pass the data from the php script to the jQuery script?

3) In html, what tag could I use to display the suggestions to the user? A p with an onclick function to fill the form perhaps or is there something better?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:16:39+00:00Added an answer on May 31, 2026 at 1:16 pm

    I would be worried about making too many ajax calls. It is likely that someone will be typing fast. So if I type ‘abc’ then you will make 3 ajax calls. Lots of overhead. However, if you to implement a delay before sending like 300 milliseconds then the script will wait for a pause before returning results.

    I find that a short (really unnoticeable) delay is better than a flicker in the suggestions.

    Also, you might consider posting this on codereview (stackoverflow’s companion site). It is better suited for answer questions like this one.

    Here is an untested example of what I am referring to:

    $(function(){
        var timer;
    
        var getSuggestions = function() {
            $.ajax({
                url: 'getit.php',
                dataType: 'json',
                data: {'data' : $('#f').value},
                success: function(response){
                    for(var i = 0; i < (response.rows*2); i = i+2){
                        alert('The name is ' + response[i] + ' ' + response[i+1])
                    }
                }
            })
        });
    
        $('#f').keyup(function() {
            clearTimeout(timer);
            timer = setTimeout(getSuggestions, 300);
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thank you for reading. I'm trying to create a HTML form so that my
I am trying to write a PHP script that will create an HTML form
I'm trying to create a form that will handle video file uploads from the
I am currently trying to create a multi-page form that uses both jQuery and
I am trying to create an upload form that allows the user to select
I'm trying to create text in html, that once clicked, the the value of
I am trying to create some dynamic html out of some data from db.
Here is what I am trying to accomplish. I have a form that uses
I'm trying to create a web form that contains checkboxes, among other input elements,
I am trying to create an app that converts html to a pdf doc

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.