473,507 Members | 13,917 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic Fields js/php to MySql need to submit dynamically to the database

10 New Member
I can not get the values from the javascript add row to go dynamically as a row into MySql only the form values show up as the form below as one row. I made it as an array, but no such luck, I have tried this code around a multitude of ways. I don't know what I am doing wrong, kindly write out the correct way.



Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2.     <html xmlns="http://www.w3.org/1999/xhtml">
  3.     <head>
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5.     <title>Dynamic Fields js/php to MySql need to submit dynamically to the database</title>
  6.     <?php
  7.     require ('database.php'); 
  8.     ?>
  9.      <script type="text/javascript">
  10.      var counter     =     1;
  11.      var collector     =     "";
  12.  
  13.      function addfields(indx)
  14.      {
  15.          var tbl = document.getElementById('table_id');
  16.          var newtr = document.createElement('tr');
  17.          counter = counter + indx;
  18.  
  19.          newtr.setAttribute('id','tr'+counter);
  20.  
  21.          newtr.innerHTML = '<td><input type="checkbox" name="checkb'+counter+'" id="checkb'+counter+'" value="'+counter+'" onclick="checkme('+counter+')"></td><td><input type="text" name="text1[]"></td><td><textarea name="textarea1[]"></textarea></td>';
  22.  
  23.          tbl.appendChild(newtr);
  24.      }
  25.  
  26.      function checkme(dx)
  27.     {
  28.           collector += dx+",";
  29.     }
  30.  
  31.      function deletetherow(indx)
  32.      {
  33.          var col = collector.split(",");
  34.  
  35.          for (var i = 0; i < col.length; i++) 
  36.          {
  37.              var remvelem = document.getElementById('tr'+col[i]);
  38.              var chckbx = document.getElementById("checkb"+col[i]);
  39.              if(remvelem && chckbx.checked)
  40.              {
  41.                  var tbl = document.getElementById('table_id');
  42.                 tbl.removeChild(remvelem);
  43.             }
  44.          }
  45.      }
  46.     </script>
  47.     </head>
  48.     <body>
  49.     <form enctype="multipart/form-data" id="1" style="background-color:#ffffff;" action="<?php echo $_SERVER['PHP_SELF']; ?>"></form> 
  50.     <table id="table_id" >
  51.       <tr id="tr1" class="trmain">
  52.       <td>
  53.       </td>
  54.        <td>
  55.        <input type="text" name="text1[]">
  56.         </td>
  57.           <td>
  58.             <textarea name="textarea1[]"></textarea>
  59.            </td>  
  60.          </tr>
  61.      </table>
  62.     <input type="button" value="Add" onClick="addfields(1);" />&nbsp;
  63.     <input type="button" value="Delete" onClick="deletetherow()" />
  64.     <input type="submit" value="Send" id="submit"  name="submit"/>
  65.  
  66.     <?php
  67.     if(isset($_POST['submit'])) {
  68.     for ($i=0; $i < count($_POST['text1']); $i++ )
  69.     {
  70.     $ced = stripslashes($_POST['text1'][$i]);
  71.     $erg = stripslashes($_POST['textarea1'][$i]);
  72.     }
  73.  
  74.  
  75.     $bnt = mysql_query("INSERT INTO tablename (first, second) VALUES ('$ced', '$erg')")or die('Error: '. mysql_error() );
  76.     $result = mysql_query($bnt);
  77.     }
  78.  
  79.     ?>
  80.  
  81.     </body>
  82.     </html>
