Avatar of Robert Granlund
Robert Granlund
Flag for United States of America asked on

jQuery Var in a PHP Query

Is there a way to place a jQuery VAR in a PHP Query?
<script>
	var lineName ="line_25"	
        	<?php	
        		$pd_base_liabel = $this->EE->db->select('id, state, pd_base_rate')
				->from('exp_state_rates')
				->where('id', '+ lineName+')
				->limit('1')
				->get();

Open in new window

PHPjQueryJavaScript

Avatar of undefined
Last Comment
Member_2_248744

8/22/2022 - Mon
Alexandre Simões

What are you trying to achieve?

That var there, as it stands, will be declared in the global scope which isn't a good idea...
Also PHP code runs server-side and javascript runs client-side... what are you expecting to do with them?
Big Monty

Is there a way to place a jQuery VAR in a PHP Query?

yes, but as Alexandre said, PHP runs on the server side, and is processed before the client side code is. You would need to use AJAX (or a simple form submission, if all you're trying to do is pass the value to the server.

if you be more specific on what you're trying to accomplish, it'll be much easier to help
Robert Granlund

ASKER
I have a form that onChange calculates an updates price.  Part of the price is determined by a value kept in the DB.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Big Monty

you're better off saving that value to a hidden variable upon page load, then you can access it like you would any other element on the page
Chris Stanyon

In the code above, you know what lineName is because you're hardcoding it in, so you could just use that directly in your php.

Depending on the element that you're binding the change() event to, you might be able to build your HTML in such as way that you don't need to make an AJAX request - you just pull the records from your database and build your element's HTML in PHP - then all your values will be available on the client-side.

We'd need to know a bit more about exactly what you're tying to do
Dave Baldwin

AJAX is the only way to do that after the page is loaded.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Member_2_248744

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Robert Granlund

ASKER
Here is most of the code and I am wondering if there is a better way to write this.  The php works because there is a for that submits.  When the form submits, the values are obtained.  Once the specific values are there the jQuey onChange just uses what ever values have been set.

<?php
	$pd_base_liabel = ee()->db->select('*')
		->from('exp_state_rates')
		->where('id', $bike_state)
		->limit('1')
		->get();	
		$bsl = $pd_base_liabel->result_array();
		$pd_base_liabel->free_result();	
		  foreach($bsl as $row) {
			$medpay_1000 = $row['medpay_1000'];
			$medpay_2500 = $row['medpay_2500'];
			$medpay_5000 = $row['medpay_5000'];
			$medpay_7500 = $row['medpay_7500'];
			$medpay_10000 = $row['medpay_10000'];
			$medpay_15000 = $row['medpay_15000'];
			$medpay_20000 = $row['medpay_20000'];
			$medpay_25000 = $row['medpay_25000'];
		  }
?>	

<script>
function() {
	var liability_option_value = $('select[name*="[product_liability]"]').val();
        	
        		if(liability_option_value == "liability_25") {
        		 		liability_rate = "<?php echo $liability_25; ?>";
        		 }	else if (liability_option_value == "liability_50" )   {
        		 		liability_rate = "<?php echo $liability_50; ?>";
        		 }	else if (liability_option_value == "liability_100" )   {
        		 		liability_rate = "<?php echo $liability_100; ?>";
        		 }	else if (liability_option_value == "liability_300" )   {
        		 		liability_rate = "<?php echo $liability_300; ?>";
        		 }	else {
 					liability_rate = 0;
 				}	
}

</script>

Is there an easier way than writing out each line like that?

Open in new window

Chris Stanyon

Not really sure how your code ties together. You set a few $medpay* variables in your PHP but do nothing with them.

Your javascript functions refers to PHP variabels called liability*, but there's no indication of what these are or where they come from.

You have an anonymous javascript function - how does this get called. I absolutely certain there's a better way to do what you want, just can't quite figure out what you want.
Member_2_248744

you say this - " have a form that onChange calculates an updates price.  Part of the price is determined by a value kept in the DB."
I simply do not know what or how you do the "calculates an updates price" part of this thing, I guess that you need a price from the DB maybe it's the   pd_base_rate    column, but your select change with -

if(liability_option_value == "liability_25") {
        		 		liability_rate = "<?php echo $liability_25; ?>";
        		 }	else if (liability_option_value == "liability_50" )   {
        		 		liability_rate = "<?php echo $liability_50; ?>";
        		 }	else if (liability_option_value == "liability_100" )   {
        		 		liability_rate = "<?php echo $liability_100; ?>";
        		 }	else if (liability_option_value == "liability_300" )   {
        		 		liability_rate = "<?php echo $liability_300; ?>";
        		 }	else {
 					liability_rate = 0;
 				}

Open in new window


makes no sense to calculate a price, do you mean get a Price ? ? or do you multiply by a quantity of items?
can not understand?

can you show use the HTML for the <form> that you use?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Robert Granlund

ASKER
Here is most of the code and I am wondering if there is a better way to write this.  The php works because there is a for that submits.  When the form submits, the values are obtained.  Once the specific values are there the jQuey onChange just uses what ever values have been set.

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

	echo '<pre>';
	var_dump($pd_base_liabel);
	echo '</pre>';
	
		  foreach($bsl as $row) {
			$liability_25 = $row['liability_25'];
			$liability_50 = $row['liability_50'];
			$liability_100 = $row['liability_100'];
			$liability_300 = $row['liability_300'];
		  }
?>	

<script>
function() {
	var liability_option_value = $('select[name*="[product_liability]"]').val();
        	
        		if(liability_option_value == "liability_25") {
        		 		liability_rate = "<?php echo $liability_25; ?>";
        		 }	else if (liability_option_value == "liability_50" )   {
        		 		liability_rate = "<?php echo $liability_50; ?>";
        		 }	else if (liability_option_value == "liability_100" )   {
        		 		liability_rate = "<?php echo $liability_100; ?>";
        		 }	else if (liability_option_value == "liability_300" )   {
        		 		liability_rate = "<?php echo $liability_300; ?>";
        		 }	else {
 					liability_rate = 0;
 				}	
}

</script>

Is there an easier way than writing out each line like that?

Open in new window

Member_2_248744

since there is no code for a <form>, then I guess there is no better way to write that?
SOLUTION
Chris Stanyon

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Robert Granlund

ASKER
@ Slick812 Thanks for the info.  Is there a good place online where I can learn this specific function?

@Chris.  Thanks for this information.  It will really help me in the future.  After looking at this response I had a light bulb moment.

@ Ray.  As always, thanks for the informative reading.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Member_2_248744

@rgranlund, , , I am not one to look for beginers in AJAX web stuff, but here is the Jquery Manual page for one of the least difficult Ajax as the Jquery  post()  method -

$.post( "test.php", { name: "John", time: "2pm" })
  .done(function( received ) {
   $( "#showme" ).html(received );
  });

      http://api.jquery.com/jQuery.post/

and at the w3schools site -
    http://www.w3schools.com/jquery/ajax_post.asp

please consider looking and studying these codes abour Non Jquery Ajax in JS using the XMLHttpRequest Object -
    http://www.w3schools.com/ajax/ajax_intro.asp
I have been able to more effectively use Ajax because I took the time and effort to learn the native XMLHttpRequest Object use and the particulars need to do special  transactions between browser and server, there is much more to the wide options for data exchange between JS and PHP then the simple Jquery examples ever show. One thing that really helped me was using the many HEADER sends that are available to use like -


ajaxObj.getResponseHeader("Content-Type");
var eTag=ajaxObj.getResponseHeader("ETag");
   ajaxObj.setRequestHeader ("If-None-Match", mrk);