Author Topic: newbie... help needed, php ajax  (Read 526 times)

0 Members and 1 Guest are viewing this topic.

Offline robtriTopic starter

  • Irregular
    • View Profile
newbie... help needed, php ajax
« on: June 19, 2009, 03:58:29 AM »
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...

Offline Ken2k7

  • Fanatic
    • View Profile
Re: newbie... help needed, php ajax
« Reply #1 on: June 20, 2009, 02:24:00 AM »
Change $row['car_model'] to $row['model']. You may want to put single quotes around $make in your SQL.
Quote from: Slaytanist
A programmer who shys away from elegant tricks will never be more than competent at best. Ego and a desire to attempt the impossible are traits of most great coders.


Offline robtriTopic starter

  • Irregular
    • View Profile
Re: newbie... help needed, php ajax
« Reply #2 on: June 22, 2009, 09:47:11 AM »
thanks, appreciate that, tried it out and still no joy....

Offline xtopolis

  • Devotee
  • Gender: Male
  • [Can't stop. Won't stop.]
    • View Profile
Re: newbie... help needed, php ajax
« Reply #3 on: June 22, 2009, 11:43:20 PM »
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:
Code: [Select]
<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.
« Last Edit: June 22, 2009, 11:44:02 PM by xtopolis »

Offline djjjozsi

  • Irregular
    • View Profile
    • Daily php
Re: newbie... help needed, php ajax
« Reply #4 on: June 30, 2009, 11:07:39 AM »
if you expected a string as a condition use the apostrofes around the data

like:
Code: [Select]
$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

-- most of the examples untested -- Its not a bug its a feature

PHP Freaks Forums

« on: »

Tired of these ads? Purchase a supporter subscription to get rid of them.