Jump to content

php > jquery > php variable


SmileyWar

Recommended Posts

Hiya guys,

 

I'm stuck, my initial idea is to dynamically from on original state and selection change to update a price change from mysql.

 

Here is what i have which works, but i cant seem to get the price for my submit button as the jquery sends the price in a div class which cant be placed in a form element, so i was trying to find out if can change the jquery code to place it in a php variable.

 

Btw i just started out with jquery so im still clueless

 

i want the input field to update dynamically too

<input type='hidden' name='product_cost' value='<?php echo $get[$i]['price']; ?>' />

 

 

This code is inserted on hundreds of pages and the id is changed plus other options which will trigger a result in the included script

<?php
if ($i == NULL){$i = "1";}


/////////////////////// ONLY MODIFY THIS SECTION //////////////////////
///////////////////////////////////////////////////////////////////////
$p[$i] = "532"; 													/// Insert Product ID
${pkey_.$i} = "yes"; 												/// Add keynumber field? yes or no
${poptions_.$i} = "(1)";											///	Set Minimum Start Number of Quantity || (1) means only one option
${return_url_.$i} = "mof";											///	cururl = return to current url || mof = alternate page in mof
${dynamic_prices_.$i} = "yes";										/// Dynamic prices are prices that change upon interaction with viewer set "yes" or "no"
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////


include($_SERVER['DOCUMENT_ROOT'] ."/product_include/script2.php"); #Scripts INCLUDE 

?> 

 

this is the included script which displays the results

 

<script type="text/javascript">
$(document).ready(function() {
     $.post('get_dynamic_prices.php', $("#productPrice").serialize(), function(data) {
				$('.dynamicprice').html(data);
				});   
$("#productPrice").change(function(){
			$.post('get_dynamic_prices.php', $("#productPrice").serialize(), function(data) {
				$('.dynamicprice').html(data);
				});
			});					
    });
</script>
<?php

/////////////
#			DO NOT MODIFY BELOW			#
/////////////

$url = curPageURL();

$result[$i] = mysql_query("SELECT * FROM products_prices WHERE product='".$p[$i]."' ",$connect) or die(mysql_error());
$get[$i] = mysql_fetch_array($result[$i]);

if ($get[$i]['quantity'] > "0"){${switch1_.$i} = ""; ${switch2_.$i} = "none";}
else{${switch1_.$i} = "none"; ${switch2_.$i} = "";}

if (${pkey_.$i} == "yes"){${switch_col_.$i} = ""; ${switch_colspan_.$i} = "5";}
elseif (${pkey_.$i} == "no"){${switch_col_.$i} = "none"; ${switch_colspan_.$i} = "4";}

if (${return_url_.$i} == "cururl"){${r_url_.$i} = $url;}
elseif (${return_url_.$i} == "mof"){${r_url_.$i} = "/mof/".$get[$i]['product']."_mof.php";}
else {${r_url_.$i} = $url;}

?>
<style type="text/css"> 
.robot { 
/*display : none ; */
} 
</style> 
<div id="emailme-<?php echo $i; ?>" style="display:none;">
    <div class="chain-email-me-popup">
    	<h1 class="chain-email-me-popup-h1">Contact me when in stock</h1>
        <div class="chain-email-me-form"> 
        Thank you for your interest in <?php echo "Thule ".$get[$i]['product']." ".$get[$i]['description'].""; ?>.<br /><br />
	Please complete your details.
        	
            *All fields below required<br /><br />
            <form name="emailme" action="/emailme/index2.php" method="POST">
            <label for="firstname">Firstname*</label><br />
            <input value="" name="fname" /><br />
            <label for="sname">Surname*</label><br />
            <input value="" name="sname" /><br />
            <label for="email">Email Address*</label><br />
            <input value="" name="email" /><br /><br />
            <input type="hidden" name="productID" value="<?php echo $get[$i]['product']; ?>" />
            <input type="hidden" name="productBrand" value="Thule" />
            <input type="hidden" name="productDescription" value="<?php echo $get[$i]['description']; ?>" />
            <input type="hidden" name="productURL" value="<?php echo $url; ?>" />
            <div class="robot"> 
            <p>Please don't change the next two fields. It's only to avoid spam</p> 
            <input type="text" name="address2" id="address2" value="sfjli"> 
            <input type="text" name="address3" id="address3" value=""> 
            </div> 
            <input type="submit" value="Submit"><br />
            </form>
            <br /><br />
            We'll only use your details to contact you about this product, and not for marketing purposes.
        </div>     
    </div>    
    </div>
    
    <?php
