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
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.

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.