469,902 Members | 1,815 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,902 developers. It's quick & easy.

Calculation error

Lazandra
Hi all

I have a bit of javascript code that is ment to add products together. This is a form and it works fine on its own. However when i integrate it into my php script it brings up this error 'document.form.info' is null or not an object.

Heres my top code

Expand|Select|Wrap|Line Numbers
  1. <?
  2. include("connect.php");
  3.  
  4.  
  5. if ($submit == "Update")
  6.     {
  7.  
  8.     $saved = mysql("db69117798","update payment set id = '$id', infoid = '$infoid', name = '$name', product_dogmobile = '$product_dogmobile', product_dogwalker = '$product_dogwalker', product_harness = '$product_harness', product_harness_area = '$product_harness_area', product_new = '$product_new', subtotal = '$subtotal', postage = '$postage', total = '$total', payment_name = '$payment_name', payment_cc = '$payment_cc', payment_valid_from = '$payment_valid_from', payment_expiry = '$payment_expiry', payment_issue = '$payment_issue', payment_security_number = '$payment_security_number', payment_type = '$payment_type', NOW() where infoid = $infoid");
  9.  
  10.     if ($saved)
  11.         {
  12.             echo "<script>\n";
  13.             echo "window.alert('$name has been updated');\n";
  14.             echo "</script>\n";
  15.           }
  16.     else
  17.         {
  18.             echo mysql_error() ;
  19.         }
  20.     }
  21.  
  22. $result = mysql("db69117798","select * from payment where infoid = $infoid");
  23. $result = mysql_fetch_array($result);
  24.     echo mysql_error() ;
  25. ?> 
  26.  
  27.  
  28.  
  29. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  30. <html>
  31. <head>
  32. <title>index-6</title>
  33. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  34. <link href="style.css" rel="stylesheet" type="text/css">
  35.  
  36. <SCRIPT LANGUAGE="JavaScript">
  37.  
  38. function  doMath() {
  39. var one = eval(document.myform.info.value)
  40. var two = eval(document.myform.info1.value)
  41. var three = eval(document.myform.info2.value)
  42. var four = eval(document.myform.info3.value)
  43. var five = eval(document.myform.info4.value)
  44. var prod = one  +   two  +  three  +  four  +  five
  45. document.myform.total.value=custRound(prod,2);
  46. }
  47. function custRound(x,places) {
  48. return (Math.round(x*Math.pow(10,places)))/Math.pow(10,places)
  49. }
  50.  
  51. </script>
And heres the form.

