Improve the code to just get the value of the localstorage [closed]

2

I have the following code:

jQuery.ajax({
  type: "POST",
  data:  $(accent_color).serialize(),
  success: function(data) {
    console.log(data); } });

How do I get this code to take the value below:

var accent_color = localStorage.getItem("accent_color");

And instead of console.log send to a post in php?

The problem is that the variable data is getting the entire HTML, not only the value of the localstorage, see the photo:

SomuchsothatifIputthis:

if($_COOKIE["data"]) { echo $_COOKIE["data"]; }

Nothing appears.

    
asked by anonymous 06.04.2016 / 18:26

2 answers

0

You can create a input of type hidden and receive the form data as POST.

<input type="hidden" value="valor" name="accent_color" id="accent_color">

Then just create a script to make the input capture the value.

var accent_color = localStorage.getItem("accent_color");
$('input #accent_color').attr('value',accent_color);
    
06.04.2016 / 19:08
0

Here's a summary of how to "catch" javascript data and send it to php and how to retrieve that data in php

    Recupere os dados em PHP oriundos do Javascript
    $arrayPost = $_POST; //Se foi serializado ou não
    $arrayPost = json_decode($_POST["meuJSON"], true); //Se foi enviado JSON

    //Codigo php que vai processar os dados e mandar de volta para o JS
    $arrToJSON = array(
    "dataPHPtoJs"=>"yourData",
    "asYouWant"=>"<div class=\".class1\">soemting</div>"    
    );  
    return json_encode(array($arrToJSON));




//Javascript que vai mandar algo para o php
$(document).on("event", "#idElement", function(){
     //Vc pode usar 
     var dt={ 
              ObjEvn:"btn_Login",
              dataJsToPHP: $("#txt_EmailLogin").val(),

            };

    //Ajax      
     var request =$.ajax({//http://api.jquery.com/jQuery.ajax/
                            url: "yourServer.php",
                            type: "POST",
                            data: dt,
                            dataType: "json"
                        });

    //Ajax Done catch JSON from PHP 
        request.done(function(dataset){
            for (var index in dataset){ 
                 dataPHPtoJsJS=dataset[index].dataPHPtoJs;
                 asManyasYouWantJS=dataset[index].asYouWant;
             }

             //JavaScript conditions. Here you can control the behaivior of your html object, based on your PHP response
             if(dataPHPtoJsJS){
                $( "#idYourHtmlElement" ).removeClass( "class1" )
                $( "#idYourHtmlElement" ).addClass( "class2" )
             }


     }); 

    //Ajax Fail 
        request.fail(function(jqXHR, textStatus) {
            alert("Request failed: " + textStatus);
        }); 
}

Note: If serializing use this function:

$.fn.serializeObject = function(){
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

and if you send JSON the whole function would look like this:

var meuJson = JSON.stringfy($(form).serializeObject());
    
02.12.2016 / 05:13