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>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.