PDA

View Full Version : add javascript answer to two fields



ianhaney
09-02-2016, 08:54 AM
Hi

I have created a cubic feet calculator and it outputs the answer in a textarea box when the calculate button is pressed, the client want the javascript output to appear in the input form field below as well so the user does not have to enter it in, is that possible?

the Javascript coding I have is below



<script language="JavaScript">

function calc() {
var aa = document.boxer.lsofa.value * 40.77;
var bb = document.boxer.ssofa.value * 29.26;
var cc = document.boxer.achair.value * 17.81;
var dd = document.boxer.rchair.value * 20.78;
var ee = document.boxer.desk.value * 13.52;
var ff = document.boxer.ldesk.value * 40;
var gg = document.boxer.bookc.value * 16;
var hh = document.boxer.dcab.value * 72;
var a1 = document.boxer.ctable.value * 13.75;
var jj = document.boxer.ntables.value * 5;
var kk = document.boxer.dltable.value * 15.75;
var ll = document.boxer.mrack.value * 2.49;
var mm = document.boxer.tseat.value * 9;
var nn = document.boxer.lamp.value * 5.79;
var oo = document.boxer.fplace.value * 1.56;
var pp = document.boxer.fpequip.value * 1.19;
var qq = document.boxer.mcentre.value * 1.3;
var rr = document.boxer.video.value * 0.9;
var ss = document.boxer.mhifi.value * 1.8;
var tt = document.boxer.midihifi.value * 9;
var uu = document.boxer.porttv.value * 1.5;
var vv = document.boxer.lartv.value * 8.0;
var ww = document.boxer.tvstand.value * 5.0;
var xx = document.boxer.tvcab.value * 12.0;
var yy = document.boxer.dehud.value * 6.75;
var zz = document.boxer.vacuum.value * 4.84;
var b1 = document.boxer.polish.value * 2.34;
var c1 = document.boxer.frz10.value * 15.62;
var d1 = document.boxer.frz15.value * 30.18;
var e1 = document.boxer.frzup.value * 44.48;
var f1 = document.boxer.frzchst.value * 33.31;
var j1 = document.boxer.cookr.value * 22.25;
var k1 = document.boxer.dryer.value * 18.19;
var l1 = document.boxer.wmach.value * 18.19;
var m1 = document.boxer.dshwsh.value * 18.19;
var n1 = document.boxer.sew.value * 1.07;
var o1 = document.boxer.mwave.value * 2.31;
var p1 = document.boxer.pcmon.value * 18.5;
var q1 = document.boxer.tchairl.value * 45.71;
var r1 = document.boxer.tchairs.value * 34.5;
var s1 = document.boxer.sbrd.value * 23.7;
var t1 = document.boxer.sbdress.value * 39.37;
var u1 = document.boxer.cnrcab.value * 33.29;
var v1 = document.boxer.foodt.value * 7.99;
var w1 = document.boxer.irnbrd.value * 1.41;
var x1 = document.boxer.utiltab.value * 3.33;
var y1 = document.boxer.utilcab.value * 15.56;
var z1 = document.boxer.vegbin.value * 1.29;
var a2 = document.boxer.swbin.value * 2.09;
var b2 = document.boxer.pedbin.value * 1.85;
var c2 = document.boxer.vanseat.value * 4.3;
var d2 = document.boxer.drstable.value * 34.25;
var e2 = document.boxer.smllchair.value * 9.64;
var f2 = document.boxer.sinbed.value * 36;
var g2 = document.boxer.dblbed.value * 67.5;
var h2 = document.boxer.kngbed.value * 90;
var j2 = document.boxer.bedtab.value * 5.37;
var k2 = document.boxer.twodrwr.value * 6.67;
var l2 = document.boxer.thrdrwr.value * 9.99;
var m2 = document.boxer.fordrwr.value * 13.33;
var n2 = document.boxer.srobe.value * 24;
var o2 = document.boxer.drobe.value * 60;
var p2 = document.boxer.chest.value * 8.1;
var q2 = document.boxer.blnkt.value * 7.88;
var r2 = document.boxer.bbath.value * 1.47;
var s2 = document.boxer.cchair.value * 2.2;
var t2 = document.boxer.hchair.value * 12.85;
var u2 = document.boxer.cot.value * 36;
var v2 = document.boxer.ppen.value * 2.03;
var w2 = document.boxer.change.value * 10.31;
var x2 = document.boxer.moses.value * 15.67;
var y2 = document.boxer.bird.value * 10.67;
var z2 = document.boxer.hose.value * 0.8;
var a3 = document.boxer.pahr.value * 7.5;
var b3 = document.boxer.pattbl.value * 10;
var c3 = document.boxer.brolly.value * 0.91;
var d3 = document.boxer.gldr.value * 36.1;
var e3 = document.boxer.barrow.value * 21.75;
var f3 = document.boxer.bbq.value * 2.25;
var g3 = document.boxer.stplddr.value * 6;
var h3 = document.boxer.ladder.value * 11.2;
var j3 = document.boxer.smbox.value * 1.875;
var k3 = document.boxer.mebox.value * 3.375;
var l3 = document.boxer.labox.value * 4.5;
var m3 = document.boxer.xlbox.value * 8;
var p3 = document.boxer.golf.value * 2.07;
var q3 = document.boxer.eheat.value * .75;
var r3 = document.boxer.gfire.value * 7;
var s3 = document.boxer.radiat.value * 3;
var t3 = document.boxer.zbed.value * 8;
var u3 = document.boxer.sled.value * 6;
var v3 = document.boxer.wbench.value * 13.5;
var w3 = document.boxer.motor.value * 4.88;
var x3 = document.boxer.bsaw.value * 29.93;
var y3 = document.boxer.tywrtr.value * 0.55;
var z3 = document.boxer.wghts.value * 10;
var a4 = document.boxer.tmill.value * 29;
var b4 = document.boxer.exbike.value * 23.7;
var c4 = document.boxer.stppr.value * 12;
var d4 = document.boxer.wgtbnh.value * 24;
var e4 = document.boxer.rmchn.value * 29;
var f4 = document.boxer.nechr.value * 6.64;
var g4 = document.boxer.dbin.value * 3.43;
var h4 = document.boxer.bskt.value * 3.43;
var j4 = document.boxer.chldbik.value * 20;
var k4 = document.boxer.adltbik.value * 36;
var l4 = document.boxer.pram.value * 16.71;
var m4 = document.boxer.fcab2.value * 4.16;
var n4 = document.boxer.fcab3.value * 6.23;
var o4 = document.boxer.fcab4.value * 11.31;
var p4 = document.boxer.trvlcot.value * 32;
var q4 = document.boxer.fan.value * 1.29;
var r4 = document.boxer.piano.value * 41.67;
var s4 = document.boxer.poolt.value * 72;
var t4 = document.boxer.pptab.value * 100;
var u4 = document.boxer.toolbx.value * 10.97;
var v4 = document.boxer.rotmow.value * 27;


cuft = (aa+bb+cc+dd+ee+ff+gg+hh+a1+jj+kk+ll+mm+nn+oo+pp+qq+rr+ss+tt+uu+vv+ww+xx+yy+zz+b1+c1+d1+e1+f1+j1+k1+l1+m1+n1+o1+p1+q1+r1+s1+t1+u1+v1+w1+x1+y1+z1+a2+b2+c2+d2+e2+ f2+g2+h2+j2+k2+l2+m2+n2+o2+p2+q2+r2+s2+t2+u2+v2+w2+x2+y2+z2);
cuft = cuft+(a3+b3+c3+d3+e3+f3+g3+h3+j3+k3+l3+m3+p3+q3+r3+s3+t3+u3+v3+w3+x3+y3+z3+a4+b4+c4+d4+e4+f4+g4+h4+j4+k4+l4+m4+n4+o4+p4+q4+r4+s4+t4+u4+v4);
cuft = parseInt(cuft);
tot = cuft;
if (tot<1)
{
var rslt = "Please enter relevant quantities in the form above.";
}
else
{
var rslt = "Thank you. We calculate your belongings will require " + cuft.toString() + " cubic feet of space.";
}
document.boxer.answer.value = rslt
}
// End -->

