title: Using ajax to return php code on the same page through a text link
body: <p>When clicked on, a link should run PHP code that pulls info from a database on the same page. </p>
  
  <p>I have achieved this using AJAX, a form and a button but really wish to use a link instead of button.</p>
  
  <p>I have managed to get the link to work but the table from the database that the PHP script creates only flashes up for a second as the browser is refreshed.</p>
  
  <p>How do I stop the refresh so the table is displayed? </p>
  
  <p>I would be very grateful for any help.</p>
  
  <p>My code is:</p>
  
  <pre><code>    &lt;script language="javascript" type="text/javascript"&gt;
  
      function myFunction(){
      var ajaxRequest;  
  
  try{
      // Opera 8.0+, Firefox, Safari
      ajaxRequest = new XMLHttpRequest();
  } catch (e){
      // Internet Explorer Browsers
      try{
          ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
          try{
              ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (e){
              // Something went wrong
              alert("Your browser has Failed");
              return false;
          }
      }
  }
  
  ajaxRequest.onreadystatechange = function(){
      if(ajaxRequest.readyState == 4){
          var ajaxDisplay = document.getElementById('ajaxDiv1');
          ajaxDisplay.innerHTML = ajaxRequest.responseText;
      }
  }
  
      document.proffForm.submit();
  ajaxRequest.open("GET", "proff/all_proff.php");
  ajaxRequest.send(null); 
  
  
      }
  
      &lt;/script&gt;
  
      &lt;form name='proffForm' &gt;
      &lt;a href="#" onclick='myFunction()' &gt;List All Professions&lt;/a&gt;
      &lt;/form&gt;
  
      &lt;div id='ajaxDiv1'&gt;&lt;/div&gt;&lt;/p&gt;
  </code></pre>
  
  <p>php</p>
  
  <pre><code>     &lt;?php 
       include '../dbc.php';
  
  
  
       $allprof1 = mysql_query("SELECT *  
                      FROM users 
                      ORDER BY profession" );
  
      $qry_result = mysql_query($allprof1) or die(mysql_error());
      ?&gt;
      &lt;?php     
      $display_string = "&lt;table border='1' cellpadding='5' width='100%'               bordercolor='000099'border='solid'&gt;";
      $display_string .= "&lt;tr align='left' bgcolor='009966'&gt;";
      $display_string .= "&lt;th&gt;&lt;span class='titlehdr3'&gt;Profession&lt;/span&gt;&lt;/th&gt;";
      $display_string .= "&lt;th&gt;&lt;span class='titlehdr3'&gt;Business Name&lt;/span&gt;&lt;/th&gt;";
     $display_string .= "&lt;th&gt;&lt;span class='titlehdr3'&gt;Profile&lt;/span&gt;&lt;/th&gt;";
     $display_string .= "&lt;/tr&gt;";
  
  
     while($row = mysql_fetch_array($qry_result)){
  $display_string .= "&lt;tr&gt;";
  $display_string .= "&lt;td&gt;&lt;span class='tabs'&gt;$row[profession]&lt;/span&gt;&lt;/td&gt;";
  $display_string .= "&lt;td&gt;&lt;span class='tabs'&gt;$row[full_name]&lt;/span&gt;&lt;/td&gt;";
  $display_string .= "&lt;td&gt;&lt;span class='tabs'&gt;$row[url]&lt;/span&gt;&lt;/td&gt;";
  $display_string .= "&lt;/tr&gt;";
  
     }
  
     $display_string .= "&lt;/table&gt;";
     echo $display_string;
  
     ?&gt; 
  </code></pre>