Hi,
It is my 1st attempt with Ajax and I'm facing some problems. Below parts of my code. First part is working ok. But then I need to get option id attribute instead of value. And it is not working. Could you help me and check it? Thx

index.php form

<select name="client_name" id="client_data" onchange="ajaxFunction5()">
	<option value="".$partner_name."">".$partner_name."</option>
</select>

ajax_select_box.js

function ajaxFunction5(){
 var ajaxRequest;  // magic variable
 
  try{
    // Opera 8.0+, Firefox, Safari
    ajaxRequest = new XMLHttpRequest();
  } catch (e){
    // Internet Explorer Browsers
    try{
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
     
      try{
        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e){
        // Something went wrong
        alert("Your browser broke!");
        return false;
      }
    }
  }
 
  // Receive Data Function
  ajaxRequest.onreadystatechange = function(){
   
    if(ajaxRequest.readyState == 4){
      var ajaxDisplay = document.getElementById('ajaxDiv5');
      ajaxDisplay.innerHTML = ajaxRequest.responseText;
    }
   
  }
  
  var client_data = document.getElementById('client_data').value;
 
  var queryString = "?client_data=" + client_data;
  ajaxRequest.open("GET", "contact_data.php" + queryString, true);
  ajaxRequest.send(null);
}

contact_data.php

$client_data = $_GET['client_data'];
<select name="contact_name" id="contact_data"onchange="ajaxFunction4()">
	
// ======================== Here goes mysql_query 
$wynik2 = mysql_query("SELECT partner_id, partner_name, contact_name FROM $sql_tabela WHERE partner_name LIKE '".$client_data."'");				
		
        while ($row = mysql_fetch_array($wynik2)) {
          $partner_id = intval($row['partner_id']);
		  $contact_name = $row['contact_name'];
		  
	<option id="$partner_id" value="$contact_name"> $contact_name </option>
}
</select>

ajax_select[...]

function ajaxFunction4(){
[...]// ======================== Here the problem starts

[B]var contact = document.getElementById('contact_data');
  var contact_data = contact.options[contact.selectedIndex].id;
 
  var queryString = "?contact_data=" + contact_data;
  ajaxRequest.open("GET", "client_data.php" + queryString, true);
  ajaxRequest.send(null);[/B]
}

client_data.php

$contact_data = $_GET['contact_data'];
// ======================== Here goes 2nd mysql_query

$wynik2 = mysql_query("SELECT partner_id, partner_name, partner_address, partner_post, partner_city, contact_name, contact_phone, 
					contact_email FROM $sql_tabela WHERE partner_id='".$contact_data."'");

Recommended Answers

All 3 Replies

Why would you add the 'id' property to 'option' tag? Would it be OK to use the value as '$partner_id' instead of '$contact_name'? If you want contact_name, you could use select.options[select.selectedIndex].innerHTML to get the contact_name instead. It is redundant... What if you remove the 'id' property and use 'value' property to store $partner_id instead, is it still a problem???

I need both. Code is part of online offering form. contact_name is stored in d-base (and send in the offer to the client). partner_id is to select right contact from chosen company. If I do like you propose I will have number instead of name-I use those vars in other parts of the code. I would like to avoid too deep reconstruction of it. I thought it could be good occasion to try ajax but I have failed. Is there any way to get option id? Maybe you can suggest other way to do it?

ok. it is working now. there was no mistake. it's just started. probably one of those mysteries...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.