473,609 Members | 2,187 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 2457
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="saveda ta();" example <input type="submit" name="saveme" id="saveme" value="Save me" onclick="saveda ta();"> and take note put your form id to getrequiredfiel ds("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 getrequiredfiel ds("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 getrequiredfiel ds() function, i made updates there.
in savedata() function in the line with getrequiredfiel ds("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

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

Similar topics

6
1951
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 appreciate if someone can point me to the right direction. Thanks
3
1277
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 going to walk through each one to update the DB. How do I access these form fields from the codebehind? I know I can put a RUNAT="SERVER" on this and then I could register it in the codebehind, but I can't predict how many INPUTS I'll need on the...
6
1175
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 source=Customers.mdb;" />
7
2322
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, etc.) suggest using an ObjectDataSource which has built-in paging functionality that, when used in conjunction with certain SQL 2005 functionality, only works with the records to be displayed on the page rather than the entire set. The problem with...
2
1933
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 called products, on my accounts Access database there is a table called items. I am trying to link certain fields in MySql database with certain fields in Access database. The reason for this is so when I update the price in the accounts database (access)...
16
1981
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 $row; I get the data I want but I can't get the data to store in a vairable. I need something that goes from $a1 to $a18 and in each of then store the data from one field in the database name a5. Thanks
0
982
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 the page whose click events call methods on the controls that process the data selected by the user. I thought I could register one control on the page then call usercontrol.LoadControl(path) to redirect the registered control to a different ascx...
8
4594
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 can be added, renamed and deleted the fields. On ther hand, the datatype of field can be changed also. One that come up to my mind to solve this through procedural language for the PostgreSQL. But I'm a newbee for this. Well, I'm studying this...
12
7108
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: albumid int(11) genreid varchar(30) albumtitle varchar(30) albumauthor varchar(30 numberofsongs int(11 price int(11)
0
8109
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8035
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8534
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8188
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8374
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6969
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6034
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5502
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4002
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.