title: PHP form validate with javascript
body: <p>I've been at this the whole day.  First I tried to validate my form in php but finally gave up.  Now I'm trying to validate the form with JavaScript.  It works but since I don't know JavaScript at all and have limited knowledge of PHP I really need help please.  It is a test and I want to validate that each question has been answered.  The field name in the form = php variable/array, can't get this to work in the javascirpt </p>
  
  <blockquote>
    <p>var x=document.forms["myForm"]["question_{$q_nr}"].value;</p>
  </blockquote>
  
  <p>.  If I just use a text field name it works fine.</p>
  
  <p><strong>The form</strong></p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;?php
  session_start();
  ?&gt;
  &lt;script type="text/javascript" src="counter.js"&gt;&lt;/script&gt;
  &lt;script language="JavaScript"&gt;
  &lt;!--
  function validateForm()
  {
  var x=document.forms["myForm"]["question_{$q_nr}"].value;
  if (x==null || x=="")
    {
    alert("First name must be filled out");
    return false;
    }
  }
  //--&gt;
  &lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;?php
  if ($_SESSION['auth']) {
  $tid = $_GET['tid'];
  echo "&lt;span id='counter'&gt;&lt;/span&gt;";
  $sql1="SELECT * FROM ex_question WHERE test_name = '$tid' ORDER BY q_nr";
  $result1=mysql_query($sql1);
  
  echo "&lt;form name='myForm' action='http://localhost/index.php?option=com_content&amp;view=article&amp;id=51' onsubmit='return validateForm()' method='post'&gt;";
  while($row1 = mysql_fetch_array($result1))
  {
      $q_nr=$row1['q_nr'];
      $q_type=$row1['q_type'];
      $question=$row1['question'];
      $option1=$row1['option1'];
      $option2=$row1['option2'];
  
      echo "&lt;P&gt;&lt;strong&gt;$q_nr $question&lt;/strong&gt;&lt;BR&gt;";
      echo "&lt;img src='images/tests/$pic'&gt;";
      echo "&lt;BR&gt;";
      echo "&lt;BR&gt;";
      echo "&lt;/p&gt;";
  if ($q_type != 'mr') {
  if($option1!="") {
  echo "&lt;input type='radio' name='question_{$q_nr}' value='A'&gt;$option1&lt;BR&gt;";
  } else {
  echo ''; }
  if($option2!="") {
  echo "&lt;input type='radio' name='question_{$q_nr}' value='B'&gt;$option2&lt;BR&gt;";
  } else {
  echo ''; }
  } else { // else if not &lt;&gt; mr
  if($option1!="") {
  echo "&lt;input type='checkbox' name='question_{$q_nr}[]' value='A'&gt;$option1&lt;BR&gt;";
  } else {
  echo ''; } 
  if($option2!="") {
  echo "&lt;input type='checkbox' name='question_{$q_nr}[]' value='B'&gt;$option2&lt;BR&gt;";
  } else {
  echo ''; } 
  } //if q_type &lt;&gt; mr
  } //while row1
  echo "First name: &lt;input type='text' name='fname'&gt;";
  echo "&lt;input type='submit' value='Submit'&gt;";
  echo "&lt;/form&gt;";
  } //else if now &gt; testend
  } //if ses auth
  ?&gt;
  </code></pre>