Page 1 of 1
PHP and javascript
Posted: Wed Nov 23, 2005 2:26 pm
by taha
I have a method coded in javascript, which dynamically adds new textfields when the tab key is pressed.
On the php side, when the user presses the submit button, i need to loop through these textfields...is there any way i can do that? my question is will the php code know that i'm referring to the javascript code??
Posted: Wed Nov 23, 2005 4:16 pm
by Grim...
I assume the text fields have names like "text1" and "text2" etc.
How about you use javascript to change the value of a hidden field to the total of text files, then use PHP to loop through using the variable variable method. Example:
Code: Select all
for ($i = 1; $i <= $total_text_fields; $i++)
{
$v = "text".$i;
$value = $$v; // this is the value in the text box.
}
Posted: Wed Nov 23, 2005 10:46 pm
by harrisonad
set the name of your textfields to the same name prefixed with "[]" to submit it as array.
something like
Code: Select all
...
<input type='text' name='input[]'>
<input type='text' name='input[]'>
<input type='text' name='input[]'>
<input type='text' name='input[]'>
...
That would be retrieve as an array of txtfields
Code: Select all
$inputs = $_POST['input'];
// $input is now an array with the values of the submitted textfields
try it.
Posted: Thu Nov 24, 2005 8:17 am
by taha
Thanks guys.
I've decided to do it in javascript instead, so i'm going to explain in more detail what i need to do, please help if you can.
Below is the method that i have that creates the dynamic textfields on tab press.
Code: Select all
var numItems = 1; // Number of Items on page
function addNewItem()
{
// Increase the number of items
numItems++;
// Add the rows to the Inner HTML of our TD
document.getElementById("textfields").innerHTML += Description <INPUT TYPE="text" NAME="idescription '+numItems+' " VALUE="" > Part Number <INPUT TYPE="text" NAME="ipartno '+numItems+' " VALUE="" ONKEYDOWN="if (event.keyCode==9) { addNewItem(); }">;
}
now on submit, i need to loop through the x amount of textfields, extract their contents and load them into the sql database.
how would i do that???
Posted: Thu Nov 24, 2005 8:49 am
by Grim...
Pretty much what I said earlier, right?
Posted: Thu Nov 24, 2005 9:55 am
by taha
Yes thank you so much.
Could you help me with this 2 diminsional array, i'm not sure if this is correct
Code: Select all
function submitSave()
{
$iarray = array();
for (i=0; i<numItems; i++)
{
// Item Description & Part No
$iDesV = "idescription".$i;
$iDescriptionValue = $$iDesV; // this is the value in the text box.
$iPnoV = "ipartno".$i;
$iPartnoValue = $$iPnoV; // this is the value in the text box.
$iarray[$i] = array($i => $iDescriptionValue, $iPartnoValue);
}
}
Posted: Thu Nov 24, 2005 11:38 am
by Grim...
Could if I wasn't crap at arrays... Anyone else?
Posted: Thu Nov 24, 2005 12:10 pm
by taha
Actually i'm more concerned with javascript syntax.
Grim, you know how you provided me with this code:
Code: Select all
for ($i = 1; $i <= $total_text_fields; $i++)
{
$v = "text".$i;
$value = $$v; // this is the value in the text box.
}
how would i convert that to javascript...i know that javascript doesn't include the $, but can i still do this:
$v = "text".$i;
please advise