Ajax Return POST var

I am in the process of creating my first Ajax function.  I am successfully sending a POST to my php file via Ajax.  What I am confused about is how to return the info to the original page.
Script on Index.html
<script>
$(document).ready(function () {	

	function ajaxCallback() {
		var state = $('select[name*="[{row_id}][association_membership]"]').find(':selected').val();
	
		$.ajax({
      	url: "view_cart_ajax.php",
       	type: "POST",
       	data: {state: state},
      [b]  success??????[/b]
    	});
	};

[b]//  I need to bring the variable back to here  //[/b]
if(liability_option_value == 'liability_25') {
        		 		liability_rate = '<?php if(isset( $liability_25 )) { echo $liability_25; } else { echo '0'; } ?>';
        		 }	else if (liability_option_value == 'liability_50' )   {
        		 		liability_rate = '<?php if(isset( $liability_50 )) { echo $liability_50; } else { echo '0'; } ?>';
        		 }	else if (liability_option_value == 'liability_100' )   {
        		 		liability_rate = '<?php if(isset( $liability_100 )) { echo $liability_100; } else { echo '0'; } ?>';
        		 }	else if (liability_option_value == 'liability_300')   {
        		 		liability_rate = '<?php if(isset( $liability_300 )) { echo $liability_300; } else { echo '0'; } ?>';
        		 }	else if (liability_option_value == undefined)   {
        		 		liability_rate = 0;
        		 }	else {
 					liability_rate = 0;
 				}	
 	alert('Liability Rate:: ' + liability_rate);	


               $( "select" ).on("change", ajaxCallback);
	}); 
</script>

<html>
<form method="post">

<select name="item_options[0][product_liability]" >
<option value="">Select a Value</option>
<option  value="liability_25"> $25,000 </option>
<option  value="liability_50"> $50,000 </option>
<option  value="liability_100"> $100,000 </option>
<option value="liability_300" selected="selected"> $300,000 </option>
</select>

<input type="submit" value="submit" />
</form>
</html>

Open in new window


The PHP file:
<?php
	$bike_state = $_POST['state'];
	$pd_base_liabel = ee()->db->select('*')
		->from('exp_state_rates')
		->where('state', $bike_state)
		->limit('1')
		->get();
		
		$bsl = $pd_base_liabel->result_array();
		
		$pd_base_liabel->free_result();	
	
		  foreach($bsl as $row) {
			$liability_25 = $row['liability_25'];
			$liability_50 = $row['liability_50'];
			$liability_100 = $row['liability_100'];
			$liability_300 = $row['liability_300'];

		  }

Open in new window

LVL 8
rgranlundAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marco GasiFreelancerCommented:
Once you get your array you have to echo it with json_encode (look at the script I posted in your previous question)

<?php
	$bike_state = $_POST['state'];
	$pd_base_liabel = ee()->db->select('*')
		->from('exp_state_rates')
		->where('state', $bike_state)
		->limit('1')
		->get();
		
		$bsl = $pd_base_liabel->result_array();
		$pd_base_liabel->free_result();	

		echo json_encode($bsl);

Open in new window

in success you have to do what you have to do. In my example is clearly illustrated how to use the retirned value to fill a select, but obviously the code depends on what you want to do with your data

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgranlundAuthor Commented:
I'm new at this, so it is not very clear.  My return is needed within the javascript, not to fill in a select or input, so I'm not really sure how to return my query/
Marco GasiFreelancerCommented:
Ok. I don't know what you want to do with your data, but keep in mind they are returned a an array. If you look at my example, you see I iterate through the returned array and I get values using associative index to fill the select: you can do the same but instead of filling the select you'll do something different... Can you tell us what is the use of those data?
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

rgranlundAuthor Commented:
I need to fill in the javascript with the following inf:
[b]//  I need to bring the variable back to here  //[/b]
if(liability_option_value == 'liability_25') {
        		 		liability_rate = '<?php if(isset( $liability_25 )) { echo $liability_25; } else { echo '0'; } ?>';
        		 }	else if (liability_option_value == 'liability_50' )   {
        		 		liability_rate = '<?php if(isset( $liability_50 )) { echo $liability_50; } else { echo '0'; } ?>';
        		 }	else if (liability_option_value == 'liability_100' )   {
        		 		liability_rate = '<?php if(isset( $liability_100 )) { echo $liability_100; } else { echo '0'; } ?>';
        		 }	else if (liability_option_value == 'liability_300')   {
        		 		liability_rate = '<?php if(isset( $liability_300 )) { echo $liability_300; } else { echo '0'; } ?>';
        		 }	else if (liability_option_value == undefined)   {
        		 		liability_rate = 0;
        		 }	else {
 					liability_rate = 0;
 				}	

Open in new window

rgranlundAuthor Commented:
Here is what I have so far.  My first alert does not fire, so I don't think the Ajax is firing:
<script>
$(document).ready(function () {	

	function ajaxCallback() {
		var state = $('select[name*="[0][liability_total]"]').find(':selected').val();
	alert(state);	
		$.ajax({
      	url: "view_cart_ajax.php",
       	type: "POST",
       	data: {state: state},
       	dataType: 'json',
       	success: function ( bsl )
                  {
                    $( bsl ).each( function ( liability_option_value )
                    {
                      if(liability_option_value == 'liability_25') {
        		 		liability_rate = 'liability_25';
        		 		}	else if (liability_option_value == 'liability_50' )   {
        		 		liability_rate = 'iability_50';
        		 		}	else if (liability_option_value == 'liability_100' )   {
        		 		liability_rate = 'liability_100';
        		 		}	else if (liability_option_value == 'liability_300')   {
        		 		liability_rate = 'liability_300';
        		 		}	else if (liability_option_value == undefined)   {
        		 		liability_rate = 0;
        		 		}	else {
 							liability_rate = 0;
 						}
                    });
                  },
                  error: function ( jqXHR, textStatus, errorThrown  )
                  {
                        console.log(textStatus + " " + errorThrown);
                  }
    	});
    	alert("AJAX RETURN:: " + liability_rate);	
	};

 			$("form").on("submit", ajaxCallback);

	});  //  END Document Ready
</script>


<?php
<?php
	$bike_state = $_POST['state'];
	$pd_base_liabel = ee()->db->select('*')
		->from('exp_state_rates')
		->where('state', $bike_state)
		->limit('1')
		->get();
		
		$pd_base_liabel->free_result();	
	
	
		while ($row = $pd_base_liabel->result_array())
		 {
			$bsl[] = $row;
		  }
		  
		  echo json_encode($bsl);

?>

Open in new window

rgranlundAuthor Commented:
Actually, The first alert is firing, It just is not returning any values
rgranlundAuthor Commented:
Marco, I am going to start this question over with clean code.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
AJAX

From novice to tech pro — start learning today.