Expand|Select|Wrap|Line Numbers
  1. <form id="form1" name="form1" method="post" action="payment2.php">
  2.  <input type="hidden" name="infoid" value="<?=$infoid?>">
  3.  
  4.   <table width="400" border="0">
  5.     <tr>
  6.       <td colspan="2"><span class="style9">Product</span></td>
  7.     </tr>
  8.     <tr>
  9.       <td>&nbsp;</td>
  10.       <td>&nbsp;</td>
  11.     </tr>
  12.     <tr>
  13.       <td>Name:</td>
  14.       <td><input type="text" name="name" value="<?=$result[name]?>"></td>
  15.     </tr>
  16.       <tr>
  17.     <td>DogMobile</td>
  18.     <td>
  19.       <select name="info">
  20.     <option selected value="0">Select</option>
  21.     <option value="255">Small - &pound;255</option>
  22.     <option value="270">Medium - &pound;270</option>
  23.     <option value="280">Large - &pound;280</option>
  24.     <option value="290">XLarge - &pound;290</option>
  25.       </select>
  26.     </td>
  27.   </tr>
  28.   <tr>
  29.     <td>DogWalker</td>
  30.     <td>
  31.       <select name="info1">
  32.     <option selected value="0">Select</option>
  33.     <option value="180">Small - &pound;180</option>
  34.     <option value="190">Medium - &pound;190</option>
  35.     <option value="200">Large - &pound;200</option>
  36.     <option value="210">XLarge - &pound;210</option>
  37.  
  38.       </select>
  39.     </td>
  40.   </tr>
  41.     <tr>
  42.     <td>DogHarness</td>
  43.     <td>
  44.       <select name="info2">
  45.     <option selected value="0">Select</option>
  46.     <option value="26">Small - &pound;26</option>
  47.     <option value="29">Medium - &pound;29</option>
  48.     <option value="32">Large - &pound;32</option>
  49.     <option value="35">XLarge - &pound;35</option>
  50.       </select>
  51.     </td>
  52.   </tr>
  53.     <tr>
  54.     <td>New Products</td>
  55.     <td>
  56.       <select name="info3">
  57.     <option selected value="0">Select</option>
  58.     <option value="16.95">2in1 Lead - &pound;16.95</option>
  59.     <option value="35">Lifting Blanket - &pound;35</option>
  60.       </select>
  61.     </td>
  62.   </tr>
  63.   <tr>
  64.     <td>Postage</td>
  65.     <td>
  66.       <input type="text" name="info4">
  67.     </td>
  68.   </tr>
  69.     <tr>
  70.     <td>Total</td>
  71.     <td>
  72.       <input type="text" name="total">
  73.     </td>
  74.   </tr>
  75.   <tr>
  76.     <td> </td>
  77.     <td>
  78.       <input type="button" value="Calculate" onClick="doMath()" name="button">
  79.     </td>
  80.   </tr>
  81.     <tr>
  82.       <td colspan="2">&nbsp;</td>
  83.     </tr>
  84.     <tr>
  85.       <td colspan="2"><span class="style9">Payment</span></td>
  86.     </tr>
  87.     <tr>
  88.       <td><span class="style9">Type of card:</span></td>
  89.       <td><span class="style9">
  90.         <select name="payment_type">
  91.           <option value="visa">Visa</option>
  92.           <option value="mastercard">Mastercard</option>
  93.           <option value="maestro">Maestro</option>
  94.           <option value="solo">Solo</option>
  95.           <option value="american express">American Express</option>
  96.           <option value="jcb">JCB</option>
  97.           <option value="cheque">Cheque</option>
  98.           <option value="money order">Money Order</option>
  99.         </select>
  100.       </span></td>
  101.     </tr>
  102.     <tr>
  103.       <td><span class="style9">Name on card:</span></td>
  104.       <td><input name="payment_name" type="text" /></td>
  105.     </tr>
  106.     <tr>
  107.       <td><span class="style9">Card Number:</span></td>
  108.       <td><input name="payment_cc" type="text" /></td>
  109.     </tr>
  110.     <tr>
  111.       <td><span class="style9">Valid From:</span></td>
  112.       <td><input name="payment_valid_from" type="text" /></td>
  113.     </tr>
  114.     <tr>
  115.       <td><span class="style9">Expiry Date:</span></td>
  116.       <td><input name="payment_expiry" type="text" /></td>
  117.     </tr>
  118.     <tr>
  119.       <td><span class="style9">Issue Number (if applicable):</span></td>
  120.       <td><input name="payment_issue" type="text" /></td>
  121.     </tr>
  122.     <tr>
  123.       <td><span class="style9">Security number:</span></td>
  124.       <td><input name="payment_security_number" type="text" /></td>
  125.     </tr>
  126.     <tr>
  127.       <td>&nbsp;</td>
  128.       <td>&nbsp;</td>
  129.     </tr>
  130.     <tr>
  131.       <td>&nbsp;</td>
  132.       <td><span class="style9">
  133.         <label>
  134.        <input type="submit" name="submit" value="Update">
  135.         </label>
  136.       </span></td>
  137.     </tr>
  138.   </table>
  139. </form>
Can anyone help?

Cheers xxx
Oct 4 '07 #1
12 1197
dmjpro
2,476 2GB
Please Post the relevant JavaScript Code :-)

Debasis Jana.
Oct 4 '07 #2
Hi

its from line 38 to 51 in the first spam of code.

Cheers
Oct 4 '07 #3
dmjpro
2,476 2GB
Hi

its from line 38 to 51 in the first spam of code.

Cheers
Make sure that field exists when you page loaded using php :-)