</script>


The form coding is below



<form class="submitphoto_form" id="contact-form" action="mail.php">

<input type="text" class="wp-form-control wpcf7-text" id="name" name="name" placeholder="Your name">
<div class="error left-align" id="err-name">Please enter your name.</div>

<input type="mail" class="wp-form-control wpcf7-email" id="email" name="email" placeholder="Email address">
<div class="error left-align" id="err-email">Please enter a valid email adress.</div>

<input type="text" class="wp-form-control wpcf7-text" id="phone" name="phone" placeholder="Phone Number">
<div class="error left-align" id="err-phone">Please enter a phone number.</div>

<input type="text" class="wp-form-control wpcf7-text" id="cubicfeet" name="cubicfeet" placeholder="Cubic Feet">
<div class="error left-align" id="err-cubicfeet">Please enter the cubic feet.</div>

<input type="text" class="wp-form-control wpcf7-text" id="colpostcode" name="colpostcode" placeholder="Collection Postcode">
<div class="error left-align" id="err-colpostcode">Please enter the collection postcode.</div>

<input type="text" class="wp-form-control wpcf7-text" id="destpostcode" name="destpostcode" placeholder="Destination Postcode">
<div class="error left-align" id="err-destpostcode">Please enter the destination postcode.</div>

<textarea class="wp-form-control wpcf7-textarea" cols="30" rows="10" id="notes" name="notes" placeholder="Notes"></textarea>

<button id="send-mail" class="wpcf7-submit button--itzel" type="submit"><i class="button__icon fa fa-envelope"></i><span>Send Message</span></button>

</form>


I can post the actual coding if need be where the user adds in the quantities of the items they have but is quite long

jscheuer1
09-02-2016, 02:10 PM
This ought to do it:


document.forms['contact-form']['cubicfeet'].value = cuft.toString();

You can put it in the existing code here:


cuft = parseInt(cuft);
tot = cuft;
if (tot<1)
{
var rslt = "Please enter relevant quantities in the form above.";
}
else
{
var rslt = "Thank you. We calculate your belongings will require " + cuft.toString() + " cubic feet of space.";
}
document.boxer.answer.value = rslt;
document.forms['contact-form']['cubicfeet'].value = cuft.toString();
}
// End -->

</script>

Might need to be tweaked for what happens when it's 0 or less, but it seems to me, that should work out OK - let me know though.

ianhaney
09-02-2016, 02:28 PM
Thank you so much, that worked perfect