Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8299695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:15:12+00:00 2026-06-08T16:15:12+00:00

I am pulling my hair out!! I have read all of the examples, tutorials,

  • 0

I am pulling my hair out!! I have read all of the examples, tutorials, etc. and others make this seem very simple, but I am new to Knockout and can’t get this to work.

I have a user profile form that I want to bind values of from a jquery ajax request. Essentially I want to be able to pull a profile from the server and load any previously saved data into the form values. I am able to request the JSON from an API and load the values into the form (I pre-populated the profile in the database ) using the mapping plugin.

$.getJSON("api/profile/" + "<?php echo $_SESSION['id']; ?>",function(json){

    var viewModel = ko.mapping.fromJS(json);     
    ko.applyBindings(viewModel);  

}); 

This works fine. The issues I’m having is on the posting side. If a user fills out the form for the first time and clicks the save button all of the values post back to the server in JSON – BUT – if you change only one field and then save again, ONLY the value that was changed gets posted and all of the other values post as null

The view model I am using (I have tried this with, ko.observalbe(), with “” values, etc!)

viewModel ={}

Here is my save call:

viewModel.save = function(){

              $.ajax({
                   url:"api/profile/" + "<?php echo $_SESSION['id']; ?>",
                   contentType: 'application/json',
                   data: ko.mapping.toJSON(viewModel),
                   type: "POST",
                   dataType: 'json',
                   success: function(data) {

                        alert("What's UP!");

                        alert(data)

                    },
                    error: function (data) { alert("error" + data); }
                    });
        }  

I know it has to do with the “data” line in the save and how Knockout binds to the values, but for the life of me I can’t figure it out. Any assistance would be most appreciated!

This is the final code that finally works for those searching:

<script type="text/javascript">

        var viewModel;

        function callback(data) {
            if (viewModel) {
                // model was initialized, update it
                ko.mapping.fromJS(data, viewModel); 
            } else {
                // or initialize and bind
                viewModel = ko.mapping.fromJS(data);
                ko.applyBindings(viewModel);
            }
        }

    save = function() {

             $.ajax({
                    url:"api/profile/" + "<?php echo $_SESSION['id']; ?>",
                    contentType: 'application/json',
                    data: ko.mapping.toJSON(viewModel),
                    type: "POST",
                    dataType: 'json',
                    success: function(data) {

                   alert( ko.mapping.toJSON(viewModel));    
                    },
                    error: function (data) { alert("error" + data); }
                    });
        }        


    $.ajax({
                url: "api/profile/" + "<?php echo $_SESSION['id']; ?>",
                contentType: 'application/json',
                type: "GET",
                dataType: 'json',
                success: function(data) {
                 callback(data);         


                }       
        });


  </script>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T16:15:18+00:00Added an answer on June 8, 2026 at 4:15 pm

    When I use knockout mapping plugin, I always create variable (can be global or in any application namespace) and initialize the model only once, and all the rest of the ajax requests just update the model, probably you are missing the update step.

    Here is the code below (javascript code is simplified, of course):

    var model; // it is undefined, I just need to have the way to find whether the model was initialized or not
    function callback(data) {
        if (model) {
            // model was initialized, update it
            ko.mapping.fromJS(data, model); 
        } else {
            // or initialize and bind
            model = ko.mapping.fromJS(data);
            ko.applyBindings(model);
        }
    }
    
    // do ajax request and call callback(data) on success callback
    

    Has it helped you to solve your problem?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been pulling my hair out trying to make this work. I have
Pulling my hair out with this simple CSS layout. I have a 2-column fixed
I've been pulling my hair out on this one all afternoon. Basically, I have
I've been pulling my hair out. I have a very simple postgre database, one
I've dug through all the tutorials and have been pulling my hair out trying
I have been pulling my hair out on this one all day, and I'm
Pulling my hair out over this one. I have one wordpress install at /2009
I have been pulling my hair out about this since Friday and I am
I'm on the verge of pulling my hair out over this. Here I have
I have been pulling my hair trying to figure this out but nothing is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.