Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an ajax function in a javascript which is to update database on button click; below is the ajax snippet. What is the mistake in this code?
I mean the database is not getting updated!!

C#
<script type="text/javascript">

            function sav() {

         
           var class_id5 = document.getElementById('ddlClass5');
           var class_id6 = document.getElementById('ddlClass6');
           var class_id7 = document.getElementById('ddlClass7');
           var subj_id = document.getElementById('ddlSubject');
           var mark5 = document.getElementById('txtMark5');
           var mark6 = document.getElementById('txtMark6');
           var mark7 = document.getElementById('txtMark7');
          
           $.ajax({
               type: "POST",
               url: "Student.aspx/SaveData",
               data: {"class_id5": class_id5, "subj_id": subj_id, "mark5": mark5, "class_id6": class_id6, "class_id7": class_id7,
                   "mark6":mark6,"mark7":mark7},
               contentType: "application/json; charset=utf-8",
               dataType: "json",
               success: alert("success")});



                 }





   </script>
Posted

1 solution

Change your code to:
JavaScript
<script type="text/javascript">
 
            function sav() {
 
         
           var class_id5 = document.getElementById('ddlClass5').value;
           var class_id6 = document.getElementById('ddlClass6').value;
           var class_id7 = document.getElementById('ddlClass7').value;
           var subj_id = document.getElementById('ddlSubject').value;
           var mark5 = document.getElementById('txtMark5').value;
           var mark6 = document.getElementById('txtMark6').value;
           var mark7 = document.getElementById('txtMark7').value;
          
           $.ajax({
               type: "POST",
               url: "Student.aspx/SaveData",
               data: "{class_id5:'" + class_id5 + "',subj_id:'" + subj_id + "',mark5:'" + mark5 + "',class_id6:'" + class_id6 + "',class_id7:'" + class_id7 + "',mark6:'" + mark6 + "',mark7:'" + mark7 + "'}",
               contentType: "application/json; charset=utf-8",
               dataType: "json",
              success: function () {
                        alert('success.');
                    }
}); 
                 } 
   </script> 
 
Share this answer
 
v2
Comments
Ankur\m/ 6-Jun-13 8:46am    
It would have been better if you pointed out the change that you made.
Zafar Sultan 6-Jun-13 8:50am    
Sometimes it is better to make them yourself. Explanation at times becomes confusing. If I would have said "You need to pass parameters wrapped in single quotes", It eventually would have ended in me writing the above thing. :)
Ankur\m/ 6-Jun-13 10:58am    
My point was - ALSO state what changes you have made along with the change.
Zafar Sultan 7-Jun-13 3:18am    
Yes. I missed that. Thanks.
Ankur\m/ 6-Jun-13 8:48am    
And if I am right you have just added the other parameters. How does that solve the problem? How do you know what parameters are expected on the server?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900