// Dynamic prices opening or closing form field
if (${dynamic_prices_.$i} == "yes")
{
	echo '';
	}
elseif (${dynamic_prices_.$i} == "no")
{
	echo '
	<form method=POST action="/basket/basket.php">
	<input type="hidden" name="type" value="single">
	<input type="hidden" name="product_link" value="'.$url.'" />
        <input type="hidden" name="return_url" value="'.${r_url_.$i}.'" />  
	';
	}
// fallback for older includes	
else
{
	echo '
	<form method=POST action="/basket/basket.php">
	<input type="hidden" name="type" value="single">
	<input type="hidden" name="product_link" value="'.$url.'" />
        <input type="hidden" name="return_url" value="'.${r_url_.$i}.'" />  
	';
	}	
?>

        
<table class="P5-table">
<tr>
<td class="P-description" colspan="<?php echo ${switch_colspan_.$i}; ?>">
<?php echo $get[$i]['notes1']; ?> <em><?php echo $get[$i]['notes2']; ?></em>
<span><?php echo $get[$i]['notes3']; ?></span>
</td>
</tr>

<tr>

<td class="P5-qty" style="display:<?php echo ${switch1_.$i}; ?>">
	<?php 
	if (${dynamic_prices_.$i} == "yes"){
		echo '
		<form name="productPrice" id="productPrice" action="" method="POST"> 
			<label for="productID">Select Qty</label> 
				<select name="productID" id="productID">
					<option value="532-s">1 </option>
					<option value="532-tp">2</option>
					<option value="532-3">3</option>
					<option value="532-4">4</option>
					<option value="532-5">5</option>
					<option value="532-6">6</option>
				</select>
		</form>
		';
		}
	elseif	(${dynamic_prices_.$i} == "no"){
		echo '<select name="product_quantity" size="1"><option value="';
		if (${poptions_.$i} == "(1)"){echo "1";}elseif (${poptions_.$i} == "evennumbers"){echo "2";} else{ echo ${poptions_.$i};}
		echo '">Qty </option>';
           
		if (${poptions_.$i} == "1"){echo '<option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option>';} 
		if (${poptions_.$i} == "2"){echo '<option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option>';}
		if (${poptions_.$i} == "3"){echo '<option value="3">3</option><option value="4">4</option><option value="5">5</option>';}
		if (${poptions_.$i} == "4"){echo '<option value="4">4</option><option value="5">5</option>';}
		if (${poptions_.$i} == "5"){echo '<option value="5">5</option>';}
		if (${poptions_.$i} == "(1)"){echo '<option value="1">1</option>';}

        	echo '</select>';
	}
	// fallback for older includes
	else {
		echo '<select name="product_quantity" size="1"><option value="';
		if (${poptions_.$i} == "(1)"){echo "1";}elseif (${poptions_.$i} == "evennumbers"){echo "2";} else{ echo ${poptions_.$i};}
		echo '">Qty </option>';
           
		if (${poptions_.$i} == "1"){echo '<option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option>';} 
		if (${poptions_.$i} == "2"){echo '<option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option>';}
		if (${poptions_.$i} == "3"){echo '<option value="3">3</option><option value="4">4</option><option value="5">5</option>';}
		if (${poptions_.$i} == "4"){echo '<option value="4">4</option><option value="5">5</option>';}
		if (${poptions_.$i} == "5"){echo '<option value="5">5</option>';}
		if (${poptions_.$i} == "(1)"){echo '<option value="1">1</option>';}

        	echo '</select>';
	}

	?>
</td>

<td class="P5-price">
<div class="P-price-container">
<div>Our Price</div>
<div class="P-price-container-subcontainer">
<div class="P-price-pound">£</div>
<div class="P-price-value">
<?php if (${dynamic_prices_.$i} == "yes"){echo '<div class="dynamicprice"><div>';} 
			elseif (${dynamic_prices_.$i} == "yes"){ echo $get[$i]['price'];} 
			else echo $get[$i]['price']; ?>
</div> 
</div>               
</div>                
</td>  
      
      <?php
// Dynamic prices opening or closing form field
if (${dynamic_prices_.$i} == "no")
{
	echo '';
	}
