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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:00:32+00:00 2026-06-17T11:00:32+00:00

Im rather new to javascript, and I’m building a basic website which someone can

  • 0

Im rather new to javascript, and I’m building a basic website which someone can search through a small database of cars, which updates the search results as the user selects search criteria.

I’ve attempted to do this by making a form in my html, with multiple selections (CODE 1), and javascript with event listeners for the selections and ajax to search the database (CODE 2) through a PHP file.

CODE 1 – HTML

<div class="intro">
        <p>Enter your search criteria:</p>

            <div id="formbox">
                <form>
                    <!-- MAKE SELECTION -->
                    <select name="make" id="selectmake">
                    <option value="test">Car Make:</option>
                    <option value="Aston Martin">Aston Martin</option>
                    <option value="Ford">Ford</option>
                    <option value="Lotus">Lotus</option>
                    <option value="Vauxhall">Vauxhall</option>
                    </select>&emsp;

                    <!-- MODEL SELECTION -->
                    <select name="model" id="selectmodel">
                    <option value="">Car Model:</option>
                    <option value="DB9 Coupe">DB9 Coupe</option>
                    <option value="Vanquish">Vanquish</option>
                    <option value="Fiesta Hatchback">Fiesta Hatchback</option>
                    <option value="Focus Hatchback">Focus Hatchback</option>
                    <option value="Mondeo Estate">Mondeo Estate</option>
                    <option value="Elise">Elise</option>
                    <option value="Exige">Exige</option>
                    <option value="Astra Hatchback">Astra Hatchback</option>
                    <option value="Corsa D">Corsa D</option>
                    <option value="Frontera 4x4">Frontera 4x4</option>
                    </select>&emsp;

                    <!-- YEAR SELECTION -->
                    <select name="year" id="selectyear">
                    <option value="">Car Year:</option>
                    <option value="1998">1998</option>
                    <option value="2000">2000</option>
                    <option value="2001">2001</option>
                    <option value="2004">2004</option>
                    <option value="2007">2007</option>
                    <option value="2009">2009</option>
                    <option value="2010">2010</option>
                    </select>&emsp;

                    <!-- CC SELECTION -->
                    <select name="cc" id="selectcc">
                    <option value="">Car CC:</option>
                    <option value="1250">1250</option>
                    <option value="1400">1400</option>
                    <option value="1600">1600</option>
                    <option value="1800">1800</option>
                    <option value="2000">2000</option>
                    <option value="3200">3200</option>
                    <option value="5900">5900</option>
                    <option value="6000">6000</option>
                    </select>&emsp;

                    <!-- COLOUR SELECTION -->
                    <select name="colour" id="selectcolour">
                    <option value="">Car Colour:</option>
                    <option value="Black">Black</option>
                    <option value="Blue">Blue</option>
                    <option value="Red">Red</option>
                    <option value="Silver">Silver</option>
                    </select>&emsp;

                    <!-- RESET SELECTIONS -->
                    <input type="reset" />
                </form>
            </div>
        </div>

        <!-- SEARCH RESULTS -->
        <div id="results">
            Your results will be displayed here
        </div>

CODE 2 – JAVASCRIPT FILE

function initialise() {
//assign selection fields to variables
var i=0;
var make=document.getElementById("selectmake");
make.addEventListener('change', setmake(this.value), false);
var model=document.getElementById("selectmodel");
model.addEventListener('change', setmodel(this.value), false);
var year=document.getElementById("selectyear");
year.addEventListener('change', setyear(this.value), false);
var cc=document.getElementById("selectcc");
cc.addEventListener('change', setcc(this.value), false);
var colour=document.getElementById("selectcolour");
colour.addEventListener('change', setcolour(this.value), false);

//assign results area to a variable
var results=document.getElementById("results");

//setting search criteria to return all results by default
var makesearch="make=make";
var modelsearch="model=model";
var yearsearch="year=year";
var ccsearch="cc=cc";
var coloursearch="colour=colour";
results.innerHTML="initiate works";

//function to set the search to search make if set to an existent value
function setmake (makevar) {
    i=i+1;
    results.innerHTML=i;
    if (makevar.length>0) {
        makesearch="make='"+makevar+"'";
        results.innerHTML="EVENTIFFUNCTIONworks";
        constructsearch();
    }
}

function constructsearch() {
    var searchstring="WHERE "+makesearch+" AND "+modelsearch+" AND "+yearsearch+" AND "+ccsearch+" AND "+coloursearch;
    results.innerHTML="construct works";
    sendsearch(searchstring);
}
}

//function to update search results
function sendsearch(searchstring) {
//update search results with an XMLHttpRequest
if (window.XMLHttpRequest) {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
}
else {
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4 && xmlhttp.status==200) {
    document.getElementById("results").innerHTML="XMLworks";
    //xmlhttp.responseText;
  }
}

xmlhttp.open("GET","results.php?search="+searchstring,true);
xmlhttp.send();
}

window.onload=initialise;

The variable ‘i’ was made and all its use was for testing… I did original have this working to the point that when the page loaded, the results div would display the ‘i’ variable, but it did not change when a new selection was made in the ‘make’ selection box.

For some reason my code will now not work at all, it won’t even display ‘initiate works’

Any help is much appreciated and thanks in advance.

  • 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-06-17T11:00:34+00:00Added an answer on June 17, 2026 at 11:00 am

    addEventListener takes a function as the second parameter as you have it you aren’t passing a function but rather calling a function. You can use an anonymous function expression to wrap the code you have in the call to addEventListener. For each of you calls to addEventListener do the following

    make.addEventListener('change', function(){
        setmake(this.value)
    }, false);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rather long running javascript code which adds a new div element
I am new to JSON and JavaScript objects. Can someone please explain the differences
I'm rather new to working with multiple threads in a database (most of my
I have been developing a new JavaScript application which is rapidly growing in size.
I'm building a new AJAX powered website with distinct sections. Each section requires a
I am rather new to all things JavaScript so please bear with me :)
(very new to JavaScript jQuery) Can an if statement contain a collection of other
I'm rather new to jQuery, and I'm not too sure which direction to take
i am rather new to Javascript and i am having trouble making a function
I'm building a website that prints the content in it's Mysql database to the

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.