0

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>' ;

?>

2 Answers 2

1

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...
});
Sign up to request clarification or add additional context in comments.

2 Comments

I Change this function $("#vehID").val(id); $("#DID").val(name); }); to $(document).on('click', 'a#id', 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); }); that. but page is still refreshing and clear data
Put this code on top (not in $('a').click(function(){}) ). Also, at end of function put return false to prevent click event on <a> link. Example: $(document).on('click', 'a', function() { var id = $(this).attr('id'); //Fetch Vehicle Number var parent = $(this).parent(); parent.slideUp('slow', function() {$(this).remove(); return false; });
0

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); 
             });       

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.