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 921533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:53:13+00:00 2026-05-15T18:53:13+00:00

I’m creating my own recipe box using php/mysql and one part I’m stuck on

  • 0

I’m creating my own recipe box using php/mysql and one part I’m stuck on is actually creating the recipes, specifically selecting the ingredients.

What I wanted to do instead is have a auto-complete search box where I can type out names of the ingredients, have the results drop down right below, and click the ones I’m looking for. After clicking the ingredient, it’ll be listed below the search box with an input to put quantity and an “x” to delete if needed. This of course would grow depending on how many ingredients the recipe requires. At the end, I would just take the data and do an insert into my database.

I’ve seen a lot of AJAX tutorials on getting the auto-complete search box functionality, but nothing tying it to the value selection. The best example of what I’m going for can be found at http://supercook.com. They have it so you can search for recipes.

Any suggestions or online resources?

Thanks!

  • 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-15T18:53:14+00:00Added an answer on May 15, 2026 at 6:53 pm

    you asked a great question. Below I’ve written a short example to get you started. Just save it as ingredients.php and it should work. Of course, you’ll need to add your database connection and query to give it real data. I’ve used the jQuery library because it makes the Javascript part a lot easier.

    <?php
    
    // connect to database here
    
    if (isset($_POST['q'])) {
        if (trim($_POST['q']) === '') die('[]');
        // $q = mysql_real_escape_string($_POST['q']);
        // run a query like: "SELECT id, name FROM ingredients WHERE name LIKE '{$q}%'"
        // and put the result in the $result array.
        // This is sample data:
        $result = array(
            array('id' => 71, 'name' => 'apple'),
            array('id' => 3, 'name' => 'anchovies'),
            array('id' => 230, 'name' => 'artichoke'),
            );
    
        if (function_exists('json_encode')) die(json_encode($result));
        else {
            $escaped = array();
            foreach ($result as $r) $escaped[] = str_replace(array('\\', '"'), array('\\\\', '\"'), $r);
            die('["'.join('","', $escaped).'"]');
        }
    }
    
    $data = array();
    if (isset($_POST['ingredients'])) {
        foreach ($_POST['ingredients'] as $i => $ingredient) {
            $data[] = array(
                'ingredient' => $ingredient,
                'quantity' => $_POST['quantities'][$i],
                );
        }
        // save data to the database here (or inside the foreach loop above)
    }
    
    ?>
    <html><head></head><body>
    <style>
        #wrap { position: relative }
        #pop {
            position: absolute;
            border: 1px solid black;
            background-color: white;
            display: none;
        }
    </style>
    
    <?php if (count($data)): ?>
    <h3>Saved:</h3>
    <pre><?php print_r($data) ?></pre>
    <?php endif; ?>
    
    <div id="wrap">
        <input id="adder" />
        <div id="pop"></div>
    </div>
    <form method="post">
        Ingredients:<br />
        <div id="recipe"></div>
        <input type="submit" value="Save" />
    </form>
    
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script>
        var last_query = '';
        jQuery(function() {
            jQuery('#adder').val('').keyup(function() {
                var query = jQuery(this).val();
                if (query != last_query) jQuery.post('ingredients.php', {q: query}, function(data) {
                    var p = jQuery('#pop').html('').show();
                    for (var i=0; i<data.length; i++) {
                        p.append(jQuery('<p>'+data[i].name+'</p>').bind('click', { ingredient: data[i] }, function(e) {
                            add_ingredient(e.data.ingredient);
                            jQuery('#pop').hide();
                            jQuery('#adder').val('');
                        }));
                    }
                }, 'json');
                else jQuery('#pop').show();
                last_query = query;
            });
        });
        function add_ingredient(data) {
            console.log(data);
            var ingredient = jQuery('<div> <input name="quantities[]" size="2" /> '+data.name
                + '<input type="hidden" name="ingredients[]" value="'+data.id+'" /></div>');
            var remover = jQuery('<span>X</span>').click(function() {
                jQuery(this).parent().remove();
            });
            jQuery('#recipe').append(ingredient.prepend(remover));
        }
    </script>
    </body></html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,
Specifically, suppose I start with the string string =hello \'i am \' me And

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.