title: javascript validate form values from database
body: <p>The below code is to create a campaign. Before creation, I have to validate the form. I have to validate the campaign name which is already existed in database or not. I don't know whether I can use PHP code inside javascript (like below).Anyway it's not working. How can I change my code? How can I validate values with database values?</p>
  
  <p>$this->campaign is an array which contain all campaign details from database.</p>
  
  <pre><code>&lt;script type="text/JavaScript"&gt;
  function validate()
  {
  var name = document.getElementById('name').value;
  var shape = document.getElementById('shape').value;
       &lt;?
       foreach($this-&gt;campaign as $c)
       {
       $old_cname=$c['name'];
       ?&gt;
          if(name==&lt;?=$old_cname;?&gt;)
          {
              alert("same name exists in database. Try again!");
          }
      &lt;?
      }
      ?&gt;
  
  if(!name)
  {
      alert("Please enter a name!");
  
      return false;
  }
  
  if(!shape)
  {
      alert("Please select shape!");
      return false;
  }
      return true;
  
  
  }
  &lt;/script&gt;
  
  
  &lt;form action="create.php" method="post" onsubmit="return(validate());"&gt;
  Name:
  &lt;input type="text" name="name" id="name"/&gt;
  Shape:
  &lt;select name="shape" id="shape"&gt;
  &lt;option value="long"&gt;Long&lt;/option&gt;
  &lt;option value="tall"&gt;Tall&lt;/option&gt;
  &lt;/select&gt;
  &lt;input type="submit" value="Create" name="submit"/&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Thanks!</p>