User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 455,973 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,819 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 3741 | Replies: 11
Reply
Join Date: Nov 2007
Posts: 6
Reputation: microtribe is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
microtribe microtribe is offline Offline
Newbie Poster

Help Move Javascript Variable to PHP

  #1  
Nov 23rd, 2007
Hi,

I have a Javascript total calculation function within a php form that uses onBlur to show the client a running total of the dollar amount of items added: http://www.microtribe.com/dev4Tony/order881x2.php

And I have a linked external PHP file/script to email the results of the form to me. All works fine but I can't figure out how to grab the total dollar amount ("sub_total") created by the Javascript with this line:
document.getElementById('sub_total').innerHTML = '$ ' + runningTotal.toFixed(2);

Is there a way to have the Javascript send this variable to my php script?

Here is the full Javascript Function:
<code>

/*Addition Function
*/
var elements = new Array();

function calculatePrice(price,me,id) {
var newValue = 0;
if (me.value == 0) {
document.getElementById('total' + id).innerHTML = '';
} else if (me.value > 0) {
newValue = price * me.value;
document.getElementById('total' + id).innerHTML = newValue.toFixed(2);
}

elements['total' + id] = newValue.toFixed(2);

calculateTotal();

}

function calculateTotal() {
var runningTotal = 0;
for (var strCurrentKey in elements) {
runningTotal += parseFloat(elements[strCurrentKey]);
}

var sub = runningTotal.toFixed(2);
var tax = sub * 0.00;
var total = sub * 1.08375;

document.getElementById('sub_total').innerHTML = '$ ' + runningTotal.toFixed(2);
document.getElementById('tax').innerHTML = '$ ' + tax.toFixed(2);
document.getElementById('grand_total').innerHTML = '$ ' + total.toFixed(2);

document.getElementById('field_subtotal').value = runningTotal.toFixed(2);
document.getElementById('field_tax').value = tax.toFixed(2);
document.getElementById('field_total').value = total.toFixed(2);

}


/*Parse number to currency format:
By Website Abstraction (www.wsabstract.com)
and Java-scripts.net (www.java-scripts.net)
*/

//Remove the $ sign if you wish the parse number to NOT include it
var wd;
function parseelement(tempnum){
wd="w";
for (i=0;i<tempnum.length;i++){
if (tempnum.charAt(i)=="."){
wd="d";
break;
}
}
if (wd=="w") {
return tempnum+".00";
} else {
if (tempnum.charAt(tempnum.length-2)==".") {
return tempnum+"0";
} else {
tempnum=Math.round(tempnum*100)/100;
return tempnum;
}
}
}
</code>
Thanks!
Tony
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 636
Reputation: kkeith29 is on a distinguished road 
Rep Power: 3
Solved Threads: 71
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Practically a Master Poster

Re: Move Javascript Variable to PHP

  #2  
Nov 23rd, 2007
you can either use AJAX or have javascript refresh the page with the dollar amount in the url.
Reply With Quote  
Join Date: Nov 2007
Posts: 6
Reputation: microtribe is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
microtribe microtribe is offline Offline
Newbie Poster

Re: Move Javascript Variable to PHP

  #3  
Nov 23rd, 2007
Originally Posted by kkeith29 View Post
you can either use AJAX or have javascript refresh the page with the dollar amount in the url.


Thanks for the reply, but I'm looking for some help. I'm aware that I can do it using AJAX, but I don't know how. Having Javascript refresh the page doesn't soung like it's going to help me get the variable into the email I'm sending from my php script.

Thanks,

t.
Reply With Quote  
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 636
Reputation: kkeith29 is on a distinguished road 
Rep Power: 3
Solved Threads: 71
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Practically a Master Poster

Re: Move Javascript Variable to PHP

  #4  
Nov 23rd, 2007
sorry i didn't go into enough detail. when you put the dollar amount in the url, php can access it through the $_GET method. i will make some code for you and post it soon.
Reply With Quote  
Join Date: Nov 2007
Posts: 6
Reputation: microtribe is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
microtribe microtribe is offline Offline
Newbie Poster

