Calling a php function within a javascript code

Hello members!..
I have this php function; Can someone help me how to call it in javascript, I have tried some ways but it is not working…:headbang:

<?php //fetch Driver data from the Driver table
function getDriverData() {
    //sql to query data from Driver table
    $sql = 'SELECT `driverId`,`driverName` FROM `Drivers`';

    //create database connection
    $conn = dbConnect('admin');

    //process the query
    $results = mysql_query($sql) or die(mysql_error());

    print '<select name="driverId">';//print selection option start
    				//fetch associative arrray from data
    while($row = mysql_fetch_assoc($results)) {
        print '<option value="';
        print $row['driverId'];
        print '">';
        print $row['driverName'];
        print '</option>';
    }
    print '</select>';//print selection option end
	}
	?>

and this is the javascript code:

$(document).ready(function(){
    $("#mySelect").change(function(){
    var y = $(this).val()
     var data='';
	
    for (i=1;i<=y;i++)
   {
   data +='<fieldset ><legend> Car'+ i +' details</legend><label><?php [B]getDriverData();[/B] ?>
  data +='<label>car insurance no:</label><input type="text" name="insurance[]" /><label>vehicle type</label><input type="text" name="v_type[]" /><br>';
 data +='<label>ownership:</label><input type="text" name="ownership[]" /><label>vehicle No:</label><input type="text" name="v_no[]" /></fieldset>';

   }
    $('#display').html(data);
  });
  });

You will need to use an AJAX call to your PHP script which is the absolute most simple way without changing your code. The jQuery method you can use is jQuery.[B]ajax/B, click the ajax link for full documentation.

OMG!..I do not understand a thing about jQuery, guess will have to study that first!..I’m real running outer time and need to finish the assignment. Isn’t there a shortcut :smiley:

You could do the whole thing in JavaScript if you don’t understand jQuery - you need to understand Javascript before you can use jQuery properly anyway since jQuery is written in JavaScript.

I added the above this to the javascript code…nothing seem to be happening what am I doing wrong?

$(document).ready(function() {
        $.get('getDriverData.php', function (data) {
		$('#getfrmdb').change(function(){							
        //    $('#background_container').html(data);
        });
    });
		});





$(document).ready(function(){
    $("#mySelect").change(function(){
    var y = $(this).val()
     var data='';
	
    for (i=1;i<=y;i++)
   {
   data +='<fieldset ><legend> Vehicle'+ i +' Details</legend><select name="driverId" id="getfrmdb"></select>';
  data +='<div><label class="fixedwidth">Vehicle Plate No:</label></div><input type="text" name="v_no[]" /><div><label class="fixedwidth">Vehicle Insurance No.</label></div><input type="text" name="insurance[]" /><br>';
 data +='<div><label class="fixedwidth">Vehicle Type:</label></div><input type="text" name="v_type[]" /><div><label class="fixedwidth">Ownership:</label></div><input type="text" name="ownership[]" /></fieldset>';

   }
    $('#display').html(data);
  });
  });

I added the above this to the javascript code…nothing seem to be happening what am I doing wrong?

$(document).ready(function() {
        $.get('getDriverData.php', function (data) {
		$('#getfrmdb').change(function(){							
        //    $('#background_container').html(data);
        });
    });
		});





$(document).ready(function(){
    $("#mySelect").change(function(){
    var y = $(this).val()
     var data='';
	
    for (i=1;i<=y;i++)
   {
   data +='<fieldset ><legend> Vehicle'+ i +' Details</legend><select name="driverId" id="getfrmdb"></select>';
  data +='<div><label class="fixedwidth">Vehicle Plate No:</label></div><input type="text" name="v_no[]" /><div><label class="fixedwidth">Vehicle Insurance No.</label></div><input type="text" name="insurance[]" /><br>';
 data +='<div><label class="fixedwidth">Vehicle Type:</label></div><input type="text" name="v_type[]" /><div><label class="fixedwidth">Ownership:</label></div><input type="text" name="ownership[]" /></fieldset>';

   }
    $('#display').html(data);
  });
  });

I can see things that need to be addressed before you can continue.

1.) Because the select box you adding to the page is not part of the original DOM you need to either use .delegate() or .live() as .click() only works on elements that are part of the original DOM.

2.) You now have an AJAX request but your not doing anything with the response HTML, is there a reason for this or do you require further assistance.

3.) Your JavaScript generated select box doesn’t have any option values, were these going to be generated by the AJAX repsonse.

@SgtLegend : I need further assistance yes, the aim of select box is to display the values from table drivers which I intended for these values to be populated by the Php code I sent ealier.

:rofl:…I finally found out how to do it! :D…@SgtLegend and felgall thank you, your advises helped