Welcome Guest, Not a member yet? Register   Sign In
Javascript array to PHP (to save in MySQL)
#1

[eluser]stingray9[/eluser]
Hey,

i got a little problem saving data from my javascript array.


What is the best way to send an array to my controller so I can query it into my database?
I've been trying this for a whole day, but nothing seems to work.

Thanks in advance!
#2

[eluser]pickupman[/eluser]
How about using AJAX(jQuery), and posting the string to your controller. Then use php json_decode() to convert the string back into an array.
#3

[eluser]stingray9[/eluser]
Thanks for your answer.

What i was trying was something like this:

Quote:$.ajax({
type: "POST",
url: "../controllers/myController.php",
data: myJSON
}
});

I don't know if this is correct, if you know that my json string is 'myJSON'.
#4

[eluser]richthegeek[/eluser]
I usually do it like this:

Code:
$.post( base_url + "ajax/save_boxes", { boxes: JSON.encode( boxes ) } )

This goes to the Ajax:Confusedave_boxes() method in /system/application/controllers/ajax.php, which then uses PHP's json_decode to parse it.

PS: the "base_url" variable in the JS is set in the header file like so:
Code:
[removed]var base_url = '<?=base_url();?>'[removed]
#5

[eluser]stingray9[/eluser]
Thanks for everything, but it sill doesn't work, i might miss something

nothing happens.
What am I doing wrong?

Thanks a lot in advance
#6

[eluser]richthegeek[/eluser]
you are posting to entirely the wrong address.

POST sends to a HTTP web address... NOT a file in your filestructure. That means it accesses the controller/method in exactly the same way as a human visitor would... so you want to post it to "/makeCanvas/saveBoxes", allowing for subfolders if required...

BTW: for all Javascript development, you're gonna want to use the Firebug plugin for Firefox - the "Console" tab will log all AJAX transactions (what was sent, what was received) and turn bright red when an AJAX send 404's... if you had used that you'd have seen in approximately 3 seconds that you were posting to somewhere that didn't exist, although not sure it'd have helped get around the brainfart you were having...
#7

[eluser]stingray9[/eluser]
Damn, thanks all, also for the tip to use firebug, it all works fine now.




Theme © iAndrew 2016 - Forum software by © MyBB