www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 23

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

  1. #1
    Join Date
    Jan 2013
    Posts
    13

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

    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.
    HTML Code:
    	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    	<html xmlns="http://www.w3.org/1999/xhtml">
    	<head>
    	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    	<title>Dynamic Fields js/php to MySql need to submit dynamically to the database</title>
    	<?php
    	require ('database.php'); 
    	?>
    	 <script type="text/javascript">
    	 var counter     =     1;
    	 var collector     =     "";
    	 
    	 function addfields(indx)
    	 {
    		 var tbl = document.getElementById('table_id');
    		 var newtr = document.createElement('tr');
    		 counter = counter + indx;
    	 
    		 newtr.setAttribute('id','tr'+counter);
    	 
    		 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>';
    	 
    		 tbl.appendChild(newtr);
    	 }
    	 
    	 function checkme(dx)
    	{
    		  collector += dx+",";
    	}
    	 
    	 function deletetherow(indx)
    	 {
    		 var col = collector.split(",");
    	 
    		 for (var i = 0; i < col.length; i++) 
    		 {
    			 var remvelem = document.getElementById('tr'+col[i]);
    			 var chckbx = document.getElementById("checkb"+col[i]);
    			 if(remvelem && chckbx.checked)
    			 {
    				 var tbl = document.getElementById('table_id');
    				tbl.removeChild(remvelem);
    			}
    		 }
    	 }
    	</script>
    	</head>
    	<body>
    	<form enctype="multipart/form-data" id="1" style="background-color:#ffffff;" action="<?php echo $_SERVER['PHP_SELF']; ?>"></form> 
    	<table id="table_id" >
    	  <tr id="tr1" class="trmain">
    	  <td>
    	  </td>
    	   <td>
    	   <input type="text" name="text1[]">
    		</td>
    		  <td>
    			<textarea name="textarea1[]"></textarea>
    		   </td>  
    		 </tr>
    	 </table>
    	<input type="button" value="Add" onClick="addfields(1);" />&nbsp;
    	<input type="button" value="Delete" onClick="deletetherow()" />
    	<input type="submit" value="Send" id="submit"  name="submit"/>
    	
    	<?php
    	if(isset($_POST['submit'])) {
    	for ($i=0; $i < count($_POST['text1']); $i++ )
    	{
    	$ced = stripslashes($_POST['text1'][$i]);
    	$erg = stripslashes($_POST['textarea1'][$i]);
    	}
    	
    	
    	$bnt = mysql_query("INSERT INTO tablename (first, second) VALUES ('$ced', '$erg')")or die('Error: '. mysql_error() );
    	$result = mysql_query($bnt);
    	}
    	
    	?>
    	 
    	</body>
    	</html>
    Last edited by jedaisoul; 08-23-2014 at 06:00 PM. Reason: code tags added

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    898
    Uhhh.....
    What is the default method for a form tag?

    And you don't have any inputs in your form. You closed it too early.

  3. #3
    Join Date
    Jan 2013
    Posts
    13

    Seriously?

    Quote Originally Posted by ginerjm View Post
    Uhhh.....
    What is the default method for a form tag?

    And you don't have any inputs in your form. You closed it too early.
    OMG This is the best you can do? You think I don't know how to post a form Why on Heaven's Green Earth did you reply to this. The URI is for example purposes if someone run's on their own. I am missing the </form> tag out of mistake from putting the example together, so what. Look, if you do not have the capability or knowledge or the skills don't reply some useless nonsense, you make yourself look asinine, really. I will ask a moderator to remove you, from lack of purposeful use.

  4. #4
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    898
    I make myself look asinine? You will not get much help from this forum with that attitude. In fact you may very well get yourself removed.

    My question was to help you recognize your OWN mistake in your code. A <form> tag's default method= is 'GET'. Not POST. But you - in your infinite wisdom - are referencing the POST array in your php code so you will not get anything at all there. Plus - you closed your form before you included any input fields so whether you reference GET or POST you won't see anything at all.

    I"d like to say HTH, but I'm not really. You're just having a bad day and are too immature to recognize it so I'm just being nice in order to give you some satisfaction in your life today.

  5. #5
    Join Date
    Jan 2013
    Posts
    13
    Quote Originally Posted by ginerjm View Post
    I make myself look asinine? You will not get much help from this forum with that attitude. In fact you may very well get yourself removed.

    My question was to help you recognize your OWN mistake in your code. A <form> tag's default method= is 'GET'. Not POST. But you - in your infinite wisdom - are referencing the POST array in your php code so you will not get anything at all there. Plus - you closed your form before you included any input fields so whether you reference GET or POST you won't see anything at all.

    I"d like to say HTH, but I'm not really. You're just having a bad day and are too immature to recognize it so I'm just being nice in order to give you some satisfaction in your life today.
    Firstly, to comment I have infinite wisdom, thanks but if that were the case I wouldn't be here seeking help. Secondly, your first reply is absolutely cut short of comprehension. Why don't you go ahead then, produce a actual working example of the code then and when you do, I shall retract from my statements. I get tired of people replying some whipped up stuff like this; the answer, but is pure rubbish. We'll go ahead now, I am waiting for your infinite wisdom to provide the working original code in dynamic DOM to MySQL

    I should point out that GET was not used as to the content delivery and wanting the data to be non cached or exploited by others for data privacy. hahaha you tell me I will be removed as a user when you aren't supplying anything, not even proper code on a coding forum, hahaha

  6. #6
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    898
    You have written your code and with some minor corrections it will begin to show you some progress. I told you what was wrong with it - fix it and try to run it again. Do a var_dump on the $_POST array to see that you are getting all the input values you expect from the client.

    This is how it works on forums such as this.

  7. #7
    Join Date
    Jan 2013
    Posts
    13
    Quote Originally Posted by ginerjm View Post
    You have written your code and with some minor corrections it will begin to show you some progress. I told you what was wrong with it - fix it and try to run it again. Do a var_dump on the $_POST array to see that you are getting all the input values you expect from the client.

    This is how it works on forums such as this.
    Thank you for your help so far. I actually do not get any result of the var_dump. I have moved the insert into statement inside the loop you should be advised. There doesn't seem anything going to var_dump or print_r . I have similar code that works fine for the same thing, but I really don't see where its' not firing here or if need some trigger function or explode, either way it has been back and forth on my end Your proving to be real help now, not just random post and leave....

  8. #8
    Join Date
    Mar 2012
    Posts
    1,838
    By my reading of your code, ginerjm is spot on:
    HTML Code:
    	<form enctype="multipart/form-data" id="1" style="background-color:#ffffff;" action="<?php echo $_SERVER['PHP_SELF']; ?>">[b]</form>[/b] 
    a) Has no method=POST, so defaults to GET.
    b) The </form> terminates the form prematurely.

    These errors mean that the subsequent code:

    HTML Code:
    	<input type="button" value="Add" onClick="addfields(1);" />*
    	<input type="button" value="Delete" onClick="deletetherow()" />
    	<input type="submit" value="Send" id="submit"  name="submit"/>
    and...

    Code:
    	if(isset($_POST['submit'])) {
    ...have no effect. The correction of these errors merely requires the addition of "method=POST" to the <form> tag, and the </form> moving to just before </body>.

    Does that help?

  9. #9
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    898


    With the help of jediasoul you should be well on your way to fixing your dilemma. Too bad he spoiled your chance at actually doing some learning.

    Good luck!

    BTW - Apology accepted!

  10. #10
    Join Date
    Jan 2013
    Posts
    13
    Quote Originally Posted by CRMBOX View Post
    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.
    HTML Code:
    	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    	<html xmlns="http://www.w3.org/1999/xhtml">
    	<head>
    	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    	<title>Dynamic Fields js/php to MySql need to submit dynamically to the database</title>
    	<?php
    	require ('database.php'); 
    	?>
    	 <script type="text/javascript">
    	 var counter     =     1;
    	 var collector     =     "";
    	 
    	 function addfields(indx)
    	 {
    		 var tbl = document.getElementById('table_id');
    		 var newtr = document.createElement('tr');
    		 counter = counter + indx;
    	 
    		 newtr.setAttribute('id','tr'+counter);
    	 
    		 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>';
    	 
    		 tbl.appendChild(newtr);
    	 }
    	 
    	 function checkme(dx)
    	{
    		  collector += dx+",";
    	}
    	 
    	 function deletetherow(indx)
    	 {
    		 var col = collector.split(",");
    	 
    		 for (var i = 0; i < col.length; i++) 
    		 {
    			 var remvelem = document.getElementById('tr'+col[i]);
    			 var chckbx = document.getElementById("checkb"+col[i]);
    			 if(remvelem && chckbx.checked)
    			 {
    				 var tbl = document.getElementById('table_id');
    				tbl.removeChild(remvelem);
    			}
    		 }
    	 }
    	</script>
    	</head>
    	<body>
    	<form enctype="multipart/form-data" id="1" style="background-color:#ffffff;" action="<?php echo $_SERVER['PHP_SELF']; ?>"></form> 
    	<table id="table_id" >
    	  <tr id="tr1" class="trmain">
    	  <td>
    	  </td>
    	   <td>
    	   <input type="text" name="text1[]">
    		</td>
    		  <td>
    			<textarea name="textarea1[]"></textarea>
    		   </td>  
    		 </tr>
    	 </table>
    	<input type="button" value="Add" onClick="addfields(1);" />*
    	<input type="button" value="Delete" onClick="deletetherow()" />
    	<input type="submit" value="Send" id="submit"  name="submit"/>
    	
    	<?php
    	if(isset($_POST['submit'])) {
    	for ($i=0; $i < count($_POST['text1']); $i++ )
    	{
    	$ced = stripslashes($_POST['text1'][$i]);
    	$erg = stripslashes($_POST['textarea1'][$i]);
    	}
    	
    	
    	$bnt = mysql_query("INSERT INTO tablename (first, second) VALUES ('$ced', '$erg')")or die('Error: '. mysql_error() );
    	$result = mysql_query($bnt);
    	}
    	
    	?>
    	 
    	</body>
    	</html>
    There are some minor code errors here as I pasted wrong code set, but I can't edit the original post to fix it after submit? So, see below.

  11. #11
    Join Date
    Jan 2013
    Posts
    13
    I have already explained to you that was an error, and as above, posted wrong code, sorry that was mistake on my end, but I couldn't revise it after I posted the code. I am supplying the code again, and to ginerjm it doesn't work.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Dynamic Fields js/php to MySql need to submit dynamically to the database</title>
    <?php
    require ('databaseconnection.php');
    ?>
    <script type="text/javascript">
    var counter = 1;
    var collector = "";

    function addfields(indx)
    {
    var tbl = document.getElementById('table_id');
    var newtr = document.createElement('tr');
    counter = counter + indx;

    newtr.setAttribute('id','tr'+counter);

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

    tbl.appendChild(newtr);
    }

    function checkme(dx)
    {
    collector += dx+",";
    }

    function deletetherow(indx)
    {
    var col = collector.split(",");

    for (var i = 0; i < col.length; i++)
    {
    var remvelem = document.getElementById('tr'+col[i]);
    var chckbx = document.getElementById("checkb"+col[i]);
    if(remvelem && chckbx.checked)
    {
    var tbl = document.getElementById('table_id');
    tbl.removeChild(remvelem);
    }
    }
    }
    </script>
    </head>
    <body>
    <table id="table_id" >
    <form enctype="multipart/form-data" id="1" style="background-color:#ffffff;" action="theformname.php" method="POST">
    <tr id="tr1" class="trmain">
    <td>
    </td>
    <td>
    <input type="text" name="text1[]">
    </td>
    <td>
    <textarea name="textarea1[]"></textarea>
    </td>
    </tr>
    </table>
    <input type="button" value="Add" onClick="addfields(1);" />&nbsp;
    <input type="button" value="Delete" onClick="deletetherow();" />
    <input type="submit" value="Send" id="submit" name="submit"/>
    </form>

    <?php

    if(isset($_POST['submit'])) {
    for ($i=0; $i < count($_POST['text1']); $i++ )
    {
    $ced = stripslashes($_POST['text1[$i]'][$i]);
    $erg = stripslashes($_POST['textarea1[$i]'][$i]);
    $query = mysql_query("INSERT INTO tablename (first, second) VALUES ('$ced', '$erg')")or die('Error: '. mysql_error() );
    }
    }
    ?>

    </body>
    </html>

  12. #12
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    898
    What is this referencing?
    PHP Code:
    $_POST['text1[$i]'][$i
    Your post array has an array entitled 'text1' but what is the second index supposed to accomplish? While surprisingly php doesn't report this as an error, you don't get anything back from this reference.

    Try $_POST[$i] instead.

  13. #13
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    898
    Examining your latest code example you may have a problem with the overlapping of you form and your table elements. Try wrapping the form around the entire table so that new rows get included in the form elements that get sent to the server.

    pseudo code:
    <form>
    <table>
    <tr><td></td><td></td></tr>
    </table
    </form>


    Also - when next you post please use the appropriate code tags to post with. Also - add this to your code:
    PHP Code:

     
    if(isset($_POST['submit'])) 
    {

        echo 
    "POST array is<br>";// new code
        
    var_dump($_POST);  // new code
        
    echo "<br>end of var dump<br>";  // new code

        
    for ($i=0$i count($_POST['text1']); $i++ )
        .... 

  14. #14
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    898
    Here is my attempt at making sense of your code:
    PHP Code:
    <?php
    //******NEW
        // Kinds of errors to show
    error_reporting(E_ALL E_NOTICE);  
       
    // turn on display at the client to display the messages
    ini_set('display_errors''1'); 
    //******MOVED
    require ('databaseconnection.php'); 
    if(isset(
    $_POST['submit'])) 
    {
        for (
    $i=0$i count($_POST['text1']); $i++ )
        {
            
    $ced stripslashes($_POST['text1[$i]']);
            
    $erg stripslashes($_POST['textarea1[$i]']);
            
    $query mysql_query("INSERT INTO tablename (first, second) VALUES ('$ced', '$erg')") or die('Error: ' mysql_error());
    //******NEW
            
    if (!$query)
                
    // show some kind of error message here and handle the error
        
    }
    //******NEW
        
    exit();
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Dynamic Fields js/php to MySql need to submit dynamically to the database</title>
    <script type="text/javascript">
    var counter = 1;
    var collector = "";
    function addfields(indx)
    {
        var tbl = document.getElementById('table_id');
        var newtr = document.createElement('tr');
        counter = counter + indx;
        newtr.setAttribute('id','tr'+counter);
        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>';
        tbl.appendChild(newtr);
    }
    function checkme(dx)
    {
        collector += dx+",";
    }
    function deletetherow(indx)
    {
        var col = collector.split(",");
        for (var i = 0; i < col.length; i++) 
        {
            var remvelem = document.getElementById('tr'+col[i]);
            var chckbx = document.getElementById("checkb"+col[i]);
            if(remvelem && chckbx.checked)
            {
                var tbl = document.getElementById('table_id');
                tbl.removeChild(remvelem);
            }
        }
    }
    </script>
    </head>
    <body>
    <!--*********MOVED-->
    <form enctype="multipart/form-data" id="1" style="background-color:#ffffff;" action="theformname.php" method="POST">
    <table id="table_id" >
    <tr id="tr1" class="trmain">
    <td>
    </td>
    <td>
    <input type="text" name="text1[]">
    </td>
    <td>
    <textarea name="textarea1[]"></textarea>
    </td> 
    </tr>
    </table>
    <input type="button" value="Add" onClick="addfields(1);" />
    &nbsp;&nbsp;
    <input type="button" value="Delete" onClick="deletetherow();" />
    &nbsp;&nbsp;<input type="submit" value="Send" id="submit" name="submit"/>
    <!--**********MOVED-->
    </form>
    <!--**********MOVED-->
    </body>
    </html>
    This seems to work for me (without the query stuff)

  15. #15
    Join Date
    Jan 2013
    Posts
    13
    Quote Originally Posted by ginerjm View Post
    What is this referencing?
    PHP Code:
    $_POST['text1[$i]'][$i
    Your post array has an array entitled 'text1' but what is the second index supposed to accomplish? While surprisingly php doesn't report this as an error, you don't get anything back from this reference.

    Try $_POST[$i] instead.
    Yes, you are right, sorry for that, again was a syntax error from something else I was working with and partially stripped out, I know it's not right. Thanks for pointing that out for the proper post of the array!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles