Jump to content

Price * Quantity input field, and auto update subtotal on onkeyup???


Presto-X

Recommended Posts

Hello everyone,

 

I'm working on a basic checkout page created in php, I get a list of products from a list products selected with checkboxes from another page, it submits the product title and price via POST. I am using a PHP FOR loop to print all of the names and prices out, what I want to do is have a quantity input text field where the user can type in the number of products they want to order then on onkeyup have it auto update the price via javascript... I have been playing with a lot of javascript over the past 12 hours and have not really been able to get any of it to work with my current project and now I am way sleepy just about to give up *sigh*... I am open to any and all ideas...

 

This is the php code that I'm using:

$i=0;
foreach($_POST as $name => $value){
  print $name.' '.$value.' <input name="'.$name.'" id="quantity'.$i.'" type="text" value="1" onkeyup="UpdateTotal()" /><br />
';
  $i++;
}
echo $totalcost;

 

Hello everyone,

 

I'm working on a basic checkout page created in php, I get a list of products from a list products selected with checkboxes from another page, it submits the product title and price via POST. I am using a PHP FOR loop to print all of the names and prices out, what I want to do is have a quantity input text field where the user can type in the number of products they want to order then on onkeyup have it auto update the price via javascript... I have been playing with a lot of javascript over the past 12 hours and have not really been able to get any of it to work with my current project and now I am way sleepy just about to give up *sigh*... I am open to any and all ideas...

 

This is the php code that I'm using:

$i=0;
foreach($_POST as $name => $value){
  print $name.' '.$value.' <input name="'.$name.'" id="quantity'.$i.'" type="text" value="1" onkeyup="UpdateTotal()" /><br />
';
  $i++;
}
echo $totalcost;

 

 

Need to know a couple of things:

 

1. Where, in the HTML, are the item prices located?  Can't update a price without being able to grab the price of one product unit.

 

2. Which element will display the updated price?

the product's name is coming from $name and the price is coming from $value, and right now i do not have a holder for a subtotal, I am guessing it could on in to something like:

<input name="subtotal" id="subtotal" value="" />

 

I am not really that good with javascript... that why I would like to get everyone's input in here about the best way to go about this, with my lack of javascript skills.

the product's name is coming from $name and the price is coming from $value, and right now i do not have a holder for a subtotal, I am guessing it could on in to something like:

<input name="subtotal" id="subtotal" value="" />

 

I am not really that good with javascript... that why I would like to get everyone's input in here about the best way to go about this, with my lack of javascript skills.

 

I need to know how to grab the prices from the actual HTML.  Knowing the PHP variables that they inhabit isn't useful, due to the way a web page is constructed.  It goes:

 

Backend processing (PHP) -> rendered page (HTML) -> dynamic behavior (JavaScript)

 

So, what is stored in $price, exactly?  Is it merely text?  Are there any HTML elements that could hold the price, like a hidden input or something?

right now it's just text, but what I could do is like to pointed out I could make a hidden field and have the text posted to that fields value.

 

So I'm guessing it could look something like this, I'm not sure what do you think?

<?PHP
foreach($_POST as $name => $value){
  print $name.' '.$value.' <input type="hidden" name="price" value="'.$value.'" /> <input name="'.$i.'" type="text" value="1" onkeyup="UpdateTotal()" />';
$i++}

echo '<input name="subtotal" id="subtotal" value="" />';
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.