Aug 23 '14 #1
10 2441
Exequiel
288 Contributor
I suggest you need to use ajax in order to get all the values of the fields in the row, You need to loop your insertion, I can't explain it very well, I will create a sample program for you and you can customize it. Sorry if i replied so much late. I'm so busy this past few days, I'm creating a system for the company and edit some system for machine operation, using rockwell software.
Aug 23 '14 #2
CRAYON34
10 New Member
I understand, and will await your reply. I have beat my head around this a lot. As in my other posts to you my other script worked as such but textarea issues, so merged and no luck as with child cells no good for textarea as re-populates innnerHTML. I really don't see why there isn't easy way dynamically. Okay, please be descriptive on your Ajax method as I am new with that language.
Aug 23 '14 #3
Exequiel
288 Contributor
This is what i've got now
Expand|Select|Wrap|Line Numbers
  1. var required_fieldz = false; //put this on the top of your initialized variables.
  2.  
Copy paste this functions
Note 1: make sure you put required to the fields that you want to be required.

Note 2: Download a jquery package and include it to your page, for you to use some of functions i call and to use ajax also.

Go to this site to download jquery package. http://jquery.com/download/ and choose this Download the compressed, production jQuery 1.11.1 to download.
Expand|Select|Wrap|Line Numbers
  1. //how to save to database.
  2. function savedata()
  3. {
  4.     $("form#myform").serialize();//get all fields in the form
  5.     getrequiredfields("myform");//go to function getrequiredfields("myform") with id to check some fields that are not field up    
  6.  
  7.     if(required_fieldz == false)
  8.     {
  9.         //all fields field up. use ajax to insert and loop your insertion
  10.         alert("insert");
  11.     }
  12.     else
  13.     {
  14.         //some fileds not yet field up
  15.         alert("no");    
  16.     }
  17. }
  18.  
  19. //check the required fields in the form 
  20. function getrequiredfields(formid)
  21. {
  22.     var x = false;
  23.     var y = false;
  24.     var z = false;
  25.  
  26.     $.each($('form#'+formid).serializeArray(), function(i, field)
  27.     {    
  28.         if($('input[name='+field.name+']').attr('required')=="required" || $('textarea[name='+field.name+']').attr('required')=="required" || $('select[name='+field.name+']').attr('required')=="required"){
  29.             if($('input[name='+field.name+']').val()=='')
  30.             {
  31.                 $('input[name='+field.name+']').css('box-shadow','0px 0px 2px 2px #F00');
  32.                 //alert(field.name);
  33.                 x = true;
  34.             }
  35.             else
  36.             {
  37.                 $('input[name='+field.name+']').css('box-shadow','');
  38.                 x = false;
  39.             }
  40.  
  41.  
  42.             if($('textarea[name='+field.name+']').val()=='')
  43.             {
  44.                 //alert(field.name);
  45.                 $('textarea[name='+field.name+']').css('box-shadow','0px 0px 2px 2px #F00');
  46.  
  47.                 y = true;
  48.             }
  49.             else
  50.             {
  51.                 $('textarea[name='+field.name+']').css('box-shadow','');
  52.                 y = false;
  53.             }
  54.  
  55.             if($('select[name='+field.name+']').val()=='')
  56.             {
  57.                 //alert(field.name);
  58.                 $('select[name='+field.name+']').css('box-shadow','0px 0px 2px 2px #F00');
  59.  
  60.                 z = true;
  61.             }
  62.             else
  63.             {
  64.                 $('select[name='+field.name+']').css('box-shadow','');
  65.                 z = false;
  66.             }
  67.  
  68.             if( x == true || y == true || z == true )
  69.             {
  70.                 required_fieldz = true;
  71.             }
  72.             else
  73.             {
  74.                 required_fieldz = false;
  75.             }
  76.         }//
  77.     });    
  78. }
  79.  
  80.  
Aug 24 '14 #4
Exequiel
288 Contributor
I don't code yet to the insertion of your data, i will post it if i'm finish, you can post comment if you already implement the code. :)
Aug 24 '14 #5
Exequiel
288 Contributor
by the way put this to your submit button in your form onclick="savedata();" example <input type="submit" name="saveme" id="saveme" value="Save me" onclick="savedata();"> and take note put your form id to getrequiredfields("myform"); in savedata() function just replace the myform
Aug 24 '14 #6
CRAYON34
10 New Member
I am a bit confused with all of this. I did implement the code. I usually use the CDN url from Google for jquery as in this case, whenever I have used it (jquery). I put required in the fields I wanted required already and as mentioned with HTML 5 its' cross browser with something like this fallback http://stackoverflow.com/questions/3...ttribute-html5 and have seen work in all browsers. But, I still tried to implement this code. Now, on submit, nothing happens. I put required as just mentioned in the fields and the
Expand|Select|Wrap|Line Numbers
  1. newtr.innerHTML =
