By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,025 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

i want to display my result in textbox

P: 6
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.         session_start();
  3.  
  4.         require("config/db_config.php");
  5.         $connection=mysql_connect ("$db_host","$db_user","$db_password")
  6.         or die ( " Not able to connect to server ");
  7.         $db_name="calc_db";
  8.         mysql_select_db($db_name,$connection);
  9.     ?>
  10. <DOCTYPE html>
  11. <head>
  12. <title></title>
  13. </head>
  14.  
  15.     <script>
  16.     function calculate_Marks()
  17.         {
  18.             var viva_Marks=document.getElementById('VivaMarks').value;
  19.             var Degree_Marks=document.getElementById('DegreeMarks').value;
  20.             var Written_Marks=document.getElementById('WrittenMarks').value;
  21.  
  22.             var viva_percentage =(20/100)*viva_Marks;
  23.             var Degree_percentage=(10/100)*Degree_Marks
  24.             var Written_percentage=(70/100)*Written_Marks;
  25.  
  26.             var Total=(viva_percentage+Degree_percentage+Written_percentage).toFixed(2);
  27.             document.getElementById('Total').innerHTML=Total;
  28.  
  29.  
  30.         }    
  31.  
  32.     </script>
  33.  
  34.  
  35.     <body>
  36.     <form ACTION="calc.php" method="POST">
  37.  
  38.     <table align="center" cellspacing="5px" cellpadding="10px">
  39.  
  40.             <tr>
  41.             <td>Enter Registartion Number:</td>
  42.             <td><input type="number" id="RegNo" name="RegNo" min="11" required  onchange="calculate_Marks()" style="width:150px;" ></td>
  43.             </tr>
  44.  
  45.             <tr>
  46.             <td>Enter Viva Marks:</td>
  47.             <td><input type="number" id="VivaMarks" name="VivaMarks" required  max="100" min="1" onchange="calculate_Marks()" ></td>
  48.             </tr>
  49.  
  50.             <tr>
  51.             <td>Enter Degree Marks:</td>
  52.             <td><input type="number" id="DegreeMarks" name="DegreeMarks"  required  max="100" min="1" onchange="calculate_Marks()" ></td>
  53.             </tr>
  54.  
  55.             <tr>
  56.             <td>Enter Written Marks Marks:</td>
  57.             <td><input type="number" id="WrittenMarks" required name="WrittenMarks" max="100" min="1" onchange="calculate_Marks()"></td>
  58.             </tr>
  59.  
  60.             <tr>
  61.             <td>Your Total Score (%):</td>
  62.             <td>
  63.             <h2 id="Total"  value="" name="Total"></h2>
  64.             </td>
  65.             </tr>
  66.  
  67.  
  68.             <tr>
  69.             <td></td>
  70.             <td rowspan="2"> <input type="submit" id="submit" name="submit" value="Submit"> <input type="reset" value="Reset" ></td>
  71.             </tr>
  72.  
  73.     </table>
  74.  
  75.  
  76.     <?php
  77.  
  78.     if(isset($_POST['submit']))
  79.     {
  80.     mysql_select_db("calc_db",$connection);
  81.  
  82.         $Registartion_No=trim($_POST['RegNo']);
  83.         $Viva=trim($_POST['VivaMarks']);
  84.         $Degree=trim($_POST['DegreeMarks']);
  85.         $Written=trim($_POST['WrittenMarks']);
  86.         $Total=$_POST['Total'];
  87.  
  88.  
  89.         $sql="(INSERT INTO tblscore(Registration_Number, Viva_Marks,Degree_Marks, Written_marks, Total) VALUES ('$Registartion_No', '$Viva','$Degree', '$Written','$Total')";
  90.         $res=mysql_query($sql) or die (mysql_error());
  91.  
  92.  
  93.  
  94.     }
  95.     ?>
  96. </form>
  97. </body>
  98. </html>
i had this code and i want to display total in textbox not in h2 tag and insert into mysql database ...plz help me doing this..i would appreciate for your help...
Jun 5 '14 #1

✓ answered by Dormilich

i want to display total in textbox not in h2 tag
code a textbox and update its value property.

Share this Question
Share on Google+
3 Replies


Dormilich
Expert Mod 5K+
P: 8,639
i want to display total in textbox not in h2 tag
code a textbox and update its value property.
Jun 5 '14 #2

