SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast nkacharani's Avatar
    Join Date
    Oct 2010
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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..
    Code PHP:
    <?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:

    Code JavaScript:
    $(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);
      });
      });

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,192
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.ajax(), click the ajax link for full documentation.

  3. #3
    SitePoint Enthusiast nkacharani's Avatar
    Join Date
    Oct 2010
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 :-D

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,836
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    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.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Enthusiast nkacharani's Avatar
    Join Date
    Oct 2010
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I added the above this to the javascript code..nothing seem to be happening what am I doing wrong?


    Code JavaScript:
    $(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);
      });
      });

  6. #6
    SitePoint Enthusiast nkacharani's Avatar
    Join Date
    Oct 2010
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I added the above this to the javascript code..nothing seem to be happening what am I doing wrong?


    Code JavaScript:
    $(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);
      });
      });

  7. #7
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,192
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.

  8. #8
    SitePoint Enthusiast nkacharani's Avatar
    Join Date
    Oct 2010
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @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.

  9. #9
    SitePoint Enthusiast nkacharani's Avatar
    Join Date
    Oct 2010
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...I finally found out how to do it! ..@SgtLegend and felgall thank you, your advises helped


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •