Sorry i couldn't elaborate on my problem more but i can't really see any problems. the two functions save()
and load()
run onclick
<button onclick="save()">save</button>
<button onclick="load()">load</button>
The code in meant to grab the value of all the inputs and save
it to local storage. then onload
add the saved data back into the form Fields
i'm new to php and JavaScript so it may look a bit messy but that's because i "generate" this via a php array and for loop (the array changes depending on with option is chosen)
//save
function save() {
if (typeof(Storage) != "undefined") {
var seometatitle = document.getElementById("seo_meta_title").value;
saveData(seometatitle);
function saveData(xseometatitle) {
localStorage.setItem("seometatitle", xseometatitle);
}
}
var seometadescription = document.getElementById("seo_meta_description").value;
saveData(seometadescription);
function saveData(xseometadescription) {
localStorage.setItem("seometadescription", xseometadescription);
}
}
var header = document.getElementById("header").value;
saveData(header);
function saveData(xheader) {
localStorage.setItem("header", xheader);
}
}
var pagetext = document.getElementById("page_text").value;
saveData(pagetext);
function saveData(xpagetext) {
localStorage.setItem("pagetext", xpagetext);
}
}
var linkurl = document.getElementById("link_url").value;
saveData(linkurl);
function saveData(xlinkurl) {
localStorage.setItem("linkurl", xlinkurl);
}
}
var buttontext = document.getElementById("button_text").value;
saveData(buttontext);
function saveData(xbuttontext) {
localStorage.setItem("buttontext", xbuttontext);
}
}
}} //end
// LOAD
function load() {
if (typeof(Storage) != "undefined") {
document.getElementById("seo_meta_title").value = localStorage.getItem("seometatitle");
document.getElementById("seo_meta_description").value = localStorage.getItem("seometadescription");
document.getElementById("header").value = localStorage.getItem("header");
document.getElementById("page_text").value = localStorage.getItem("pagetext");
document.getElementById("link_url").value = localStorage.getItem("linkurl");
document.getElementById("button_text").value = localStorage.getItem("buttontext");
}}
Just for the sake of it here is the php to generate the save function
$arrlength = count($matches[0]); # Get how many items in array two.
for($x = 0; $x < $arrlength; $x++) { #counting
$place_name = str_replace ( "_" , "" , $matches[0][$x] ); #Create readible name _ to space.
echo 'var ' .$place_name. ' = document.getElementById('.'"'.$matches[0][$x].'"'.').value;';
echo "
";
echo 'saveData('.$place_name.');';
echo "
";
echo 'function saveData(x'.$place_name.') {';
echo "
";
echo 'localStorage.setItem("'.$place_name.'", x'.$place_name.');';
echo "
";
echo '}}';
echo "
";
And here is the php for the load function
$arrlength = count($matches[0]); # Get how many items in array two.
for($x = 0; $x < $arrlength; $x++) { #counting
$place_name = str_replace ( "_" , "" , $matches[0][$x] ); #Create readible name _ to space.
// Retrieve
echo 'document.getElementById("'.$matches[0][$x].'").value = localStorage.getItem("'.$place_name.'");';
echo "
"; }
The array it's using is, its made automatically by grabbing {placeholder}
form a .html file the place holders change depending on the html file used
array(1) {
[0]=> array(6) {
[0]=> string(12) "placeholders"
[1]=> string(15) "page_meta_title"
[2]=> string(6) "header"
[3]=> string(9) "page_text"
[4]=> string(8) "link_url"
[5]=> string(11) "button_text" }
}
}
Any help would be greatly appreciated! thanks for your time.