spenceddd Posted July 28, 2011 Share Posted July 28, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/243076-updating-mysql-table-info-with-js-array-on-reload/ Share on other sites More sharing options...
WebStyles Posted July 28, 2011 Share Posted July 28, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/243076-updating-mysql-table-info-with-js-array-on-reload/#findComment-1248381 Share on other sites More sharing options...
spenceddd Posted July 28, 2011 Author Share Posted July 28, 2011 Yes but the form value is a js multidimensional array. Php doesn't seem to see this as anything but a very long string.... Quote Link to comment https://forums.phpfreaks.com/topic/243076-updating-mysql-table-info-with-js-array-on-reload/#findComment-1248417 Share on other sites More sharing options...
WebStyles Posted July 28, 2011 Share Posted July 28, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/243076-updating-mysql-table-info-with-js-array-on-reload/#findComment-1248419 Share on other sites More sharing options...
spenceddd Posted July 28, 2011 Author Share Posted July 28, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/243076-updating-mysql-table-info-with-js-array-on-reload/#findComment-1248426 Share on other sites More sharing options...
WebStyles Posted July 28, 2011 Share Posted July 28, 2011 in js you can loop through the array and create the string with your own separators before inserting it into the form. That should do the trick. Quote Link to comment https://forums.phpfreaks.com/topic/243076-updating-mysql-table-info-with-js-array-on-reload/#findComment-1248431 Share on other sites More sharing options...
spenceddd Posted July 28, 2011 Author Share Posted July 28, 2011 My issues there is that one of the table cell which is updateable is a text block where people can write comments, these comments could have any characters in which could ultimately trip it over. Quote Link to comment https://forums.phpfreaks.com/topic/243076-updating-mysql-table-info-with-js-array-on-reload/#findComment-1248435 Share on other sites More sharing options...
WebStyles Posted July 28, 2011 Share Posted July 28, 2011 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?) Quote Link to comment https://forums.phpfreaks.com/topic/243076-updating-mysql-table-info-with-js-array-on-reload/#findComment-1248436 Share on other sites More sharing options...
trq Posted July 28, 2011 Share Posted July 28, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/243076-updating-mysql-table-info-with-js-array-on-reload/#findComment-1248439 Share on other sites More sharing options...
djlee Posted July 28, 2011 Share Posted July 28, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/243076-updating-mysql-table-info-with-js-array-on-reload/#findComment-1248441 Share on other sites More sharing options...
spenceddd Posted July 28, 2011 Author Share Posted July 28, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/243076-updating-mysql-table-info-with-js-array-on-reload/#findComment-1248448 Share on other sites More sharing options...
trq Posted July 28, 2011 Share Posted July 28, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/243076-updating-mysql-table-info-with-js-array-on-reload/#findComment-1248459 Share on other sites More sharing options...
spenceddd Posted July 28, 2011 Author Share Posted July 28, 2011 OK great that makes sense, now can form element hold a json value? and can a post variable hold a json value? Quote Link to comment https://forums.phpfreaks.com/topic/243076-updating-mysql-table-info-with-js-array-on-reload/#findComment-1248464 Share on other sites More sharing options...
trq Posted July 28, 2011 Share Posted July 28, 2011 Yes and yes. Give it a try. Quote Link to comment https://forums.phpfreaks.com/topic/243076-updating-mysql-table-info-with-js-array-on-reload/#findComment-1248478 Share on other sites More sharing options...
spenceddd Posted July 28, 2011 Author Share Posted July 28, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/243076-updating-mysql-table-info-with-js-array-on-reload/#findComment-1248526 Share on other sites More sharing options...
trq Posted July 28, 2011 Share Posted July 28, 2011 json_encode is a php function. Most Javascript libraries (if your using one) come with some json manipulation functions. Otherwise, json is just simple JavaScript objects within a string. Quote Link to comment https://forums.phpfreaks.com/topic/243076-updating-mysql-table-info-with-js-array-on-reload/#findComment-1248536 Share on other sites More sharing options...
spenceddd Posted July 28, 2011 Author Share Posted July 28, 2011 OK great thanks. I will continue to look for some. Do you know of any of the top of your head? Thanks again for your help. Quote Link to comment https://forums.phpfreaks.com/topic/243076-updating-mysql-table-info-with-js-array-on-reload/#findComment-1248546 Share on other sites More sharing options...
trq Posted July 28, 2011 Share Posted July 28, 2011 Some what? Quote Link to comment https://forums.phpfreaks.com/topic/243076-updating-mysql-table-info-with-js-array-on-reload/#findComment-1248558 Share on other sites More sharing options...
spenceddd Posted July 29, 2011 Author Share Posted July 29, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/243076-updating-mysql-table-info-with-js-array-on-reload/#findComment-1248915 Share on other sites More sharing options...
trq Posted July 29, 2011 Share Posted July 29, 2011 Do you understand what json is? Quote Link to comment https://forums.phpfreaks.com/topic/243076-updating-mysql-table-info-with-js-array-on-reload/#findComment-1248992 Share on other sites More sharing options...
TeNDoLLA Posted July 29, 2011 Share Posted July 29, 2011 Using jquery and this you could do it I guess. http://code.google.com/p/jquery-json/ Quote Link to comment https://forums.phpfreaks.com/topic/243076-updating-mysql-table-info-with-js-array-on-reload/#findComment-1248996 Share on other sites More sharing options...
spenceddd Posted August 2, 2011 Author Share Posted August 2, 2011 Hi Thorpe, No I don't realy understand what it is to be honest, only that I have used it to pass an array from PHP to JS before.... Thanks TeNDoLLA I will check that link out. Spencer Quote Link to comment https://forums.phpfreaks.com/topic/243076-updating-mysql-table-info-with-js-array-on-reload/#findComment-1250664 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.