mgunnels Posted July 9, 2011 Share Posted July 9, 2011 Alright, I give up! ----I need help! Here is what I am doing. I am creating a way for a user to create dynamic tasks/subtasks using Jquery. My problem is really 3 fold. 1. (the easy part)...I am going to use Jquery Serialize() to submit the form to my PHP script. 2. (now we are getting warmed up)...I am lost on how to read the dynamically created fields...in PHP. The Jquery script will create a unique name for each field that is dynamically created. 3. (even harder)...MySQL (a)---I need to figure out an appropriate table format so that the tasks are associated with the subtasks. I tried to incorporate this in the names that are generated through jquery, but in SQL is where the real magic will need to happen in order to tie the subtasks to the corresponding task. (b)----How do I create the insert statement? Here is the code thus far: http://jsfiddle.net/mgunnels/WNUEJ/38/ Please help....I know this is a lot to ask, but I am truly lost. Quote Link to comment https://forums.phpfreaks.com/topic/241457-jquery-serialize-php-mysql/ Share on other sites More sharing options...
trq Posted July 9, 2011 Share Posted July 9, 2011 I'm not 100% sure this is what your getting at but if you name form elements as arrays you can loop through them in php. eg; <input type="text" name="foo[]" /> <input type="text" name="foo[]" /> <input type="text" name="foo[]" /> Will allow you to do: foreach ($_POST['foo'] as $foo) { echo $foo; } Quote Link to comment https://forums.phpfreaks.com/topic/241457-jquery-serialize-php-mysql/#findComment-1240333 Share on other sites More sharing options...
mgunnels Posted July 9, 2011 Author Share Posted July 9, 2011 No not really. I can use JQuery serialize(); to handle the post. The post is not the issue. The issue is what to do after that. Quote Link to comment https://forums.phpfreaks.com/topic/241457-jquery-serialize-php-mysql/#findComment-1240342 Share on other sites More sharing options...
trq Posted July 9, 2011 Share Posted July 9, 2011 Ok, well you might want to better explain the issue. Quote Link to comment https://forums.phpfreaks.com/topic/241457-jquery-serialize-php-mysql/#findComment-1240344 Share on other sites More sharing options...
PFMaBiSmAd Posted July 9, 2011 Share Posted July 9, 2011 Thorp's suggestion to use array names for the dynamically produced form fields had to do with a method that would allow the "what to do after that" to be a simple matter of looping through the resulting array(s) after they are posted. Dynamically creating a series of uniquely named form fields is shooting yourself in the foot because of the extra processing you must do to find and parse the field names and values (and it also takes more code to keep track of the names while you dynamically produce the fields.) To use an array(s), all you need to do to create another dynamic field is to add an identical array element ( name="foo[]" ) and then you simply use a foreach() loop(s) in the php code to access each element of the array(s.) Quote Link to comment https://forums.phpfreaks.com/topic/241457-jquery-serialize-php-mysql/#findComment-1240349 Share on other sites More sharing options...
mgunnels Posted July 9, 2011 Author Share Posted July 9, 2011 Let me try again. 1. A knowledge of Jquery serialize(); is needed. This creates a JSON string that would need to be decoded after the user posts the form. ---The form uses Jquery to dynamically create/name the input fields. An example of the form is here: http://jsfiddle.net/mgunnels/WNUEJ/38/ Each task can have multiple subtasks. Instead of trying to put an huge code bock in this forum all the code is found at the above URL. It should explain how everything is done. 2. I need to get help on how to create the decode of the JSON string and then insert that data into the DB. 3. The next issue is the DB table. I am lost on how I should format/create this table(s) to handle dynamically created fields. And also..how to associate the tasks/subtasks. ***Imagine that a user can create multiple tasks and each tasks can have multiple subtasks. Each tasks/subtask relationship should be unique. Check out the code for a better idea. Sorry. Quote Link to comment https://forums.phpfreaks.com/topic/241457-jquery-serialize-php-mysql/#findComment-1240360 Share on other sites More sharing options...
mgunnels Posted July 9, 2011 Author Share Posted July 9, 2011 Thorp's suggestion to use array names for the dynamically produced form fields had to do with a method that would allow the "what to do after that" to be a simple matter of looping through the resulting array(s) after they are posted. Dynamically creating a series of uniquely named form fields is shooting yourself in the foot because of the extra processing you must do to find and parse the field names and values (and it also takes more code to keep track of the names while you dynamically produce the fields.) To use an array(s), all you need to do to create another dynamic field is to add an identical array element ( name="foo[]" ) and then you simply use a foreach() loop(s) in the php code to access each element of the array(s.) I think the main issue I had with this is that...each subtask that is dynamically created must relate back to its task. I may be way off base...but the names look some thing like: task_1 subtask1:1 subtask1:2 (where 1= the task number and 2 tells me this is the 2nd subtask for task 1) Quote Link to comment https://forums.phpfreaks.com/topic/241457-jquery-serialize-php-mysql/#findComment-1240363 Share on other sites More sharing options...
PFMaBiSmAd Posted July 9, 2011 Share Posted July 9, 2011 The array index (you can use name="foo[key]") can be used to convey the names/values. Quote Link to comment https://forums.phpfreaks.com/topic/241457-jquery-serialize-php-mysql/#findComment-1240380 Share on other sites More sharing options...
PFMaBiSmAd Posted July 9, 2011 Share Posted July 9, 2011 If your form dynamically produced fields that look like this - <form method='post' action='some_page.php'> <input type='text' name='task[1]'><br /> <input type='text' name='task[1:1]'><br /> <input type='text' name='task[1:2]'><br /> <input type='submit'> </form> Your php code to iterate over that data would look like this - <?php echo '<pre>',print_r($_POST,true),'</pre>'; // display the data for demonstration purposes foreach($_POST['task'] as $key => $value){ $parts = explode(':',$key); if(count($parts) == 1){ // no ':' was in the $key, primary task, $parts[0] is the task number echo "Primary Task, Number: $parts[0], Value: $value<br />"; } else { // a ':' was in the $key, subtask, $parts[0] contains the primary task number and $parts[1] contains the subtask number // if you have sub-sub-tasks..., those numbers would be in $parts[2], $parts[3], ... echo "Subtask, Primary: $parts[0], Subtask Number: $parts[1], Value: $value<br />"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/241457-jquery-serialize-php-mysql/#findComment-1240384 Share on other sites More sharing options...
jcbones Posted July 9, 2011 Share Posted July 9, 2011 2. I need to get help on how to create the decode of the JSON string json_decode Quote Link to comment https://forums.phpfreaks.com/topic/241457-jquery-serialize-php-mysql/#findComment-1240541 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.