Jump to content

Dynamically create input fields and insert after submit


Yohanne

Recommended Posts

Hi coders,

 

i create Dynamically input fields, kindly assist, what going wrong with this.. the problem is cannot save. please.. 

<html>
<head>
<title> Dynamically create input fields </title>
<script  src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>        
<script type="text/javascript">
$(function() 
	{
	var addDiv = $('#addinput');
	var i = $('#addinput p').size() + 1;
		$('#addNew').live('click', function() 
			{
				$('<p><input type="text" id="p_new" size="20" name="p_new_' + i +'" value="" placeholder="I am New" /><a href="#" id="remNew">Remove</a> </p>').appendTo(addDiv);
				i++;
					return false;
			});

$('#remNew').live('click', function() 
	{
		if( i > 2 )
		 {
			$(this).parents('p').remove();
			i--;
		 }
			return false;
	});
	});
</script>
</head>
<body>
<div id="addinput">
	<?php
		require_once 'database/connection.php';
			$DBcon = new connection();
	?>
<p>
	<form name ="form" action = "" method = "POST">
	<input type="text" id="p_new" size="20" name="p_dnew" value="" placeholder="Input Value" /><a href="#" id="addNew">Add</a>
	<input type="submit" size="20" name="Save" value="Save"/>
	</form>
</p>
	<?php
		if($_SERVER['REQUEST_METHOD'] == 'POST')
			{
				$p_dnew = implode(',',($_POST['p_dnew']); 
				$query = ("INSERT INTO branch(brach_name) VALUES('{$p_dnew}')");
				
					$result = mysql_query($query);
			}
		else
			{
				return false;
			}
		
	?>
</div>
</body>
</html>

Link to comment
Share on other sites

I don't get the use of the implode function on an input of 1 value.  All this is going to do is put a comma(?) after that value and then you will pass that to the query.

 

If you had used a test on the query results you would have probably been shown a mysqli error message saying your query is bad.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.