Re: Move Javascript Variable to PHP

  #5  
Nov 23rd, 2007
Thanks.

In js script:
document.getElementById('subtotal').value = '$ ' + runningTotal.toFixed(2);
On HTML form:
<input type="hidden" id="subtotal" name="subtotal" value="">
In php script:
$sub_total = $_POST['subtotal'];

Didn't work...
Last edited by microtribe : Nov 23rd, 2007 at 7:59 pm.
Reply With Quote  
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 636
Reputation: kkeith29 is on a distinguished road 
Rep Power: 3
Solved Threads: 71
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Practically a Master Poster

Re: Move Javascript Variable to PHP

  #6  
Nov 23rd, 2007
here is the ajax code you need:

<script type="text/javascript">
function Send() {
function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}
xmlHttp=GetXmlHttpObject();
xmlHttp.onreadystatechange=changeofState;

//change the page below to you php page name

var url="something.php";

//change 'variable' to the name of the javascript variable that contains the dollar amount

url = url+"?amt="+variable;
url = url+"&sid"+Math.random();
xmlHttp.open("GET",url,true);
xmlHttp.send(null);  
function changeofState()
{
if(xmlHttp.readyState==4)
{
document.write('ok');
}
}
}
</script>
Last edited by kkeith29 : Nov 23rd, 2007 at 9:08 pm.
Reply With Quote  
Join Date: Nov 2007
Posts: 6
Reputation: microtribe is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
microtribe microtribe is offline Offline
Newbie Poster

Re: Move Javascript Variable to PHP

  #7  
Nov 24th, 2007
Thanks. That looked great, and it's over my head at this point, but it didn't work out...I don't understand so I can't ask any intelligent questions. I did replace the url and variable name, but it didn't work.

You know, I may be able to figure this out if I can understand how the Javascript gets the total amount to the div tag with this piece of code:
document.getElementById('sub_total').innerHTML = '$ ' + runningTotal.toFixed(2);

It would make sense from the code naming that it is "getting" something, not "putting" something.

But the amount shows up on the page through the div tag:
<div class="price" id="sub_total" name="sub_total"> $0.00</div>

However, I can't seem to get that same total into a hidden field. So that if I have a hidden field, and use the code referenced a couple posts above, I can get a fixed number from the hidden field to my php form, so that part works, but I can't get the Javascript total into the hidden field. If I could just do that, it would work...

Thanks a lot for your help. I appreciate the script.

Tony
Reply With Quote  
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 636
Reputation: kkeith29 is on a distinguished road 
Rep Power: 3
Solved Threads: 71
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Practically a Master Poster

Re: Move Javascript Variable to PHP

  #8  
Nov 24th, 2007
oh ok i see what you are trying to do. I am going about it all wrong. you are sending the sub total value along with the form via a hidden field. i guess i didn't read into your problem very well.

this should do it. it worked for me:

HTML
<input type="hidden" id="subtotal" name="subtotal" value="">

JAVASCRIPT
// you need to put the name of your form in place of 'formname' below
document.formname.subtotal.value = '$ '+runningTotal.toFixed(2);
Last edited by kkeith29 : Nov 24th, 2007 at 9:37 pm.
Reply With Quote  
Join Date: Nov 2007
Posts: 6
Reputation: microtribe is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
microtribe microtribe is offline Offline
Newbie Poster

Re: Move Javascript Variable to PHP

  #9  
Nov 25th, 2007
Unbelievably, this didn't work.

I have -
external.js:
document.order_guide.subtotal.value = '$ '+runningTotal.toFixed(2);
php/html form:
<form enctype="multipart/form-data" name="order_guide" action="sendorder.php" method="post">

and

<input name="subtotal" type="hidden" id="subtotal" value="">

???
Reply With Quote  
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 636
Reputation: kkeith29 is on a distinguished road 
Rep Power: 3
Solved Threads: 71
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Practically a Master Poster

Re: Move Javascript Variable to PHP

  #10  
Nov 25th, 2007
post your html. ill look at it and find the problem
Last edited by kkeith29 : Nov 25th, 2007 at 12:40 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 9:14 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC