LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-11-2021, 06:17 PM   #1
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Rep: Reputation: 12
Jquery .submit() not working with Dynamically Created Form


I have a Dynamically Created Form which I can't get to submit.
The Form is displayed on screen and you can enter data just fine but when I press the "save" button nothing happens. How can I adjust my code so that this works?? By the way, his same code with a normal Typed in Form works just fine.

Thanks in advance RP.

Code:
<!--  =======  BUTTON TO SUBMIT FORM  ==================

	 <div class='butpos' >
    	<button type="submit" id='addFormButton' form="addForm" >Save</button>
    	<button type="button" id="bak">Go Back</button>
    </div>
    
// SUBMIT FORM DATA TO DATABASE
	
$("#addForm").submit(function(event) {	
	event.preventDefault();

	var request=$.ajax ({
    	url: "AccountAdd.php",
    	type: "POST",
    	data: $('#addForm').serialize(),
    	dataType: "text"
 	}) 
 	
<!-- =============  DYNAMICALLY CREATE ENTRY FORM  =============== -->

<script type="text/javascript" >

$(document).ready(function () { 

	// get field names from database for use as column headers
 	fieldnames=<?php echo json_encode($fieldNames); ?>;
 	
 	// create form
 	$("<form id='addForm' name='addForm' method='post' action='#'>\n").appendTo('div#form1');  
 	
 	// add 'id' that is auto-increment so don't allow entry
 	 str="<p><label for='"+fieldnames[0]+"'>"+fieldnames[0]+" </label>\n";
    str+="<input class='hov body' id='i1' type='text' readonly name='"+fieldnames[0]+"' value='No Entry' ></p>\n";
    $(str).appendTo('div#form1');
    
   // add remaining elements
 	for(var i=1; i<(fieldnames.length); i++) {
 	 str="<p><label for='"+fieldnames[i]+"'>"+fieldnames[i]+" </label>\n";    
    str+="<input class='hov body' id='i2' type='text' name='"+fieldnames[i]+"' ></p>\n";
    $(str).appendTo('div#form1'); 
 	}

 	// terminate form creation
	$("</form>").appendTo('div#form1');
	
 });
 	
 </script>
 
Old 12-12-2021, 04:23 PM   #2
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
WELL I found another site with a clue and came up with this. It works!

Code:
<!-- =============  DYNAMICALLY CREATE ENTRY FORM  =============== -->

<script type="text/javascript" >

	var str="";

	// get field names from database for use as column headers
 	fieldnames=<?php echo json_encode($fieldNames); ?>;
 	
 	// Create Form
    $('#form1').append("<form action='AccountAdd.php' method='POST'>");
    
    // add controls
	 $("#form1 form").append("<div id='frmcontrols'></div>");
    $('#form1 #frmcontrols').append("<br><input type='submit' class='hov' id='savebutton' value='Insert' />");
    $('#form1 #frmcontrols').append("&nbsp;&nbsp;<button type='button' class='hov' id='bak'>Exit</button><br>");
    
    // add field 'id' which is auto-increment so readonly
    str="<p><label for='"+fieldnames[0]+"'>"+fieldnames[0]+" </label>\n";
    str+="<input class='hov body' id='i1' type='text' readonly name='"+fieldnames[0]+"' value='Auto Increment - No Entry' ></p>\n"; 
    $('#form1 form').append(str);
     
    // add remaining elements
 	for(var i=1; i<(fieldnames.length); i++) {
 		 	str="<p><label for='"+fieldnames[i]+"'>"+fieldnames[i]+" </label>\n";    
    		str+="<input class='hov body' id='i2' type='text' name='"+fieldnames[i]+"' ></p>\n";
    	 $('#form1 form').append(str);
 		}


 $("#bak").click(function () {
 	window.location.href="AccountsFrontEnd.php";	
 });
	
 </script>
 
Old 12-12-2021, 07:27 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
Thank you for returning with your solution.

It would be more helpful to others in future if you would point out the essential differences between your original code and the solution code. The thing that I see in a quick look is the change in the action target of the form element, which makes sense, but was that the change which fixed it for you?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] html form - Unable to have 2 form 'submit' on same line. rblampain Programming 3 11-26-2015 09:57 PM
jquery to dynamically create cookies using the jquery cookie plugin lmcilwain Programming 5 07-27-2011 09:11 PM
php form submit code not working FirstBorn Programming 3 12-27-2008 07:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration