Jump to content

[SOLVED] Update Order Total w/o refresh


bhavin_85

Recommended Posts

hey guys/gals

 

I have create an order form in php and i need to write some javascript that will automatically update the total without refreshing the page.

 

the price is carried over to the page by the url

http://www.site.com/page.php?id=1&price=15

as shown above.

 

there is only 1 item in each page so what i need the page to do is just update the total field when the user enters the quantity that they want

 

ive found examples online but they all have a fixed price nothing dynamic like this

 

any ideas?

 

Link to comment
Share on other sites

have a button they push when they have entered the quantity required which runs this function

function setTotal()
{
var x=document.getElementById("quantity");
var y=document.getElementById("price");
var z = x*y;
document.getElementById("total").value = z;
}

Link to comment
Share on other sites

ok im sorry but i am sooo shit with javascript

 

could u  please tell me how to do it...ive attached my code

<?
session_start();
include('config.php');
$username = $_SESSION['username'];
$filmid = $_GET['itemid'];
$price = $_GET['price'];
?>
<html>
<head>
<title>Online DVD Shop</title>
<script>
function setTotal()
{
var x=document.getElementById("quantity");
var y=document.getElementById("price");
var z = x*y;
document.getElementById("total").value = z;
}
</script>
</head>
<body>
<table align="center" valign="top" width="700" cols="3">
<tr>
<td align="center" colspan="3">
<font size="+5">Online DVD Shop</font>
</td></tr>
<tr>
<td align="left" valign="top" colspan="3">
<?
$sql="SELECT customerid FROM regcustomer where username='$username'";
$query=mysql_query($sql);
$row=mysql_fetch_assoc($query);
$customerid=$row['customerid'];
$sql2="SELECT name FROM customer WHERE customerid='$customerid'";
$query2=mysql_query($sql2);
$row2=mysql_fetch_assoc($query2);
$name=$row2['name'];
if ( empty($_SESSION['username'])){
echo "<a href='login.php'>Login In or Register</a>";
}
else {
echo "Welcome to our online shop $name";
echo "<a href='logout.php'>Logout</a>";
}
?>
</td></tr>
<tr>
<td align="center"><a href="default.php">Home</a></td>
<td align="center"><a href="search.php">Search</a></td>
<td align="center"><a href="registration.php">Register</a></td>
</tr>
<tr>
<td colspan="4" align="center"><font size="+2">Purchase Confirmation</font></td>
</tr>
<tr>
<td><b>Title</b></td>
<td><b>Price</b></td>
<td><b>Quantity</b></td>
<td><b>Total</b></td>
</tr>
<form name="order" method="post" onSubmit="">
<tr>
<td>
<? 
$sql3 = "SELECT * FROM film WHERE filmid='$filmid'";
$query3 = mysql_query($sql3) or die(mysql_error());
$row3 = mysql_fetch_assoc($query3);
$title = $row3['title'];

echo $title;
?>
</td>
<td>
<input name="price" type="text" value="<? echo $price; ?>" readonly="readonly"></input>
</td>
<td>
<input name="" type="text" value="1" onChange=""></input>
</td>
<td>
<input name="total" type="text" readonly="readonly" value=""></input>
</td>
</tr>
<tr>
<td>
<input type="button" value="Update" onClick="return setTotal()"></input>
</td>
</tr>
</form>
</table>
</body>
</html>

 

thank you your help is really appreciated

Link to comment
Share on other sites

add ids to your input fields

<input name="price" id="price" type="text" value="<? echo $price; ?>" readonly="readonly">

<input name="quantity" id="quantity" type="text" value="1" onChange="setTotal()">

<input name="total" id="total" type="text" readonly="readonly" value="">

and this should do it

Link to comment
Share on other sites

<input name="total" id="total" type="text" readonly="readonly" value="<? echo $price; ?>">

 

as the quantity defaults to 1 then the default total should also be the same as the price of one. I have included that in the above line

 

change the function to this one

function setTotal()
{
var x=Number(document.getElementById("quantity"));
var y=Number(document.getElementById("price"));
var z = x*y;
var ans = z.toFixed(2);
document.getElementById("total").value = ans;
}

 

I have cast the values of quantity and price as numbers, NaN means that one or both of the variables you are trying to do maths with is Not a Number. I have also added the line to format the number in the form of 2 decimal places (23.45)

Link to comment
Share on other sites

hi paul

 

ive made the changes that you suggested, but it is still showing  NaN in the total field  ???

 

script

<script>
function setTotal()
{
var x=Number(document.getElementById("quantity"));
var y=Number(document.getElementById("price"));
var z = x*y;
var ans = z.toFixed(2);
document.getElementById("total").value = ans;
}
</script>

 

form

<form name="order" method="post" onSubmit="">
<input name="price" id="price" type="text" value="<? echo $price; ?>" readonly="readonly"></input>
</td>
<td>
<input name="quantity" id="quantity" type="text" value="1" onChange="setTotal()"></input>
</td>
<td>
<input name="total" id="total" type="text" readonly="readonly" value="<? echo $price; ?>"></input>
</form>

Link to comment
Share on other sites

do me a favour and use this version of the function, it will put up an alert box for each of the values, you will find out if any of them is throwing the problem as they should all be numbers

 

<script>
function setTotal()
{
var x=Number(document.getElementById("quantity"));
alert("quantity = " + x);
var y=Number(document.getElementById("price"));
alert("price = " + y);
var z = x*y;
var ans = z.toFixed(2);
alert("total = " + ans);
document.getElementById("total").value = ans;
}
</script>

Link to comment
Share on other sites

 

I think I may have made a mistake and missed out the value part try this one anyway please

<script>
function setTotal()
{
var x=Number(document.getElementById("quantity").value);
alert("quantity = " + x);
var y=Number(document.getElementById("price").value);
alert("price = " + y);
var z = x*y;
var ans = z.toFixed(2);
alert("total = " + ans);
document.getElementById("total").value = ans;
}
</script>

 

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.