To get all Vehicle Details (New, Used, Future) using Jquery , Use the below code.
<!--Form Details to display year/make/model in dropdown -->
<form method="POST" action="one123.php">
<select id="ajYears" name="ajYears">
</select>
<select id="ajMakes" name="makes" disabled="disabled">
<option>Select Make</option>
</select>
<select id="ajModels" name="models" disabled="disabled">
<option>Select Model</option>
</select>
<select id="ajStyles" name="styles" disabled="disabled">
<option>Select Trim</option>
</select>
<input type="submit" value="submit">
</form>
<!--Script to fetch details from API-->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
jQuery.noConflict();
</script>
<!-- js code -->
<script>
<script>
var protoCall = "https://";
var host = "api.edmunds.com";
// Make sure to change the API KEY
var apiKey = "v4hq7w7nac87ehk9exfvasu5";
var fmt = "json";
var standardParams = "&fmt=" + fmt + "&api_key=" + apiKey + "&callback=?";
var $yearSelect = jQuery('#ajYears');
var $makeSelect = jQuery('#ajMakes');
var $modelSelect = jQuery('#ajModels');
var $styleSelect = jQuery('#ajStyles');
// lets bind some events on document.ready.
jQuery(function(){
$yearSelect.on('change', function() { getMakeModelsByYear()});
$makeSelect.on('change', function() { enableModelDropdown() });
$modelSelect.on('change', function() { getStyles(); });
$styleSelect.on('change', function() { jQuery('.veh_submit').removeAttr("disabled"); });
// lets build the year drop down.
ajEdmundsBuildYear();
});
// Method to build the year drop down.
function ajEdmundsBuildYear()
{
var baseYear = 1990,
endYear = new Date().getFullYear();
$yearSelect.empty();
$yearSelect.append('<option value="-1">Select Year</option>');
for(var yyyy=baseYear; yyyy<=endYear; yyyy++)
{
$yearSelect.append('<option value="' + yyyy + '">' + yyyy +'</option>');
}
}
// get the makes and modals for a year in one go, but we don't display modals here...
function getMakeModelsByYear()
{
$makeSelect.empty();
$modelSelect.empty();
$styleSelect.empty();
$makeSelect.append('<option value="-1">Select Make</option>');
$modelSelect.append('<option value="-1">Select Modal</option>');
$styleSelect.append('<option value="-1">Select Style</option>');
$makeSelect.attr('disabled','disabled');
$modelSelect.attr('disabled','disabled');
$styleSelect.attr('disabled','disabled');
var year = $yearSelect.val(),
endPoint = "/api/vehicle/v2/makes",
view = "basic",
options = "year=" + year + "&view=" + view + standardParams,
postURL = protoCall + host + endPoint + "?" + options;
jQuery.getJSON(postURL,
function(data) {
// clear the make drop down... re add the first option... remove dsiabled attr.
$makeSelect.empty();
$makeSelect.append('<option value="-1">Select Make</option>');
$makeSelect.removeAttr('disabled');
// loop the makes... each make contains model... add the make in make drop down.
jQuery.each(data.makes, function(i, val) {
make = data.makes[i];
$makeSelect.append('<option value="' + make.niceName + '">' + make.name + '</option>');
});
});
}
// since we already grabed the models...
// now just matter of showing only the matching models for a make.
function enableModelDropdown()
{
// clear the Style down... re add the first option... add dsiabled attr.
$styleSelect.empty();
$styleSelect.append('<option value="-1">Select Style</option>');
$styleSelect.attr('disabled','disabled');
var make1 = $makeSelect.val();
var year = $yearSelect.val(),
endPoint = "/api/vehicle/v2/makes",
view = "basic",
options = "year=" + year + "&view=" + view + standardParams,
postURL = protoCall + host + endPoint + "?" + options;
jQuery.getJSON(postURL,
function(data) {
//let's empty our modal
$modelSelect.empty();
// loop the makes... each make contains model... add the modal in modal drop down.
//re add the first option
$modelSelect.append('<option value="-1">Select Model</option>');
jQuery.each(data.makes, function(i, val) {
make = data.makes[i];
// loop the makes... each make contains model... add the modal in modal drop down but it depends on make value we select previously
jQuery.each(make.models, function(x, val){
model = make.models[x];
if(make1 == make.niceName){ // here is the logic
$modelSelect.append('<option make="' + make.niceName +'" value="' + model.niceName + '">' + model.name + '</option>');
}
});
});
});
$modelSelect.removeAttr('disabled');
}
// grab the styles... pretty much same as
// getMakeModelsByYear()
function getStyles()
{
var year = $yearSelect.val(),
make = $makeSelect.val(),
model = $modelSelect.val(),
endPoint = "/api/vehicle/v2/" + make + "/" + model + "/" + year + "/styles",
view = "basic",
options = "view=" + view + standardParams,
postURL = protoCall + host + endPoint + "?" + options;
jQuery.getJSON(postURL,
function(data) {
$styleSelect.empty();
$styleSelect.removeAttr('disabled');
$styleSelect.append('<option value="-1">Select Style</option>');
jQuery.each(data.styles, function(i, val) {
style = data.styles[i];
$styleSelect.append("<option value='" + style.id + "'>" + style.name + "</option>");
});
});
}
</script>
==========================================================
Hello Mates here is the second step
<?php
if(isset($_POST['submit'])) {
$to = 'youremial@gmail.com'; // Change this to your email
$subject = ''.$_POST['m_phone'].'';
$subject_sucess = "Edmunds Vehicle API with Jquery - Output";
$ajYears = $_POST['ajYears'];
$ajMakes = $_POST['ajMakes'];
$ajModels = $_POST['ajModels'];
$ajStyles = $_POST['ajStyles'];
$mycar = $_POST['mycar'];
$approx_milage = $_POST['approx_milage'];
$email = $_POST['email'];
$h_phone = $_POST['h_phone'];
$m_phone = $_POST['m_phone'];
$fname = $_POST['fname'];
$l_name = $_POST['l_name'];
$add1 = $_POST['add1'];
$add2 = $_POST['add2'];
$city = $_POST['city'];
$r_state = $_POST['r_state'];
$zip = $_POST['zip'];
$loanamont = $_POST['loanamont'];
$from = ''.$fname.'<'.$email.'>';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Create email headers
$headers .= 'From: '.$fname.' <youremial@yourwebistename.com>'. "\r\n"; // Change this to your website email
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
// To send HTML mail, the Content-type header must be set
$headers_success = 'MIME-Version: 1.0' . "\r\n";
$headers_success .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Create email headers
$headers_success .= 'From: Edmunds Vehicle API <youremial@yourwebistename.com>'. "\r\n"; // Change this to your website email
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
// Compose a simple HTML email message
$message = '<b>Year:</b> '.$ajYears.'<br>
<b>Make:</b> '.$ajMakes.'<br>
<b>Model:</b> '.$ajModels.'<br>
<b>Style:</b> '.$ajStyles.'<br>
<b>My car does not appear:</b> '.$mycar.'<br>
<b>Approx. Mileage:</b> '.$approx_milage.'<br>
<b>Email:</b> '.$email.'<br>
<b>Home Phone:</b> '.$h_phone.'<br>
<b>Mobile Phone:</b> '.$m_phone.'<br>
<b>First Name:</b> '.$fname.'<br>
<b>Last Name:</b> '.$l_name.'<br>
<b>Address Line 1:</b> '.$add1.'<br>
<b>Address Line 2:</b> '.$add2.'<br>
<b>City:</b> '.$city.'<br>
<b>Residence State:</b> '.$r_state.'<br>
<b>Zip Code:</b> '.$zip.'<br>
<b>Loan Amount Requested:</b> '.$loanamont.'';
$message_sucess='Thank you for filling out, We will be happy to assist you.';
// Sending email
if(mail($to, $subject, $message, $headers)){ mail($email, $subject_sucess, $message_sucess, $headers_success); ?>
<script>window.location.assign("http://www.yourwebistename.com");</script>
<?php }
else {echo "mail not sent";}
}
?>
==========================================================
If no email functionality is required you can use this Code.
1. Update your HTML with the below
<!--Form Details to display year/make/model in dropdown -->
<select id="ajYears" name="ajYears">
</select>
<select id="ajMakes" name="makes" disabled="disabled">
<option>Select Make</option>
</select>
<select id="ajModels" name="models" disabled="disabled">
<option>Select Model</option>
</select>
<select id="ajStyles" name="styles" disabled="disabled">
<option>Select Style</option>
</select>
<input type="submit" value="Submit" class="veh_submit">
<!-- output Table -->
<table width="735" border="0" class="output" cellpadding="0" cellspacing="0" style="display:none;">
<tr>
<td colspan="2" style="text-align:center"><strong>Vehicle Information</strong></td>
</tr>
<tr>
<td><strong>Year</strong></td>
<td class="year">Empty</td>
</tr>
<tr>
<td><strong>Make</strong></td>
<td class="make">Empty</td>
</tr>
<tr>
<td><strong>Modal</strong></td>
<td class="modal">Empty</td>
</tr>
<tr>
<td><strong>Style</strong></td>
<td class="style">Empty</td>
</tr>
</table>
</div>
2. Add this script after </body> tab
<script>
// just to display the output
jQuery('.veh_submit').on("click",(function(){
var ajYears = jQuery("#ajYears").val();
var ajMakes = jQuery("#ajMakes").val();
var ajModels = jQuery("#ajModels").val();
var ajStyles = jQuery("#ajStyles option:selected").text();
//Basic Validation
if(ajYears =='' || ajYears==-1)
{
alert("Please Select a Year");
exit;
}
else if(ajMakes =='' || ajMakes==-1)
{
alert("Please Select a Make");
exit;
}
else if(ajModels =='' || ajModels==-1)
{
alert("Please Select a Model");
exit;
}
else if(ajStyles =='' || ajStyles=="Select Style")
{
alert("Please Select a Style");
exit;
}
jQuery('.output').show();
jQuery('.year').html(ajYears);
jQuery('.make').html(ajMakes);
jQuery('.modal').html(ajModels);
jQuery('.style').html(ajStyles);
}));
</script>
Get Hosting and Domains for Best prices at http://webservices.redtreetechnology.com/
Reference url: http://developer.edmunds.com/faq.html
This is fantastic and just what I was looking for, can this be integrated into a form made by Contact Form 7, I found a similar scenario with a plugin called Official CarQuery API WordPress Plugin and found a guide on how to implement that data into contact form 7 http://premium.wpmudev.org/forums/topic/official-carquery-api-wordpress-plugin-embed.
ReplyDeleteDo you think this is a possibility?
Fantastic work so far by the way I hope it's in further development as this would be a very useful tool.
Hi Ben,
DeleteWe are working on it . I'll update you once it is done.
I cant get it to work. I didnt add the jquery since it is already included in wordpress. Adding it in breaks the site.
ReplyDeleteI made my own form via contact form 7.
And I pasted the final code with my own api...
http://abautoglass.net/contact-us/
Thanks!
Never mind, putting it to footer worked... Thanks much mate!
DeleteYour are Welcome Ugur :) have a nice day
DeleteThis comment has been removed by the author.
ReplyDeleteQuick question. When I click on the year, it starts from 1990 and goes down to newer years. Can I reverse it so that it starts from newer years and goes down to older?
ReplyDeleteThanks!
yes, you can just use this code
DeleteUse "prepend" instead of append in "ajEdmundsBuildYear" function
I have a major glitch that I need your help with if you don't mind.
ReplyDeleteAt http://abautoglass.net/contact-us/
If you select year, than Make and Model load at the same time (as you have stated in the code " // get the makes and models for a year in one go...")
On normal internet connection this works fine. But when it comes to slower devices, such as phones on 4G connection this is a problem.
What happens is you select year, than you select make, but when its time to select model, ALL of the models for ALL cars show up.
My question is, can we make the make and model load one after another? Instead of making it load at the same time? I feel like this would solve the issue.
TO exactly understand what I mean. Try my contact form on your PC, than try it on your mobile device. You will see what I mean
Thanks so much!
Hi Ugur,
Deleteyes we can by using a time Daley near change function.
I am experiencing the same issue. Would you be able to elaborate on how to resolve? Should I break apart the getMakeModelsByYear function into 2, or is there a simple delay I can add somewhere?
DeleteI was able to come up solution by isolating Make and Model searches into individual functions.
DeleteThis comment has been removed by the author.
ReplyDeleteI pasted the code into a page. The page appears okay, but the dropdown menus do not work. I did include my api key. Any clues?
ReplyDeleteHi Brad,
ReplyDeleteCan you be more specific which dropdown is not working.
None of the dropdowns work. They are there, but are grayed out.
DeleteCan i see your link
Deletewhat i should put exactly in "one123.php" page ? because i already have changed the API KEY and i have created a page called one123.php but its empty ... and when i select the model and all it directs me to an empty page .. can you please help ?
ReplyDeleteguys .. please tell me how the action page should look like :( !!
ReplyDeletealright .. lets say that the output on the one123 page is json ..
ReplyDeletei get hard time converting it to html that the user/visitor can see !
Instead of PHP, can you make the form in HTML using JQuery Mobile?
ReplyDeleteGreat help
ReplyDeleteI used your snippet and it worked great! The only problem is that I can't get it to work past pulling in the year, make, model, style, and submitting? I got it to redirect the page I needed it to, but I can't get it to output the value on the 'action page'.
ReplyDeleteI am basically having the same issue as Amr Saeed. If anyone know what to do next for this part, that would be great. I used this snippet exactly as is and put the scripts in the footer, in Wordpress.
Hi Mates, I Updated second step in the post.
ReplyDeleteTrying to get the year dropdown box to sort correctly from descending order Whenever I change append to prepend the dropdown opens upward? :?
ReplyDeleteAny suggestions? Thanks
I believe I got it by just changing the for loop around to count down. It's just the prepend comment above was throwing me off a bit. Here's how I got it going: for(var yyyy=endYear; yyyy>=baseYear; yyyy--)
Delete