title: Send variable as ajax response
body: <p>I'm posting a form by ajax and printing response in the form page.</p>
  
  <p><strong>Index.php:</strong></p>
  
  <pre><code>&lt;script src="jquery.min.js"&gt;&lt;/script&gt;
  &lt;script src="jquery.form.min.js"&gt;&lt;/script&gt; 
  &lt;script type="text/javascript"&gt;
      function a(){alert('ok');
  $(function(){ 
      $('#submitForm').ajaxForm(function(result){
                      alert('the form was successfully posted!');
                      document.getElementById('resulte').innerHTML=result;
                  }).submit();
           });
           }
  &lt;/script&gt;
      &lt;body&gt;
      &lt;form id="submitForm" name="submitForm" action="upload.php" method="post" enctype="multipart/form-data" &gt;
      File: &lt;input name="myfile" type="file" /&gt;
      &lt;input type="button" id="submitBtn" name="submitBtn" value="Upload" onclick="a();" /&gt;
      &lt;/form&gt;
      &lt;div id="resulte" name="resulte"&gt;a&lt;/div&gt;
      &lt;span id="usererr" name="usererr" class="formerror"&gt;&lt;/span&gt;
      &lt;span id="passerr" name="passerr" class="formerror"&gt;&lt;/span&gt;
      &lt;span id="passconferr" name="passconferr" class="formerror"&gt;&lt;/span&gt;
      &lt;/body&gt;
  </code></pre>
  
  <p><strong>upload.php:</strong></p>
  
  <pre><code>&lt;?php 
  $result = 0; 
  $target_path = 'uploads/' . basename( $_FILES['myfile']['name']); 
  if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
  $result = 1;
  } 
  sleep(1);
  echo"good";
  $arr=array('username'=&gt;array('isEmpty'=&gt;'required','exists'=&gt;'username exists'),'password'=&gt;array('isEmpty'=&gt;'required'))
  ?&gt;
  </code></pre>
  
  <p>I want to send $arr to ‘index.php’ by ajax and show the values in ‘usererr’ and ‘passerr’ spans</p>
  
  <p><strong>OR</strong></p>
  
  <p>echo each line in different spans in ‘index.php’. How can I do that ?</p>
  
  <p>-thanks.</p>
  
  <p><em><strong>Edit:</em></strong></p>
  
  <p>I did the following but now no text is printing there in ‘usererr’ span and the alert box not displaying.whats wrong did I do ? (form is posted and file saved successfully, but the problem is about 'response')</p>
  
  <p><strong>Index.php:</strong></p>
  
  <pre><code>&lt;script src="jquery.min.js"&gt;&lt;/script&gt;
  &lt;script src="jquery.form.min.js"&gt;&lt;/script&gt; 
  &lt;script type="text/javascript"&gt;
  function a(){ 
      $('#submitForm').ajaxForm({
          dataType: 'json',
          success: function(result){        
                      alert('the form was successfully posted!');
                $('#usererr').html(result['username']['isEmpty']);
                  }}).submit();
           }&lt;/script&gt;
  
      &lt;body&gt;
      &lt;form id="submitForm" name="submitForm" action="upload.php" method="post" enctype="multipart/form-data" &gt;
      File: &lt;input name="myfile" type="file" /&gt;
      &lt;input type="button" id="submitBtn" name="submitBtn" value="Upload" onclick="a();" /&gt;
      &lt;/form&gt;
      &lt;div id="resulte" name="resulte"&gt;a&lt;/div&gt;
      &lt;span id="usererr" name="usererr" class="formerror"&gt;&lt;/span&gt;
      &lt;span id="passerr" name="passerr" class="formerror"&gt;&lt;/span&gt;
      &lt;span id="passconferr" name="passconferr" class="formerror"&gt;&lt;/span&gt;
      &lt;/body&gt;
  </code></pre>
  
  <p><strong>upload.php:</strong></p>
  
  <pre><code>&lt;?php 
  $result = 0; 
  $target_path = 'uploads/' . basename( $_FILES['myfile']['name']); 
  if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
  $result = 1;
  }
  $arr=array('username'=&gt;array('isEmpty'=&gt;'required','exists'=&gt;'username exists'),'password'=&gt;array('isEmpty'=&gt;'required'));
  echo json_encode($arr);
  ?&gt;
  </code></pre>