This has probably been asked a bunch of times , but :
I only recently started to self learn PHP , and this small project i am making is quite a challange for me.
The scripts below are not realy writen by me , but i wished to tie them together to get a fully working code.
What is suppose to happen is :
In the form i wish to put a special button that puts extra input fields in the form.
After the form is submited ,with whatever amount of feilds in it, the data goes to a processing.php file that sorts all the info gathered and finaly sends all this data formated in to the last file that will be inluded in the index.php.
i already have the code for increcing the amount of inputs but the problem is , all inputs have the same names. Coz of that i get a mess.
What i need is each input to have a unique name or id , that way the processing file would receive different names(id's) with different values(what ever is writen in the inputs by users).
for example:
I have 2 enitial fields displayed on form.html , i click the button "add new" 5 times and add 5 more input fields.
All this data must be sent to a process.php and on the output write all of this information formated in the last file in a row that will be displayed in the index by useing
<?include("blablabla.php");?>
The input increment is done by a java script. the problem is sending data with unique id(name) and receiving it.
The form file :
<?include("header.php");?>
<?$i = 1; ?>
<script type="text/javascript">
function validate(frm)
{
var ele = frm.elements['feedurl[]'];
if (! ele.length)
{
alert(ele.value);
}
for(var i=0; i<ele.length; i++)
{
alert(ele[i].value);
}
return true;
}
function add_feed()
{
var div1 = document.createElement('div');
// Get template data
div1.innerHTML = document.getElementById('newlinktpl').innerHTML;
// append to our form, so that template data
//become part of form
document.getElementById('newlink').appendChild(div1);
}
var ct = 1;
function new_link()
{
ct++;
var div1 = document.createElement('div');
div1.id = ct;
// link to delete extended form elements
var delLink = '<div style="text-align:right;margin-right:65px"><a href="javascript:delIt('+ ct +')">Del</a></div>';
div1.innerHTML = document.getElementById('newlinktpl').innerHTML + delLink;
document.getElementById('newlink').appendChild(div1);
}
// function to delete the newly added set of elements
function delIt(eleId)
{
d = document;
var ele = d.getElementById(eleId);
var parentEle = d.getElementById('newlink');
parentEle.removeChild(ele);
}
</script>
<TABLE>
<style>
#newlink {width:600px}
</style>
<form action='sendorder.php' method='post'>
<div id="newlink">
<div>
<table align="center" border=0>
<TR><TD><B>Product:</B> </TD><TD><input type=text name=prodname1 value="<?=$_GET['prodname1'];?>"> </TD>
<TD><B>Price:</B> </TD><TD><input type=text name=price1 value="<?=$_GET['price1'].$curency;?>"><b>%</b></TD></TR>
</table>
</div>
</div>
<p>
<br>
<input type="submit" name="submit1">
<input type="reset" name="reset1">
</p>
<p id="addnew"></p>
<FORM>
<INPUT type="button" value="Add New" name="button2" onClick="javascript:new_link()">
</FORM>
</form>
<!-- Template -->
<div id="newlinktpl" style="display:none">
<div>
<table border=0>
<TR><TD><B>Product:</B> </TD><TD><input type=text name=<?$_POST[++$i];?> value="<?$_GET[$i];?>"> </TD>
<TD><B>Price:</B> </TD><TD><input type=text name=<?$_POST [++$i];?> value="<?$_GET[$i].$curency;?>"> </TD></TR>
</form>
</TABLE>
<?include("footer.php");?>
in the code above i tryed to make the programm POST the numbers in the name increcing it by 1. But when i click submit i get offset error.
The processing file :
<?
$timestamp = strftime("%d-%m-%Y %H:%M:%S %Y"); // for later use (ignore it)
$i =1;
####################################################################################
if(($_POST['prodname']!="")&&($_POST['price']!="")){
############################################################
$writetocsv = $_POST['prodname1'] . "," . $_POST['price1']"%" <BR> . $_POST[$i] . $_POST[$i];
$fp = fopen("data/data.csv", "a");
fwrite ($fp,$writetocsv);
fclose ($fp);
echo "<script>alert('Data sent.')</script>";
}
else{
echo "<script>alert('Error! Please fill the form!')</script>";
}
echo "<script>location.replace('index.php')</script>";
?>
Perhaps someone knows a easyer way , since i am not sure how to make stuff due to the lack of knowlage in php. I also tryed arrays but , as said before , too tough when you are a noob
Please help.