Saving data in dynamically generated text fields into the database

In this tutorial, I’m going to show you how you can save the data inputted from a dynamically generated text field into the database.

I consider this one a quick tutorial, since most of it has already been tackled in the first part:

https://rootninja.wordpress.com/2011/06/28/generating-form-elements-using-jquery/

All we have to do now is to write the php code which will loop through the values of those text fields.

 

Requirements

You must already have the script which allows you to generate text fields and select fields using jquery. Though we won’t really use the select field in this tutorial.

image

If you have not written it yet, then I advice you to go check out the tutorial which I linked above. Or just check my mediafire folder. You can see the full post about the folder in the files category. I currently don’t have internet connection, and I’ll probably publish this one early tomorrow so bear with me if I don’t have links for you.

 

Assumptions

You should already know how to make use of arrays, read arrays, and create arrays in php. And that $_POST is also an array. A container for all the values of input fields in an html form.

 

Procedure

Okay, so I won’t spoil the excitement any further. Here’s the 3 liner code which gets the job done:

foreach($_POST['tx'] as $tx){
		$db->query("INSERT INTO tbl_names SET names='$tx'");
	}

Yup, its just a simple foreach loop. Nothing fancy about it. As long as you know how arrays are formed in php, you are good to go. Using print_r will give you an idea, on what is this code all about:

This was the output when I clicked on the submit button:

Array ( [tx] => Array ( [0] => data [1] => integrated [2] => entity [3] => blog ) ) 

As you can see, php automatically assigns a value to the brackets which we placed on the last tutorial:

<input name="tx[]"/>

That’s not the full code from the last tutorial, but at least you already get what I mean. Those values assigned by php, served a purpose of being the index for each of those generated text fields.

Yeah, I guess my explanation sucks. So just study the array returned by print_r and you will know what the heck am I talking about.

Using ajax, in saving those data might be a little bit complicated. But I’ll show it to you next time I got lazy of coding the system that we are creating.
And speaking about system. I guess I’ll take this opportunity to give you, the awesome reader of this sucker blog. An update of my life, to make this post a little bit unique, because the internet is so wide and what I’m talking about might already exist somewhere else.

 

Life Update and Conclusion

Were working on this system called the Business Permit and Licensing System. And I admit that this isn’t easy to make considering the sheer amount of data to be stored in the database, the number of tables which are connected together (oh heck!, lots of join queries!). And other factors which unfortunately haven’t entered my mind yet.

That’s it for this tutorial, thank you so much for reading. Please don’t hesitate to use the comments section below if you didn’t understand something, you want to suggest something, or anything else that you wish to say to this boring blogger.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: