I want to store my array into my database using jQuery. Here is my code i'm using serialize(); function. Its storing correctly but when i unserialize(); nothing happening. Please help here is my code.

<script>
    $(document).ready(function(){
        $("#save-skills").click(function(){
            var skills = $('input[name="skills[]"]').serialize();
            console.log(skills);
            $('#loading').css('display', 'block');
            var queryString = "skills=" + skills + "";
            $.ajax({
              url: 'save_skills.php',
              cache: false,
              contentType: "application/json; charset=utf-8",
              dataType:'json',
              async: false,
              type:'GET',
              data: queryString,
              success: function(data){
                $('#loading').css('display', 'none');
                if('error' in data){
                    $(".re_skills_success").hide(100);
                    $(".re_skills_err").text(data.error);
                    $(".re_skills_err").slideDown(400).delay(3000).slideUp(400);
                }else if('success' in data){
                    $(".re_skills_err").hide(100);
                    $(".re_skills_success").text(data.success);
                    $(".re_skills_success").slideDown(400).delay(3000).slideUp(400);
                }
              }
            });
        });
    });
</script>

<!-- Unserialize() code -->

$who = $_COOKIE['Email'];
$skills = @mysql_query("SELECT * FROM `skills` WHERE `who` = '$who'");
$skill_count = @mysql_num_rows($skills);
echo "<h1>Skill count : $skill_count</h1>";
while($sk = @mysql_fetch_array($skills)){
    $set_of_skills = "{$sk['set_of_skills']}";
    echo "<h1>Array skills : $set_of_skills</h1>";
    print_r(unserialize($set_of_skills));
}

this is printing nothing.
Member Avatar for diafol

this is printing nothing.

I assume that you get skill_count and the h1 echo? If not your problem is not the print_r but probably the SQL.

Member Avatar for diafol

Ok, the serialize/unserialize functions I don't think are compatible. The serialize() is a jQuery function, which produces a 'url-like' querystring.

Serializing with PHP creates a whole different beast, so I can't see how they'd work together. Serialize/Unserialize with the same language.

Sorry for late reply - leaving a comment doesn't flag the watched list that there's any change. Mind you I don't mind the rep! :)

Is there any way to store the array into the database using jQuery ?

Member Avatar for diafol

How do you need your data stored? url-type (jQuery serialize) or the more info-full php serialize?
You can't use jQuery to store to db without using ajax.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.