PHP and javascript

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
taha
Forum Newbie
Posts: 17
Joined: Tue Nov 22, 2005 2:40 pm

PHP and javascript

Post 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??
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post 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.
}
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post 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.
taha
Forum Newbie
Posts: 17
Joined: Tue Nov 22, 2005 2:40 pm

Post 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???
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Pretty much what I said earlier, right?
taha
Forum Newbie
Posts: 17
Joined: Tue Nov 22, 2005 2:40 pm

Post 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);

      }
  }
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Could if I wasn't crap at arrays... Anyone else?
taha
Forum Newbie
Posts: 17
Joined: Tue Nov 22, 2005 2:40 pm

Post 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
Post Reply