elseif (${dynamic_prices_.$i} == "yes")
{
	echo '
	<form method=POST action="/basket/basket.php">
	<input type="hidden" name="type" value="single">
	<input type="hidden" name="product_link" value="'.$url.'" />
        <input type="hidden" name="return_url" value="'.${r_url_.$i}.'" />  
	';
	}
// fallback for older includes	
else
{
	echo '';
	}	
?>
      
<td class="P5-keynumber" style="display:<?php echo ${switch1_.$i}; echo ${switch_col_.$i}; ?>">
Key Number: <img class="fbTooltip" data-fb-tooltip="source:#tooltip1 attachToHost:true" src="/template_load/img/infox15.png"/><br />
  <input name="product_key" type="text" class="BoxAreaFormat" id="quantity" value="N" size="3" maxlength="4" width="75" />
</td>

<td class="P5-addtobasket" style="display:<?php echo ${switch1_.$i}; ?>">
  <input type='hidden' name='product_id' value='<?php echo $get[$i]['product']; ?>' />
  <input type='hidden' name='product_name' value='<?php echo $get[$i]['description']; ?>' />
  <input type='hidden' name='product_cost' value='<?php echo $get[$i]['price']; ?>' />
  <input type='hidden' name='product_thumb' value=/images/shop/<?php echo $get[$i]['product']; ?>.jpg' />
  <input type='hidden' name='product_thumb_large' value='/images/large/<?php echo $get[$i]['product']; ?>.jpg' />
  <input type='hidden' name='is_key' value='0' />	
  <?php
  if (${dynamic_prices_.$i} == "yes") {
  echo '<input type="hidden" name="product_quantity" value="1" />';
  }
  elseif (${dynamic_prices_.$i} == "no") {
  echo '';}
  // fallback for older includes	
  else {echo '';}
  ?>	
  <input name="add_to_basket" type="image" src="template_load/img/addtobasket_new.png" 
alt="Click here to add items to your shopping basket" align="absmiddle"/>
</td>

<td class="P5-stock" style="display:<?php echo ${switch1_.$i}; ?>">In Stock</td>

<td class="P-outofstock" colspan="3" style="display:<?php echo ${switch2_.$i}; ?>">
<p class="floatbox" data-fb-options="modal:false disableScroll:true width:500 scrolling:no enableDragResize:true cornerRadius:4 padding:12 colorTheme:black, contentBackgroundColor:#ffffe7">
This item is out of stock, 
<a href="#emailme-<?php echo $i; ?>" data-fb-options="caption:`Contact me when in Stock` boxLeft:+0% boxTop:100">Email me when in stock</a></p>
</td>
</form>
</tr>
</table>
	<!--</form> -->
<?php $i++; ?>        

 

get_dynamic_prices.php

<?php
$productID = $_POST['productID'];
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/mysql_connect.php");

$result = mysql_query("SELECT * FROM products_prices WHERE product='".$productID."'") or die(mysql_error()); 
$row = mysql_fetch_array($result);

echo $row['price'];

mysql_close($connect);
?>

Link to comment
Share on other sites

Another thing i was thinking was to have jquery to load the information from the same page

 

then place an if (!isset($_POST['productID']) but i'm was not succesfull in getting jquery to load the information from the same page which is possible with php but on refresh page.

 

the following code is with the query on the same page with !isset, any idea's on this concept?

 

But then it i'm unsure if it would update the variable

 

<?php
        
        if (!isset($_POST['productID']){


$productID = $_POST['productID'];
include_once($_SERVER['DOCUMENT_ROOT'] ."include/mysql_connect.php");

$result = mysql_query("SELECT * FROM products_prices WHERE product='".$productID."'") or die(mysql_error()); 
$row = mysql_fetch_array($result);

echo $row['price'];
        $$get[$i]['price'] = $row['price'];

mysql_close($connect);
}
?>

<script type="text/javascript">
$(document).ready(function() {
     $.post('#', $("#productPrice").serialize(), function(data) {
				$('.dynamicprice').html(data);
				});   
$("#productPrice").change(function(){
			$.post('#', $("#productPrice").serialize(), function(data) {
				$('.dynamicprice').html(data);
				});
			});					
    });
</script>

Link to comment
Share on other sites

Here is what i have which works, but i cant seem to get the price for my submit button as the jquery sends the price in a div class which cant be placed in a form element, so i was trying to find out if can change the jquery code to place it in a php variable.

 

You said you can't get the price because it's not in a form element, but I'm saying in addition to putting the price in the div update the hidden field as well. Then the price is in the form.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.