PDA

View Full Version : If statement ( variable )



LightBulb
08-28-2012, 03:18 PM
I need to create an "if statement" for the below result of a javascript calculation, however the "var ansD" variable won`t let me, any help would be appreciated,

It is the below line in question, i have seperated it out in the script for easier viewing,


var ansD = document.getElementById("result_price");
ansD.value = '' + Math.round ((val1 * val2 * val3*100/5000/100 + 15.00 * 1.50 +(val4 * 0.68))*(valq1))


<script type="text/javascript">
var btn = document.getElementById('calculate');

btn.onclick = function()
{

// get the input values
var val1 = parseInt(document.getElementById('val1').value);
var val2 = parseInt(document.getElementById('val2').value);
var val3 = parseInt(document.getElementById('val3').value);

var val4 = parseInt(document.getElementById('val4').value);

var valq1 = parseInt(document.getElementById('valq1').value);

var valq1a = parseInt(document.getElementById('valq1a').value);

var val1a = parseInt(document.getElementById('val1a').value);
var val2a = parseInt(document.getElementById('val2a').value);
var val3a = parseInt(document.getElementById('val3a').value);

var val4a = parseInt(document.getElementById('val4a').value);


var ansD = document.getElementById("answer");
ansD.value = (val4 + val4a) * (valq1);

var ansD = document.getElementById("answer2");
ansD.value = '' + Math.round((valq1) * val1 * val2 * val3*100/5000/100 + val1a * val2a * val3a * 100/5000/100);
var ansD = document.getElementById("answer3");
ansD.value = '' + Math.round((valq1) * val1 * val2 * val3*100/1000000/100 + val1a * val2a * val3a*100/1000000/100);
var ansD = document.getElementById("answer4");
ansD.value = '' + Math.round((valq1) * val1 * val2 * val3*100/1000000*35.31/100 + val1a * val2a * val3a*100/1000000*35.31/100);

var ansD = document.getElementById("result_price");
ansD.value = '' + Math.round ((val1 * val2 * val3*100/5000/100 + 15.00 * 1.50 +(val4 * 0.68))*(valq1))

var ansD = document.getElementById("result_pricea");
ansD.value = '' + Math.round ((val1a * val2a * val3a*100/5000/100 + 15.00 *1.50 +(val4a * 0.68))*(valq1a))
var result_price = parseInt(document.getElementById("result_price").value);
var result_pricea = parseInt(document.getElementById("result_pricea").value);
var ansD = document.getElementById("answer5");
ansD.value =result_price + result_pricea;

// get the elements to hold the results

var result1a = document.getElementById('result1a')
var result1 = document.getElementById('result1')
var result2 = document.getElementById('result2');

var result3 = document.getElementById('result3');

var tot_result3 = document.getElementById('tot_result3');

var result1a = document.getElementById('result1a')
var result2a = document.getElementById('result2a');

var result3a = document.getElementById('result3a');

var tot_result3 = document.getElementById('tot_result3');

var val1a = parseInt(document.getElementById('val1a').value);
var val2a = parseInt(document.getElementById('val2a').value);
var val3a = parseInt(document.getElementById('val3a').value);

var val4a = parseInt(document.getElementById('val4a').value);

// get the elements to hold the results

// create an empty array to hold error messages
var msg = [];
// check each input value, and add an error message
// to the array if it's not a number
if (isNaN(val1)) {
msg.push('Height is not a number');
}
if (isNaN(val2)) {
msg.push('Width is not a number');
}
if (isNaN(val3)) {
msg.push('Length is not a number');
} if (isNaN(val1a)) {
msg.push('Height is not a number');
}
if (isNaN(val2a)) {
msg.push('Width is not a number');
}
if (isNaN(val3a)) {
msg.push('Length is not a number');
}
// if the array contains any values, display the error message(s)
// as a comma-separated string in the first <span> element

if (msg.length > 0) {
result1.innerHTML = msg.join(', ');
} else if (msg.length > 0) {
result1a.innerHTML = msg.join(', ');
} else {
// otherwise display the results in the <span> elements
result1.innerHTML = '' + Math.round((val1 * val2 * val3*100)/5000)/100;
result1a.innerHTML = '' + Math.round((val1a * val2a * val3a*100)/5000)/100;
result5.innerHTML = '' + Math.round((val1 * val2 * val3*100)/1000000)/100;
result5a.innerHTML = '' + Math.round((val1a * val2a * val3a*100)/1000000)/100;
result6.innerHTML = '' + Math.round((val1 * val2 * val3*100)/1000000*35.31)/100;
result6a.innerHTML = '' + Math.round((val1a * val2a * val3a*100)/1000000*35.31)/100;
//START TEST LINE
//END TEST LINE

// calculations for first line on form
{
var x="";
if (+ val1 * val2 * val3/ 5000 <70)
{
x="OK";
}
else
{
x="X";
}
document.getElementById("result_volweight").innerHTML=x;
}
result2.innerHTML = '' + (2*val1 + 2*val2 + val3);
{
var x="";
if (2*val1 + 2*val2 + val3 <419)
{
x="OK";
}
else
{
x="X";
}
document.getElementById("result_girth").innerHTML=x;
}
result3.innerHTML = '' + (val4);
{
var x="";
if (val4 <70)
{
x="OK";
}
else
{
x="X";
}
document.getElementById("result_actual_weight").innerHTML=x;
}
if (+ val1 * val2 * val3/ 5000 >69)
{
alert("Line 1 - The Volumetric Weight Exceeds The Limits For Standard Parcel Delivery - Please Contact Us For Alternative Delivery Options");
return false;
}
if (val4 >69)
{
alert("Line 1 - The Actual Weight Exceeds The Limits For Standard Parcel Delivery - Please Contact Us For Alternative Delivery Options");
return false;
}
else if (2*val1 + 2*val2 + val3 >419)
{
alert("Line 1 - The Girth + Length Exceeds The Limits For Standard Parcel Delivery - Please Contact Us For Alternative Delivery Options");
return false;
}
else if (val3>270)
{
alert("Line 1 - Length Exceeds The Limits For Standard Parcel Delivery - Please Contact Us For Alternative Delivery Options");
return false;
}
//1st line of form end

//calculations for 2nd line of form
{
var x="";
if (+ val1a * val2a * val3a/ 5000 <70)
{
x="OK";
}
else
{
x="X";
}
document.getElementById("result_volweighta").innerHTML=x;
}
result2a.innerHTML = '' + (2*val1a + 2*val2a + val3a);
{
var x="";
if (2*val1a + 2*val2a + val3a <419)
{
x="OK";
}
else
{
x="X";
}
document.getElementById("result_girtha").innerHTML=x;
}
result3a.innerHTML = '' + (val4a);
{
var x="";
if (val4a<70)
{
x="OK";
}
else
{
x="X";
}
document.getElementById("result_actual_weighta").innerHTML=x;
}
if (+ val1a * val2a * val3a/ 5000 >69)
{
alert("Line 2 - The Volumetric Weight Exceeds The Limits For Standard Parcel Delivery - Please Contact Us For Alternative Delivery Options");
return false;
}
if (val4a >69)
{
alert("Line 2 - The Actual Weight Exceeds The Limits For Standard Parcel Delivery - Please Contact Us For Alternative Delivery Options");
return false;
}
else if (2*val1a + 2*val2a + val3a >419)
{
alert("Line 2 - The Girth + Length Exceeds The Limits For Standard Parcel Delivery - Please Contact Us For Alternative Delivery Options");
return false;
}
else if (val3a>270)
{
alert("Line 2 - Length Exceeds The Limits For Standard Parcel Delivery - Please Contact Us For Alternative Delivery Options");
return false;
}
//2nd line of form end
}
};

</script>

Any help would be much appreciated,

Regards,
LightBulb

jscheuer1
08-28-2012, 03:49 PM
It would be a good idea to use a separate variable name for each element that you want to affect. This of course would mean that you would have to change its name in any subsequent code that references it. Unless document.getElementById("result_price"); is a form element, changing its value will do nothing. If it's a span, div, etc., use:


var ansD = document.getElementById("result_price");
ansD.innerHTML = '' + Math.round ((val1 * val2 * val3*100/5000/100 + 15.00 * 1.50 +(val4 * 0.68))*(valq1))

If document.getElementById("result_price"); doesn't exist or hasn't been parsed yet by the browser. The code will create an error and/or do nothing.

Diagnosing this without seeing the page is difficult.

If you want more help, please include a link to the page on your site that contains the problematic code so we can check it out.

LightBulb
08-28-2012, 04:09 PM
Hi Thank you for your reply,
This is the link http://www.norspeed.com/NEW-TEST-WITH-LINED-FORM-NEW-8.html The elemement already exists, this element allows me to calculate the results from the form and to send via a php file to an e-mail address,

I can set up an "if statement" for it if i configuire it for a <span>tag, but as it needs to calculate and then send the calculation to the email i have to configuire it for an<input> tag which is where the problem lies,

I need to code an "if statement" to stop the price being shown in the price box if the size or weights exceed the shipping parameters,

Of course if it is possible to get the <span> tag as opposed to the <input>tag to send the result to the e-mail it would be even better as i can do an "if statement" for that without a problem,

Any help would be much appreciated,

Regards,
LightBulb[/QUOTE]

jscheuer1
08-28-2012, 05:21 PM
That actually seems to be working, it's:


var ansD = document.getElementById("result_pricea");

That's a problem, there are too many of them. Only one element per page may have a given id.

Also, you should get rid of this:


<script src="zz-jscode.js" type="text/javascript"></script>

It's throwing an error because it comes before the elements in it are parsed.

LightBulb
08-28-2012, 05:43 PM
Hi,
Thank you for your reply,
The var ansD = document.getElementById("result_pricea"); is actually the next row down and not a duplicate,

The "if statement" is working for the alerts but i need it to not show a price in the price colum if the weight / size parameters are exceeded, as well as the alert,

Of course if it is possible to get the <span> tag as opposed to the <input>tag to send the result to the e-mail it would be even better as i can do an "if statement" for that without a problem,

Regards,
LightBulb,





That actually seems to be working, it's:


var ansD = document.getElementById("result_pricea");

That's a problem, there are too many of them. Only one element per page may have a given id.

Also, you should get rid of this:


<script src="zz-jscode.js" type="text/javascript"></script>

It's throwing an error because it comes before the elements in it are parsed.

jscheuer1
08-29-2012, 02:22 PM
I never said it wasn't and there still are too many elements on the page with an id of result_pricea. There can be only one.

However, you may be right, that might not have anything to do with the if problem. If it works for span, it can work for input. A solution would be to make it work for span again and solve the problem of it not getting into the email by making a hidden input:


<input type="hidden" name="result_price">

and simply populate its value property with the same data you are putting into the visible span. You could have only one of these and it would be accessible via javascript as:


document.getElementsByName('result_price')[0];

It will show up in POST (or GET if you submit via get, I think you're using post) as a string:


$_POST['result_price']

You could have more than one of these on a page if you made them with brackets:


<input type="hidden" name="[result_price]">

They would be submitted as an array to PHP as either POST or GET (I think you're doing POST, so):


$_POST['result_price']

And could be accessed via javascript on the page as:


var result_prices = document.getElementsByName('[result_price]');

which would be a nodeList, example usage:


result_prices[0].value = somevalue;
result_prices[1].value = someothervalue;
result_prices[2].value = yetsomeothervalue;

If the span code is working for the if stuff use it for that, while also setting the hidden field(s) to the same value for use with the PHP that sends the email.

On the other hand, if you just want to solve this if problem with the code otherwise as close as possible to its current form, please tell me step by step what actions I must take on the page to get the error.

LightBulb
09-05-2012, 03:27 PM
Now Sorted

LightBulb
09-05-2012, 03:28 PM
Now sorted