/    Sign up×
Community /Pin to ProfileBookmark

total update w/o refresh

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
[url]http://www.site.com/page.php?itemid=1&price=15[/url]
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?

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@gooncorpApr 10.2007 — edit: sorry, forget that.
Copy linkTweet thisAlerts:
@konithomimoApr 11.2007 — you can use PHP to get the value via $_GET['price'] and then just echo it to a JS variable, or you can use JS to get the query string via document.location.href.split('?')[1], and then loop through the values for price and get the value of it.

With PHP:

var price = <? $p = $_GET['price']; echo $p; ?>;


With just JS:

<i>
</i>var q = document.location.href.split('?')[1];
var sps = q.split('&amp;');
for(var i=0;i&lt;sps.length;i++)
{
sp = sps[i].split('=');
if(sp[0]=='price')
{
var price = sp[1];
break;
}
}


Now in both cases the variable price holds the value from the querystring. Of course, you can use ASP instead of PHP.
Copy linkTweet thisAlerts:
@bhavin_85authorApr 11.2007 — ive written this

[CODE]
<script>
function setTotal()
{
var x=document.getElementById("quantity");
var y=document.getElementById("price");
var z = x*y;
document.getElementById("total").value = z;
}
</script>
[/CODE]


and these are my forms
[code=php]
<input name="price" id="price" type="text" value="<? echo $price; ?>" readonly="readonly"></input>
<input name="quantity" id="quantity" type="text" value="1" onChange="setTotal()"></input>
<input name="total" id="total" type="text" readonly="readonly" value=""></input>
[/code]


the only problem with this is instead of puttin the total in the total field its writting "NaN"

any ideas?
Copy linkTweet thisAlerts:
@konithomimoApr 11.2007 — If you are trying to get the price value from the querystring then this:

&lt;input name="price" id="price" type="text" value="&lt;? echo $price; ?&gt;" readonly="readonly"&gt;&lt;/input&gt;

wont work, since you don't have a php variable named price.
Copy linkTweet thisAlerts:
@konithomimoApr 11.2007 — Instead change it to:

&lt;input name="price" id="price" type="text" value="&lt;? echo $_GET['price']; ?&gt;" readonly="readonly"&gt;&lt;/input&gt;
Copy linkTweet thisAlerts:
@bhavin_85authorApr 11.2007 — sorry i forgot 2 mention

i have taken the value from the querystring and turned it into a variabe:

$price = $_GET['price'];
Copy linkTweet thisAlerts:
@konithomimoApr 11.2007 — and what does your source code show the value as?
Copy linkTweet thisAlerts:
@bhavin_85authorApr 11.2007 — its showing the right value, it shows 15.30 in the text box and its showing 15.30 in the source code,

however my source code is showing 1 as the value for quantity even thought ive put 2 in the text box,
[CODE]<input name="quantity" id="quantity" type="text" value="1" onChange="setTotal()"></input>[/CODE]

how do i set the default to 1? i guess value just make the values a constant 1?
×

Success!

Help @bhavin_85 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...