Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/241457-jquery-serialize-php-mysql/
Share on other sites

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;
}

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.)

 

 

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.

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)

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 />";
}
}

?>

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.