where needed. It works like that. Anyway, if you meant something else let me know.
Where jquery is
Expand|Select|Wrap|Line Numbers
  1. $("form#myform")
the myform is what the name of the form was set to and the
Expand|Select|Wrap|Line Numbers
  1. ('form#'+formid)
was in same instance gave my form the id of formid. I replaced wherever needed. I tried without also, still no result on submit, nothing happens. I don't follow you "and take note put your form id to getrequiredfields("myform"); in savedata() function just replace the myform" as I did rename that with form id but still nothing on submit.

Since I have the html required attribute then all I am really worried about is getting the data dynamically into the MySQL database as separate rows. This is the milestone now that I can't break through, we'll and that what you gave me for the required is not working for me. I can post you a mini version if your not follow me of what have done.
Aug 24 '14 #7
Exequiel
288 Contributor
coppy paste again the entire getrequiredfields() function, i made updates there.
in savedata() function in the line with getrequiredfields("myform"); the myform there is the id of your form, just replace it with the id that you put to your form. The $("form#myform").serialize(); we will use this later,
Aug 24 '14 #8
Exequiel
288 Contributor
it must be work, did you put this to your submit button as its function ?
Expand|Select|Wrap|Line Numbers
  1. onclick="savedata();"
Aug 24 '14 #9
Exequiel
288 Contributor
I made the sample system already. Whats your email address? I will send the files.
Aug 24 '14 #10
Exequiel
288 Contributor
You can now download the files here. http://www.mediafire.com/download/bk...8aav/bytes.rar
Aug 24 '14 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

6
1948
by: P | last post by:
Hi, As the subject says, I would like to submit dynamically generated hidden forms with javascript. I'm fairly new to javascript. I've googled around but couldn't find a solution. I would...
3
1272
by: Darrel | last post by:
I'm writing out my own Table via codebehind. Each row has an INPUT TYPE="TEXT" that I am writing out dynamically as well. I want people to be able to update this text, then, upon submit, I'm...
6
1169
by: Sparky Arbuckle | last post by:
web.config <?xml version="1.0" encoding="utf-8" ?> <configuration> <!-- application specific settings --> <appSettings> <add key="myConnection" value="Provider=Microsoft.Jet.OLEDB.4.0;Data...
7
2315
by: =?Utf-8?B?SmVmZiBCZWVt?= | last post by:
The default paging behavior of the gridview doesn't work well with very large sets of data which means we have to implement some sort of custom paging. The examples I have seen (4guysfromrolla,...
2
1930
by: occyink | last post by:
I run a website selling cartridges. My stock on the website is in a MySql database. My accounts package and invoicing software runs on an Access database. In my website databse there is a table...
16
1969
by: webandwe | last post by:
Hi, I need to store database data in variables so I can work with it. Please se below what I am trying to do in my script.... If you echo the stuff out is work fine. Meaning if I go echo...
0
977
by: joeller | last post by:
I need to dynamically load controls and access the methods and property of the control from the page. I need to load a different control depending on a selection made by the user. I have buttons on...
8
4585
by: lion cave | last post by:
Hi hello, I have a task assignment regarding for the dynamic fields. My task is to create a table in postgresql database and make the fields of this table a dynamic. This means that the table...
12
7087
by: Xicer | last post by:
Hello everyone, this is my first post here so I will try to make it as clear as possible. Firstly i have a database with a single table named "albums" with the following fields: ...
0
7221
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7109
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7481
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3190
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1537
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.