Jump to content

Updating mysql table info with js array on reload


spenceddd

Recommended Posts

Hi,

 

I've been stuck on this for a while now and I keep coming back to it only to hit another wall.

 

What I am trying to do is allow a user to modify a table on a page then submit the changes by refreshing the page.

 

I have setup the javascript to allow the user to alter the values of the table cells. These values then get passed to a hidden form element that keeps track of the updated rows information in the form of a multidimensional array. This array has all the information needed to update the mysql tables if only I could pass the array back to php when the table reloads.

 

I have had success in passing a php array to javascript using json.

 

I have already posted a thread about this thinking that a jquery ajax function would work but I'm not sure that's necessary plus the thread was moved to the ajax forum where nobody seems to post.

 

Could anyone please help me with this?

 

Any help much appreciated...

 

Spencer

Link to comment
Share on other sites

if the values get put into a form, all you need to do is submit the form (button or javascript can do this), then detect if a forma has been submitted (because you're submitting to the same page) and just use php to do whatever it is you want to do with the values.

 

 

Link to comment
Share on other sites

true, for php that will be a very long string. But it gets there doesn't it?

so you have 2 options:

either seperate all the variables in js and place them in individual forms so that they arrive separated in your php file.

or

break down the entire string once it reaches your php file.

Link to comment
Share on other sites

Hmm...I can't send to individual form items as the number is limitless and unknown.

 

and I can't seem to interpret the string using php as the seperators for arrays are the same as seperators for array items.

 

Also isn't there just a relatively simple way to pass arrays from js to php?

 

Thanks for your help by the way.

Link to comment
Share on other sites

you can also search through the text to see if it has anything that will mess up your delimiters and escape them if necessary.

 

or you can always choose something really far-out for your delimiters...

|#|#|-|||||-|#|#| (what are the chances someone will write something like that?) :)

 

Link to comment
Share on other sites

I can't send to individual form items as the number is limitless and unknown.

 

You can send arrays through post and get using a syntax such as:

 

<input type="text" name="foo[]" />
<input type="text" name="foo[]" />
<input type="text" name="foo[]" />

 

The value of the fields would show up as an array within the $_GET or $_POST arrays depending on your forms method.

 

Also isn't there just a relatively simple way to pass arrays from js to php?

 

Yes, you can send it as json then use php's json_decode to turn it into a php array.

 

As for the rest of your question. It sound more like your stuck with how to format the JavaScript to send to PHP. This is a JavaScript issue and doesn't belong in this board.

Link to comment
Share on other sites

i may not understand the problem. but from what i can tell, could you not use json ? php has a json plugin, and theres both javascript and php libraries for json out there if you dont have the php_json or a library like jquery to work with. Just json encode the form elements and then decode in php. Failing that, why not use post arrays, you can send a limitless number of data via a single variable name using []. for example

 

<textarea name="body[]"></textarea>

 

Then you can just do

 

foreach($_POST['body'])

 

dont know if any pof those two are pertinent to your problem however

 

 

edit: thorpe seems to type quicker than me

Link to comment
Share on other sites

Ah, sorry I keep replying but others keep beating me to it before I submit.

 

Thanks for all the suggestions but djlee may have a point with posting the array, that sounds even simpler than using json....so to confirm can a post variable hold a multidimesional array?..and can a form item hold a js multidimesional array?

 

thanks a million!

 

Spence

 

 

Link to comment
Share on other sites

Thanks for all the suggestions but djlee may have a point with posting the array, that sounds even simpler than using json....so to confirm can a post variable hold a multidimesional array?

Yes

..and can a form item hold a js multidimesional array?

No.

Link to comment
Share on other sites

Sorry one last thing, and I know this is specifically js but how do you format the code to put a js multidimensional array to json, is it just:

 


var jsArrays = json_encode(multidimensionalArray);

 

I have tried to find this online but am getting complicated possibilities and not one simple...

 

Thanks again

 

Spencer

Link to comment
Share on other sites

Do you know of any json manipulation functions that you mentioned above?

 

Something that is equivalent to :

 

var jsArrays = json_encode(multidimensionalArray);

 

but that will allow me to store the multidimensional array in such a way that it can be successfully passed via post and held in a form element.

 

Thanks

 

Spencer

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.