P: 6
Expand|Select|Wrap|Line Numbers
  1.  
  2. <DOCTYPE html>
  3. <head>
  4. <title></title>
  5. </head>
  6.  
  7.     <script>
  8.  
  9.     function calculate_Marks()
  10.         {
  11.             var viva_Marks=document.getElementById('VivaMarks').value;
  12.             var Degree_Marks=document.getElementById('DegreeMarks').value;
  13.             var Written_Marks=document.getElementById('WrittenMarks').value;
  14.  
  15.             var viva_percentage =(20/100)*viva_Marks;
  16.             var Degree_percentage=(10/100)*Degree_Marks
  17.             var Written_percentage=(70/100)*Written_Marks;
  18.  
  19.             var Total=(viva_percentage+Degree_percentage+Written_percentage).toFixed(0);
  20.             document.getElementById('Total').value=Total;
  21.  
  22.  
  23.         }    
  24.  
  25.     </script>
  26.  
  27.  
  28.  
  29.  
  30.     <body>
  31.     <form ACTION="calc.php" method="POST" name="MarkEntryForm">
  32.  
  33.     <table align="center" cellspacing="5px" cellpadding="10px">
  34.  
  35.             <tr>
  36.             <td>Enter Registartion Number:</td>
  37.             <td><input type="number" id="RegNo" name="RegNo" min="11" required  style="width:150px;" ></td>
  38.             </tr>
  39.  
  40.             <tr>
  41.             <td>Enter Viva Marks(20%):</td>
  42.             <td><input type="number" id="VivaMarks" name="VivaMarks" required  max="100" min="1" ></td>
  43.             </tr>
  44.  
  45.             <tr>
  46.             <td>Enter Degree Marks(10%):</td>
  47.             <td><input type="number" id="DegreeMarks" name="DegreeMarks"  required  max="100" min="1" ></td>
  48.             </tr>
  49.  
  50.             <tr>
  51.             <td>Enter Written Marks Marks(70%):</td>
  52.             <td><input type="number" id="WrittenMarks" required name="WrittenMarks" max="100" min="1")"></td>
  53.             </tr>
  54.  
  55.             <tr>
  56.             <td>Your Total Score (%):  <INPUT TYPE="button" NAME="button" Value="=" onClick="calculate_Marks()"></td>
  57.             <td><INPUT TYPE="text" id="Total" NAME="Total" VALUE="">
  58.  
  59.             </td>
  60.             </tr>
  61.  
  62.  
  63.             <tr>
  64.             <td></td>
  65.             <td rowspan="2"> <input type="submit" id="submit" name="submit" value="Submit"> 
  66.             <input type="reset" value="Reset" ></td>
  67.             </tr>
  68.  
  69.     </table>
  70.  
  71.  
  72.     <?php
  73.  
  74.     if(isset($_POST['submit']))
  75.     {
  76.     mysql_select_db("calc_db",$connection);
  77.  
  78.         $Registartion_No=trim($_POST['RegNo']);
  79.         $Viva=trim($_POST['VivaMarks']);
  80.         $Degree=trim($_POST['DegreeMarks']);
  81.         $Written=trim($_POST['WrittenMarks']);
  82.         $Total=trim($_POST['Total']);
  83.  
  84.  
  85.         $sql="INSERT INTO tblscore VALUES ('','$Registartion_No','$Viva','$Degree','$Written', '$Total')";
  86.         $res=mysql_query($sql) or die (mysql_error());
  87.  
  88.         $query = mysql_query("SELECT * FROM tblscore WHERE Registration_Number='$Registartion_No'",($connection));
  89.         if(mysql_num_rows($query) != 1)
  90.             {
  91.                 echo "<script>alert('Mark of this Registration number already exists')</script>";
  92.                 // redirect back to form and populate with 
  93.                 // data that has already been entered by the user
  94.             }
  95.  
  96.     }
  97.  
  98.     ?>
  99. </form>
  100. </body>
  101. </html>
  102.  
  103.  
Jun 6 '14 #3

Dormilich
Expert Mod 5K+
P: 8,639
a very important point to consider:

currently, while JS calculates the total, the user still can modify this value himself and since you have no server-side verification this may lead to wrong data (at best).
better consider the JS calculation as an aid and re-do the necessary calculations in PHP before you insert them into the DB (there you can also get rid of the floating point arithmetics problem you currently have).
despite that you’re totally open to SQL Injection on top of using the deprecated mysql_* functions. better use PDO or MySQLi with Prepared Statements.
Jun 6 '14 #4

Post your reply

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