0

I have two text boxes (textbox1 and textbox2) and a csv file. Textbox1 corresponds to column 0 in the csv and similarly for textbox2 to column 1. I want to type a letter into textbox1, have it search through column 0 for a match of the first letter of that field and then once it finds a match, populates the corresponding value in textbox2. For example:

Col0, Col1 Dan, Smith Andrew, White Matt, Stevens

If I type a "d" in textbox1 it should populate textbox2 with "Smith"

Here is the code I have so far:

var textBox = document.getElementById("firstName");

var textBox2 = document.getElementById("lastName");

var theValue = textBox2.value;

for(var i=0;i<15;i++)

{
   (open php tag) $a++; (close php tag)

   for(var j=0;j<3;j++)

   {

   theArray[i][j]=

   (open php tag)

      $fp = fopen('data.csv', 'r');

      $data = array();

      $b++;

      while($row = fgetcsv($fp))

         $data[] = $row;

      echo $data[$a][$b];

   (close php tag)

   if(theArray[0][j].substring(0,1)==theValue)

      textBox.value = theArray[1][j];
}

Something tells me incrementing $a in a separate php tag wont talk with the other php tag. Yes/no?

Any help with this getting this code working would be much appreciated. Thanks in advance.

Machavity
  • 30,841
  • 27
  • 92
  • 100
user2382321
  • 105
  • 1
  • 10

1 Answers1

0

I'm looking at your code and I don't see any AJAX, URL requests, or form submits in the JavaScript. That being said, I don't see how your JavaScript will pass values to PHP because the JavaScript is running in the client's browser and the PHP is executing on the server.

How to pass JavaScript variables to PHP?

Community
  • 1
  • 1
Alex W
  • 37,233
  • 13
  • 109
  • 109
  • So I have to post the values? – user2382321 Nov 19 '13 at 04:00
  • @user2382321 You have to send the JavaScript values to the server somehow, or PHP can't access them. – Alex W Nov 19 '13 at 14:20
  • K I've played around with it a little more. I am not actually passing any js variables to php, I'm only doing the reverse, but my code is crashing because for some reason it won't let me iterate $a and $b. If I set them equal to a value the code works. Any ideas? – user2382321 Nov 19 '13 at 16:01
  • @user2382321 When you do `$a++` you are only iterating the variable once, because there is no PHP loop (executing on server), only a JavaScript loop (executing in client browser). – Alex W Nov 19 '13 at 16:29