1

I have a page with a lot of input fields. I am using Jquery to get all of the values of the form at once using data: $("#profile").serialize().

Since my MySQL columns have the same name as my input fields. Is there an easy to select * from my db and load the results into the appropriate fields using jquery / ajax? Perhaps an example or a link?

Thanks, Scott

Edit: Ok thanks for the help, but I am still a bit lost and not getting it to work, here is what I have so far:

<script type="text/javascript">
$(window).load(function(){
$.ajax({
url: "/profile_functions/profile_about_retrieve.php",
type: 'POST',
data: allFormFields,
success: function(){
    //console.log('done');
}
});

jQuery(#profile).find(":input").each(function(key, val) {
fieldName = jQuery(this).attr("name");
if(fieldName != "" && fieldName != undefined) {
    allFormFields[fieldName] = jQuery(this).val();
  }
 });
});
</script>

And a little test data from my db being echo'ed via json_encode

[{"key":"test","value":"9"}]

And html

<form id="profile" method="post">
<input type="text" name="test"
</form>
1
  • you can used map function i guess Commented Mar 7, 2014 at 2:28

1 Answer 1

1
           jQuery(formId).find(":input").each(function(key, val) {

                fieldName = jQuery(this).attr("name");

                if(fieldName != "" && fieldName != undefined) {
                    allFormFields[fieldName] = jQuery(this).val();
                }
            });

Use the allFormFields array to send to the PHP endpoint

                  $.ajax({
                        url: url,
                        type: 'POST',
                        data: allFormFields,
                        success: function(){
                            console.log('done');
                        }
                    });
Sign up to request clarification or add additional context in comments.

2 Comments

Ok the logic looks good but I am little confused. How would I specify the ajax url for this?
url:url to url:'theURLlink.php'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.