Aditya Kumar
Aditya Kumar

Reputation: 793

JavaScript onchange()

i am new to JS and Programming..

i want to learn How to Implement onchange() in a text field in a form and capturing the value of that form field if the value is changed and i want to store that information in a text file, which i can create in php..i already have the text file in which i can use fopen and insert the value. but how to capture and store in some temp variable before writing it to text file.

i am including the code here..

This is from a update page which i am using to update this data...

thanx in advance..

//function for multiple select field 
<script>
function Technology(value,state)
{
   if (value > -1) {
      var input= new Array(value);
      var p;

      var techs = [

         <?php
            $con = mysql_connect("localhost", "root");
            if (!$con)
            {
               die('Could not connect: ' . mysql_error());
            }
            mysql_select_db("root", $con);

            $load = mysql_query("SELECT tech from t_tech");
            while($arr = mysql_fetch_array($load))
            {
               echo "'".$arr[0]."',";
            }
         ?>
      ].sort();
      var ele = document.getElementById("tech");

      if(ele.hasChildNodes())
      {
         var len = ele.childNodes.length;
         while(ele.childNodes.length - value > 1)
         {
            ele.removeChild(ele.lastChild);
         }
      }
      var tech_array = new Array;
      <?php
         for($i = 0;$i<$_POST['tech_num'];$i++)
         {
            $p = $i+1;
            echo "tech_array[$i] = '".$_POST['tech'.$p]."';";
         }
      ?>

      for (var i=len-1;i<value;i++)
      {
         p= i+1;
         input[i] = document.createElement('div');

         if(state ==1 )
         {
            input[i].innerHTML = 'Technology' + p + ':  &nbsp;&nbsp; <input type="text" name="tech' + p + '" id="tech' + p +'" size = 25 value="'+tech_array[i] +'"/>';
         }
         else if (state == 0)
         {
            input[i].innerHTML = 'Technology' + p + ':  &nbsp;&nbsp; <input type="text" value="" name="tech' + p + '" id="tech' + p +'" size = 25/>';
         } 

         document.getElementById("tech").appendChild(input[i]);

         var techId = 'tech' + p;

         AutoComplete_Create(techId,techs);

      }           
}}
</script>  

This is the form field:

<tr> 
   <td><b>
      Field1: <select name="tech_num" id="tech_num" onChange="return Technology(this.value,0)"> 
      <?php 
         for ($i=0;$i<=5;$i++) { 
            if ($i==0) 
               $temp = "<option value=$i> - </option>"; 
            else if($i == $_POST['tech_num']) 
               $temp = "<option value=$i selected='selected'>$i</option>"; 
            else $temp = "<option value=$i>$i</option>"; 
               echo $temp; } 
      ?> 
      </select>
   </td>
</tr> 
</table> 
<div id="tech"> </div> <table>

Upvotes: 0

Views: 1366

Answers (2)

rxgx
rxgx

Reputation: 5160

I recommend this article from Mozilla about using files in Web applications.

The simplest example they offer includes snippets such as:

// Dynamically adding a change listener
var inputElement = document.getElementById("inputField");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
    var fileList = this.files; /* now you can work with the file list */
}

Note: I'd be cautious with anything you read on W3Schools. Their information is more geared towards SEO than accuracy.

Upvotes: 0

Phil H
Phil H

Reputation: 20131

To store a value in a text file on the server, you will need to send the data from the client. Your best bet is probably to add an ajax call in the onChange function to a special php page which simply stores the value.

http://www.w3schools.com/jsref/event_onchange.asp

http://www.w3schools.com/ajax/default.asp

Upvotes: 1

Related Questions