Kind regards,
Dmjpro.
Oct 4 '07 #4
What do you mean? the field is there?
Oct 4 '07 #5
dmjpro
2,476 2GB
What do you mean? the field is there?
View the source code ..check that whether any field in your html page by name "info" :-)

Got my point buddy!

Debasis Jana
Oct 4 '07 #6
View the source code ..check that whether any field in your html page by name "info" :-)

Got my point buddy!

Debasis Jana
Hi

if we ignore the php for the time.

Expand|Select|Wrap|Line Numbers
  1. <SCRIPT LANGUAGE="JavaScript">
  2.  
  3. function  doMath() {
  4. var one = eval(document.myform.info.value)
  5. var two = eval(document.myform.info1.value)
  6. var three = eval(document.myform.info2.value)
  7. var four = eval(document.myform.info3.value)
  8. var five = eval(document.myform.info4.value)
  9. var prod = one  +   two  +  three  +  four  +  five
  10. document.myform.total.value=custRound(prod,2);
  11. }
  12. function custRound(x,places) {
  13. return (Math.round(x*Math.pow(10,places)))/Math.pow(10,places)
  14. }
  15.  
  16. </script>
  17.  
  18. <form id="form1" name="form1" method="post" action="payment2.php">
  19.  <input type="hidden" name="infoid" value="<?=$infoid?>">
  20.  
  21.   <table width="400" border="0">
  22.     <tr>
  23.       <td colspan="2"><span class="style9">Product</span></td>
  24.     </tr>
  25.     <tr>
  26.       <td>&nbsp;</td>
  27.       <td>&nbsp;</td>
  28.     </tr>
  29.     <tr>
  30.       <td>Name:</td>
  31.       <td><input type="text" name="name" value="<?=$result[name]?>"></td>
  32.     </tr>
  33.       <tr>
  34.     <td>DogMobile</td>
  35.     <td>
  36.       <select name="info">
  37.     <option selected value="0">Select</option>
  38.     <option value="255">Small - &pound;255</option>
  39.     <option value="270">Medium - &pound;270</option>
  40.     <option value="280">Large - &pound;280</option>
  41.     <option value="290">XLarge - &pound;290</option>
  42.       </select>
  43.     </td>
  44.   </tr>
  45.   <tr>
  46.     <td>DogWalker</td>
  47.     <td>
  48.       <select name="info1">
  49.     <option selected value="0">Select</option>
  50.     <option value="180">Small - &pound;180</option>
  51.     <option value="190">Medium - &pound;190</option>
  52.     <option value="200">Large - &pound;200</option>
  53.     <option value="210">XLarge - &pound;210</option>
  54.  
  55.       </select>
  56.     </td>
  57.   </tr>
  58.     <tr>
  59.     <td>DogHarness</td>
  60.     <td>
  61.       <select name="info2">
  62.     <option selected value="0">Select</option>
  63.     <option value="26">Small - &pound;26</option>
  64.     <option value="29">Medium - &pound;29</option>
  65.     <option value="32">Large - &pound;32</option>
  66.     <option value="35">XLarge - &pound;35</option>
  67.       </select>
  68.     </td>
  69.   </tr>
  70.     <tr>
  71.     <td>New Products</td>
  72.     <td>
  73.       <select name="info3">
  74.     <option selected value="0">Select</option>
  75.     <option value="16.95">2in1 Lead - &pound;16.95</option>
  76.     <option value="35">Lifting Blanket - &pound;35</option>
  77.       </select>
  78.     </td>
  79.   </tr>
  80.   <tr>
  81.     <td>Postage</td>
  82.     <td>
  83.       <input type="text" name="info4">
  84.     </td>
  85.   </tr>
  86.     <tr>
  87.     <td>Total</td>
  88.     <td>
  89.       <input type="text" name="total">
  90.     </td>
  91.   </tr>
  92.   <tr>
  93.     <td> </td>
  94.     <td>
  95.       <input type="button" value="Calculate" onClick="doMath()" name="button">
  96.     </td>
  97.   </tr>
  98.     <tr>
  99.       <td colspan="2">&nbsp;</td>
  100.     </tr>
  101.     <tr>
  102.       <td colspan="2"><span class="style9">Payment</span></td>
  103.     </tr>
  104.     <tr>
  105.       <td><span class="style9">Type of card:</span></td>
  106.       <td><span class="style9">
  107.         <select name="payment_type">
  108.           <option value="visa">Visa</option>
  109.           <option value="mastercard">Mastercard</option>
  110.           <option value="maestro">Maestro</option>
  111.           <option value="solo">Solo</option>
  112.           <option value="american express">American Express</option>
  113.           <option value="jcb">JCB</option>
  114.           <option value="cheque">Cheque</option>
  115.           <option value="money order">Money Order</option>
  116.         </select>
  117.       </span></td>
  118.     </tr>
  119.     <tr>
  120.       <td><span class="style9">Name on card:</span></td>
  121.       <td><input name="payment_name" type="text" /></td>
  122.     </tr>
  123.     <tr>
  124.       <td><span class="style9">Card Number:</span></td>
  125.       <td><input name="payment_cc" type="text" /></td>
  126.     </tr>
  127.     <tr>
  128.       <td><span class="style9">Valid From:</span></td>
  129.       <td><input name="payment_valid_from" type="text" /></td>
  130.     </tr>
  131.     <tr>
  132.       <td><span class="style9">Expiry Date:</span></td>
  133.       <td><input name="payment_expiry" type="text" /></td>
  134.     </tr>
  135.     <tr>
  136.       <td><span class="style9">Issue Number (if applicable):</span></td>
  137.       <td><input name="payment_issue" type="text" /></td>
  138.     </tr>
  139.     <tr>
  140.       <td><span class="style9">Security number:</span></td>
  141.       <td><input name="payment_security_number" type="text" /></td>
  142.     </tr>
  143.     <tr>
  144.       <td>&nbsp;</td>
  145.       <td>&nbsp;</td>
  146.     </tr>
  147.     <tr>
  148.       <td>&nbsp;</td>
  149.       <td><span class="style9">
  150.         <label>
  151.        <input type="submit" name="submit" value="Update">
  152.         </label>
  153.       </span></td>
  154.     </tr>
  155.   </table>
  156. </form>
  157.  
  158.  
