I have a function which saves an array each time the button is clicked to localStorage.The button will be clicked multiple times and I need to put this Array list into PHP somehow which is on another page from this file.
Thanks
a.js (this function listens onLoad of the page)
function doFirst(){
var button = document.getElementById("button");
button.addEventListener("click", save, false);
var buttons = document.getElementById("clear");
buttons.addEventListener("click", clear, false);
var buttonss = document.getElementById("submittodb");
buttonss.addEventListener("click", toPHP, false);
$.ajax({
method: 'post',
dataType: 'json',
url: 'edit.php',
data: { items: oldItems }, //NOTE THIS LINE, it's QUITE important
success: function() {//some code to handle successful upload, if needed
}
});
}
function save(){
var oldItems = JSON.parse(localStorage.getItem('itemsArray')) || [];
var newItem = {
'num': document.getElementById("num").value,
'methv': document.getElementById("methv").value,
'q1': document.getElementById("q1").value,
'q2':document.getElementById("q2").value,
'q3':document.getElementById("q3").value,
'q4':document.getElementById("q4").value,
'comm':document.getElementById("comm").value,
};
oldItems.push(newItem);
localStorage.setItem('itemsArray', JSON.stringify(oldItems));}
edit.php
$parsed_array = json_decode($_POST['items']);
and i get the error: Notice: Undefined index: items in /home/project/edit.php on line 9
-
Why not make a post to the server which you then use to render the new page?David L– David L2013年03月12日 20:41:44 +00:00Commented Mar 12, 2013 at 20:41
-
1POST the data to a PHP script using AJAX.Nick Pickering– Nick Pickering2013年03月12日 20:41:49 +00:00Commented Mar 12, 2013 at 20:41
-
If you are changing pages, then you can simply post the data to the server and render the new page. If you want to stay on the same page then and still post the data to the server, then your best bet is to use ajax.War10ck– War10ck2013年03月12日 20:42:47 +00:00Commented Mar 12, 2013 at 20:42
-
@NicholasPickering could you put an example please cause im pretty new to javascript and AJAXuser2162768– user21627682013年03月12日 20:42:56 +00:00Commented Mar 12, 2013 at 20:42
-
You need to learn AJAX to do what you want: w3schools.com/ajax/default.aspNick Pickering– Nick Pickering2013年03月12日 20:48:34 +00:00Commented Mar 12, 2013 at 20:48
3 Answers 3
In order to pass this array to PHP you need to:
- JSon-encode it
- Make an AJAX or POST request to PHP
- Parse the passed array into PHP array
If you're using jQuery (if you're not you should start - it is really handy tool) steps (1) and (2) is as simple as
$.ajax({
method: 'post',
dataType: 'json',
url: 'the URL of PHP page that will handle the request',
data: { items: oldItems }, //NOTE THIS LINE, it's QUITE important
success: function() {//some code to handle successful upload, if needed
}
});
In PHP you can parse the passed array with just
$parsed_array = json_decode($_POST['items']);
There is a direct connection between { items: oldItems } and $_POST['items']. The name of variable you give to the parameter in javascript call will be the name of key in $_POST array where it ends up. So if you just use data: oldItems in javascript you'll have all your entities scattered around the $_POST array.
More on $.ajax, and json_decode for reference.
6 Comments
$.ajax goes to javascript file. $parsed_array = json_decode($_POST['items']); - to the file that will respond to the url you've set in the $.ajax call. Spend some time reading about AJAX, it's not as hard as it may look like.You can create an AJAX function (use jQuery) and send the JSON data to the server and then manage it using a PHP function/method.
Basically, you need to send the data from the client (browser) back to the server where the database hosted.
1 Comment
Call
JSON.stringify(oldItems);to create the json stringDo a Do a POST request using AJAX.
Probably the simplest way is using jQuery:
$.post('http://server.com/url.php', { items: JSON.stringify(oldItems) }, function(response) {
// it worked.
});
8 Comments
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> in the head of your html file.JSON.stringify in the code sample.