Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Team

I have a form when i insert values to be stored to the local database, they dont get clear after being sent from the form they remained. I am struggling to understand how to implement if there is a logic.

What I have tried:

<form action="/" method="post">


       <!--Title-->
       <div class="form-group">
           <input asp-for="Title" id="Title" type="text" class="form-control" value="Title" />
       </div>

       <!--Description-->
       <div class="form-group">
           <input id="Description11" type="text" class="form-control" value="Description" />
       </div>

       <!--Date-->
       <div class="form-group">
           <input asp-for="Date" id="Date" type="Date" class="form-control" value="Date_Occured" />
       </div>

       <!--Time-->
       <div class="form-group">
           <input asp-for="Time" id="Time" type="time" class="form-control" value="Time"/>
       </div>

       <!--Assignment_Group-->
       <div class="form-group">
           <input asp-for="Assignment_Group" id="Assignment_Group" type="assignment_group" class="form-control" value="Assignment_Group" />
       </div>


       <!--Reported_CI-->

       <div class="form-group">
           <input asp-for="Reported_CI" id="Reported_CI" type="reported_ci" class="form-control" value="Reported_CI"/>
       </div>

       <!--Submit form-->
       <div class="form-group">
           <button type="button" class="btn btn-primary" id="btnSubmit">Submit</button>
       </div>

   </form>

<script>

           $("#btnSubmit").click(function () {
               debugger;


               var Title = $("#Title").val();
               var Date = $("#Date").val();
               var Description = $("#Description11").val();
               var Date_Occured = $("#Date_Occured").val();
               var Time = $("#Time").val();
               var Assignment_Group = $("#Assignment_Group").val();
               var Reported_CI = $("#Reported_CI").val();

               alert(Title + Date + Description + Date_Occured + Time + Assignment_Group + Reported_CI );

               // Ajax call request transpoted to save values.
               $.ajax({
                   type: "POST",
                   url: "/Home/SaveIncidentTemplates?Title=" + Title + "&Date=" + Date + "&Description=" + Description + "&Date_Occured=" + Date_Occured + "&Time=" + Time + "&Assignment_Group=" + Assignment_Group + "&Reported_CI=" + Reported_CI,

                   dataType: "application/json",
                   success: function () {
                       alert("Saved successfully");
                   }

               })

           });



   </script>
Posted
Updated 14-Nov-21 22:14pm

1 solution

Firstly, that's not a good way to pass the data. You're making a POST request, but passing everything in the URL as if it were a GET request. And you're not URL-encoding the values you send in the URL, so one wrong character by the user could corrupt the data you're inserting.
JavaScript
$.ajax({
    type: "POST",
    url: "/Home/SaveIncidentTemplates",
    data: {
        Title: Title,
        Date: Date,
        Description: Description,
        Date_Occured: Date_Occured,
        Time: Time,
        Assignment_Group: Assignment_Group,
        Reported_CI: Reported_CI
    },
    ...

Once the request succeeds, you then need to clear the form yourself:
JavaScript
...
success: function(){
    alert("Saved successfully");
    $("#Title").val("");
    $("#Date").val("");
    ...
}
 
Share this answer
 
Comments
Tgcobza Mkontwana 15-Nov-21 4:55am    
@Richard modify the ajax call, now i am getting this javascript error when debugging, jquery-3.4.1.js:9837 POST https://localhost:44344/Home/SaveIncidentTemplates 500
send @ jquery-3.4.1.js:9837
ajax @ jquery-3.4.1.js:9434
(anonymous) @ Index:203
dispatch @ jquery.min.js:3
r.handle @ jquery.min.js:3
Richard Deeming 15-Nov-21 4:57am    
Error 500 means the server-side code you're calling has thrown an exception. You'll need to debug that code to find out why.
Tgcobza Mkontwana 15-Nov-21 5:06am    
@Richard will debug the reason, thanks for raising this to me

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