phpmax84
phpmax84

Reputation: 1

display values on a textbox when click one from a jquery result with php

when I Select a date from the html5 date field, It will appear list of available vehicles for that date.I Used jquery $.post to retrive data. Now I need to select one of the vehicle number and driver id from the list and diplay those values in the text fields in the same form without refreshing the form.

<script type="text/javascript">
        
        $(document).ready(function(){

          //send data to process if vehicles available for the selected date from the input
        $('#Odate').change(function(){
          var cdate=$(this).val();
          
          $.post('searchExistingDrivers.php',{dates:cdate}, function(data) {

          $('#odateInfo').slideDown(300);
          $('#odateInfo').html(data);
         
          //alert(data);
        });
        });
        

        $('a').click(function(){
            var id = $(this).attr('id'); //Fetch Vehicle Number
            var parent = $(this).parent();
            parent.slideUp('slow', function() {$(this).remove();});
             
              $("#vehID").val(id);
              $("#DID").val(name); 
             });   
  </script>

After some modifications of my code like below it dispay on checkbox values as [object Object] how to resolve this problem?

$(document).on('click', 'a', function() {
            var ID = $(this).val('id'); //Fetch Vehicle Number
            var Name=$(this).val('name');
            //alert(id);
            
             
              $("#vehID").val(ID);
              $("#DID").val(Name); 
             });       

PHP Code

<?php
 
// Data could be pulled from a DB 

include('database.php');

// Cleaning up the term

 $term = $_POST['dates'];
 

$query=mysql_query("SELECT * FROM vehiclereg WHERE NOT EXISTS (SELECT * FROM orders WHERE orders.VehID=vehiclereg.VehicleNo AND orders.OrderDate='$term') ");

echo '<table width=100%><tr>';
echo '<th>Vehicle No</th><th>DriverID</th></tr>';
while($row=mysql_fetch_array($query)){
	
		$VehNo=$row['VehicleNo'];
		$DriverID=$row['DriverID'];

		echo '<tr><td><a href=order.php?id=".$VehNo.">'.$VehNo.'</a></td>';
		echo '<td><a href=order.php?name=".$DriverID.">'.$DriverID.'</a></td></tr>';
		
		
}
echo '</table>' ;

?>

Upvotes: 0

Views: 913

Answers (2)

phpmax84
phpmax84

Reputation: 1

Finally found the solution for my problem. This will helps who working with same type of Jquery post.

$(document).on('click', 'tr', function(e) {
           e.preventDefault();
            var ID = $(this).find('a').first().text();//Fetch Vehicle Number
            var Name=$(this).find('a').last().text();//Fetch Driver ID Number
                         
              $("#vehID").val(ID);
              $("#DID").val(Name); 
             });       

Upvotes: 0

user4299190
user4299190

Reputation:

Problem is that you try to attach event on tag which not exist yet (it is rendered after ajax call). You can try this:

$(document).on('click', 'a#someId', function() {
    // whatever you need to do...
});

Upvotes: 1

Related Questions