Jump to content

newbie... help needed, php ajax


robtri

Recommended Posts

hi, very new to this php and ajax world, just learning it as a personal project... I am currently trying to put together two drop down menu items, of car makes and when a make is selected it picks a model..

 

here's the code I have for both files

 

<html>

<head>

<title>Selentry5 test</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

 

<script>

function getXMLHTTP() { //fuction to return the xml http object

var xmlhttp=false;

try{

xmlhttp=new XMLHttpRequest();

}

catch(e) {

try{

xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");

}

catch(e){

try{

xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

}

catch(e1){

xmlhttp=false;

}

}

}

 

return xmlhttp;

}

 

 

 

function getModel(strURL) {

 

var req = getXMLHTTP();

 

if (req) {

 

req.onreadystatechange = function() {

if (req.readyState == 4) {

// only if "OK"

if (req.status == 200) {

document.getElementById('citydiv').innerHTML=req.responseText;

} else {

alert("There was a problem while using XMLHTTP:\n" + req.statusText);

}

}

}

req.open("GET", strURL, true);

req.send(null);

}

 

}

</script>

 

 

 

 

</head>

 

<body>

 

 

 

And for findmodel.php

 

<?php

include ("dbconnect.php");

doDB();

 

if (mysqli_connect_errno()) {

printf("Connect failed: %s\n", mysqli_connect_error());

exit();

} else {

$make=$_REQUEST['make'];

 

 

$query="select model from car_makes where car_make=$make";

$result=mysql_query($query);

 

?>

<select name="model">

<option>Select Model</option>

<? while($row=mysql_fetch_array($result)) { ?>

<option value><?=$row['car_model']?></option>

<? } ?>

</select>

 

 

 

when I load this, it produces the two drop down boxes but I get nothing in the model drop down??????

 

any help on this will really be appreciated...

 

Link to comment
https://forums.phpfreaks.com/topic/162881-newbie-help-needed-php-ajax/
Share on other sites

I highly recommend changing all of your short tags (<? ?>) to proper tags (<?php ?>) as using short tags is considered bad practice.

 

I think this block should be modified to:

<select name="model">
<option>Select Model</option>
<?php
while($row=mysql_fetch_array($result)) {
  echo '<option value="' . $row['car_model'] . '">' . $row['car_model'] . '</option>';
}
?>
</select>

 

That may not change anything however, but I also don't see where you call getModel() anywhere?  It's defined, but not called in the code you have.

if you expected a string as a condition use the apostrofes around the data

 

like:

$query=sprintf("select * from car_makes where car_make='%s'" , 
mysqli_real_escape_string($make);

Lets select that field which you're using on the following codes.

 

you have opened a mysqli connecttion, but then you should use the

 

mysqli_query() on this select, not the mysql_query

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.