Jump to content

New to Ajax, need some help getting started


snowdog

Recommended Posts

I have a dealers select box that when I select it, I then want to load the dealers staff names into the next select box. I have been reading and I have this as the progress in my head.

 

on submit, the ajax needs to send over the dealer_id to a php file that will then do a database query to get the staff members of that dealer_id and send the name and staff_id back to the html page to populate the dealers_staff select box. Once that is done it needs to do it all over again to get the dealers_staff member's name, email address and contact numbers to ill in some text boxes. So I think I understand what has to happen just don't know what to do to make it happen now.

 

 

Thanks

 

Snowdog

Link to comment
Share on other sites

Update: I have completed my php code that ajax will access. I have read tutorials out the wazoo and think I have come up with my ajax call. I need to populate a new drop down with the returned code from the php file. Having a hard time understanding how to get the information out of the returned array and jsdon decoded no to loop through the information and create the drop downs I guess with javascript now? Here is the code so far:

 

Jquery ajax function: 

<script id="source" language="javascript" type="text/javascript">

  jQuery(function () 
  {
    //-----------------------------------------------------------------------
    // 1) Send a http request with AJAX
    //-----------------------------------------------------------------------
    jQuery.ajax({                                      
      url: 'ajax/appointment_ajax_1.php',   //the script to call to get data          
      data: "dealer_id = " + jQuery('dealer_id').val(),                        		//you can insert url argumnets here to pass to api.php
                                       		//for example "id=5&parent=6"
      dataType: 'json',                		//data format      
      success: function(data)          		//on recieve of reply
      {
        var first = data[0];              	//get firstname
        var last = data[1];           		//get lastname
        var id = data[2];           		//get id
        //--------------------------------------------------------------------
        // 2) Update Dealer's Staff Select Box
        //--------------------------------------------------------------------
        jQuery('#output').html("<option name = 'name' value = "+id+">"+first+"  "+last"</option>"); 	//Set option value and display       
      } 
    });
  }); 

  </script>

 

And here is the php file:

<?php

	//--------------------------------------------------------------------------
	// php/ajax script for onchange from add appointments
	//--------------------------------------------------------------------------

	session_start();

	//--------------------------------------------------------------------------
	// 1) Connect to mysql database
	//--------------------------------------------------------------------------

	include("../include/db_connect.php");

	//--------------------------------------------------------------------------
	// 2) Query database for data
	//--------------------------------------------------------------------------

	// Create container array
	$dealer_staff_array = array();

	$franchise_id = $_SESSION['franchise_id'];
	$dealer_id = $_POST['dealer_id'];
					
	$SQL = "SELECT * FROM login WHERE franchise_id = '$franchise_id' and dealer_id = '$dealer_id' ORDER BY name ASC";
	$result = mysql_query($SQL) or die(mysql_error());							
					
	while($row = mysql_fetch_array($result))
	{

	//--------------------------------------------------------------------------
	// 3) echo result as json string 
	//--------------------------------------------------------------------------

	$response = array(		            
		  'first' => $row['first'],
		  'last' => $row['last'],
		  'id' => $row['id']	
		  );
		   array_push($dealer_staff_array, $response);

	}

	echo json_encode($response);
?>

 

Any help would be appreciated. Thanks

 

Snowdog

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.