i am using the php to get all the values from textbox, inside a loop i am getting the values.
inside a loop i also have the insert statement and the count.
The flow is: i will select the value from drop-down list, when i select the number from the drop down list, the corresponding number of text-boxes will be created.
for ex:- i have selected the number 3 from drop-down list. so it will create 3 text-boxes.
now i will enter data into those 3 text-boxes and click submit. when i click submit only the value of last text-box is getting inserted into database.
i.e. when i select 3 and enter data to 3 text-box it is inserting only 3rd text-box value 3 times, instead of inserting 1st,2nd and 3rd value..
how can i solve it..?
here is code i have used..
php:
<?php
mysql_connect("localhost", "tiger", "tiger") or die(mysql_error());
mysql_select_db("theaterdb") or die(mysql_error());
for ($i=0; $i < $_POST["range"] ; $i++)
{
$query = mysql_query("INSERT INTO movie (movie_name,language) VALUES('$_POST[Fname]','$_POST[language]') ") or die(mysql_error());
}
?>
range is a value what i have selected in dropdown.
javascript code:
for(i = 0; i < param; i += 1) {
target.innerHTML +='</br>';
target.innerHTML +='New Movie '+i+' ';
target.innerHTML += '<input type="text" name="Fname">';
}
this is generating the textbox dynamically
the problem is that you are creating new input with the same name as previously created ones, php's $_POST[] goes on the inputs name to get arround this you will need to create inputs with unique names something like
for(i = 0; i < param; i += 1) {
target.innerHTML +='</br>';
target.innerHTML +='New Movie '+i+' ';
target.innerHTML += '<input type="text" name="Fname_' + i + '">';
}
<?php
mysql_connect("localhost", "tiger", "tiger") or die(mysql_error());
mysql_select_db("theaterdb") or die(mysql_error());
for ($i=0; $i < $_POST["range"] ; $i++)
{
$query = mysql_query("INSERT INTO movie (movie_name,language) VALUES('" . $_POST['Fname_' . $i] . "','" . $_POST['language'] . "') ") or die(mysql_error());
}
?>
this will however only fork for adding inputs once or you will create inputs with identicle names again, to get arround this i would reccomend storing the total count in a hidden input called count <input type="hidden" value="0" name="count" />
the basing the names off of that count
var count = $('input[name=count]');
for(i = 0+count; i < param+count; i += 1) {
target.innerHTML +='</br>';
target.innerHTML +='New Movie '+i+' ';
target.innerHTML += '<input type="text" name="Fname">';
}
<?php
mysql_connect("localhost", "tiger", "tiger") or die(mysql_error());
mysql_select_db("theaterdb") or die(mysql_error());
for ($i=0; $i < $_POST["range"] ; $i++)
{
$query = mysql_query("INSERT INTO movie (movie_name,language) VALUES('" . $_POST['Fname_' . $i] . "','" . $_POST['language'] . "') ") or die(mysql_error());
}
?>
i havnt tested this code so you might need to tinker a bit to make it work but the concept is there
try
<?php
mysql_connect("localhost", "tiger", "tiger") or die(mysql_error());
mysql_select_db("theaterdb") or die(mysql_error());
for ($i=0; $i <= count($_POST["Fname"]); $i++)
{
$query = mysql_query("INSERT INTO movie (movie_name,language) VALUES('$_POST[Fname][$i]','$_POST[language][$i]') ") or die(mysql_error());
}
?>
You are using $_POST["Fname"] for the insert. So instead you should use count($_POST["Fname"]) in the for statement
change your code to:
for(i = 0; i < param; i += 1) {
target.innerHTML +='</br>';
target.innerHTML +='New Movie '+i+' ';
target.innerHTML += '<input type="text" name="Fname[]">';
}
php automatically translates Fname[] into an array() so you can loop through your movie name with:
<?php
mysql_connect("localhost", "tiger", "tiger") or die(mysql_error());
mysql_select_db("theaterdb") or die(mysql_error());
for ($i=0; $i < $_POST["range"] ; $i++) {
$query = mysql_query("INSERT INTO movie (movie_name,language) VALUES('{$_POST[Fname][i]}','{$_POST[language]}') ") or die(mysql_error());
}
?>
When you are posting a form to server, All inputs in form are sent to server with their name attribute. In your case you have specified input name as 'Fname'. Hence only one input with name 'Fname' is sent to server. So you are getting only one post input i.e. $_SERVER["Fname"].
Just change you javascript code to
for(i = 0; i < param; i += 1) {
target.innerHTML +='</br>';
target.innerHTML +='New Movie '+i+' ';
target.innerHTML += '<input type="text" name="Fname[]">';
}
use Fname[] instead of Fname. Now you are posting array of Fname. So you will get array in $_SERVER["Fname"]. Here you will receive all Fname input field in array. So change your php code to
<?php
mysql_connect("localhost", "tiger", "tiger") or die(mysql_error());
mysql_select_db("theaterdb") or die(mysql_error());
for ($i=0; $i < $_POST["range"] ; $i++)
{
$query = mysql_query("INSERT INTO movie (movie_name,language) VALUES('$_POST[Fname][$i]','$_POST[language]') ") or die(mysql_error());
}
?>
This will solve your problem.