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;

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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="" />';
?>

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.