It still doesn't work.

Regards
Oct 4 '07 #7
dmjpro
2,476 2GB
Well :-)
Your form name is form1 and you use "document.myform" ....
Why?

Kind regards,
Dmjpro.
Oct 4 '07 #8
gits
5,390 Expert Mod 4TB
hi ...

your form is called 'form1' not 'myform' ... and please avoid using eval ... you may use parseFloat() for that here ... eval == evil!!!

kind regards
Oct 4 '07 #9
gits
5,390 Expert Mod 4TB
Well :-)
Your form name is form1 and you use "document.myform" ....
Why?

Kind regards,
Dmjpro.
we nearly posted the same time ;) ...

and one more thing to mention: it would be much better to give the relevant select-boxes an id and use the getElementById()-reference for the fields to retrieve the values ...

kind regards
Oct 4 '07 #10
Thats brilliant!! Thank you both for your help. Much appreciated!

Cheers
Oct 4 '07 #11
dmjpro
2,476 2GB
hi ...

your form is called 'form1' not 'myform' ... and please avoid using eval ... you may use parseFloat() for that here ... eval == evil!!!

kind regards
Please tell me Gits why it is evil :-)

Debasis Jana
Oct 4 '07 #12
gits
5,390 Expert Mod 4TB
hi ...

eval will evaluate everything what you put in and you simply don't need it ... except for evaluating json-data ...

when using eval in php for example someone could try to inject code to the server that is executed there ... at the clientside this is not as relevant as at the server side, besides someone is able to cross-site-script to your page and something is executed within your page that you would never want to be happen there ... but in my personal point of view the most ugly thing with it is ... that a lot of people are using eval for everything ... instead of using correct javascript for it. as we could see we may use parseFloat in the case above, and so there should be always a way to avoid eval ... its simply an unnessecary operation in nearly all cases and it 'uglyfies' every code ... and i cannot think of any need for eval except for the mentioned reason with the json-data ...

knd regards
Oct 4 '07 #13

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by Tony Williams | last post: by
4 posts views Thread by vg-mail | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.