title: Form text field variable is showing undefined in Javascript
body: <p>This is my first post, anyway I'll get straight to the point.</p>
  
  <p>I have a form that when a user writes they're name in, Javascript saves the value to the global variable "ch_name". When I access this variable through an alert at any point on the site it always shows the correct value that I entered. However, it only works for alerts, when I try reference the user by using the variable it always shows as undefined.</p>
  
  <p>I've tried re-ordering the code in case it hadn't loaded correctly and this hasn't yielded any results either. I've begun wondering whether I've referenced it right at all as I'm still fairly new to JavaScript.</p>
  
  <p>As you'll probably find out my code is a mess but here's what I've got.</p>
  
  <pre><code>var ch_name; //my global variable
  
  var data = new Array(); //I've taken a snippet out of an array of 8.
  data[0] = '&lt;p&gt;Question 1&lt;/p&gt;' + ch_name + "\n" + //ch_name is referenced on this line.
  '&lt;button onclick="results1()"&gt;Click Me&lt;/button&gt;' + "\n" + 
  '&lt;button onclick="results2()"&gt;Click Me&lt;/button&gt;' + "\n" +
  '&lt;button onclick="results3()"&gt;Click Me&lt;/button&gt;';
  
  function charName(form) { //This is the code that changes the global variable ch_name.
  ch_name = document.nameform.user.value;
  ranData = Math.floor(Math.random() * data.length);
  document.getElementById("placeholder").innerHTML = data[ranData];
  }
  </code></pre>
  
  <p>The html snippet:</p>
  
  <pre><code>&lt;form NAME="nameform" method="GET"&gt;
  &lt;p&gt;Enter your character name:&lt;/p&gt; &lt;input type="text" name="user" value="" /&gt;
      &lt;input type="button" value="Start" onClick="charName(this.form)"/&gt;
  &lt;/form&gt;
  &lt;div id="placeholder"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>Referencing the ch_name through an alert anywhere gives me the correct result, blank on page load and after the string is entered it gives the correct name. But anytime I reference it through the array variable it shows an undefined. This is not the full code but I'm certain there's no conflicts in variable names etc. I've scanned it a good few times, I'm certain it's just me being an idiot.</p>
  
  <p>Is there a work around for this? I'm in a place ready to drop the whole code behind ch_name entirely to focus on other areas of the page.</p>
  
  <p>Thanks for reading and any help.</p>