title: Auto filling a form and submitting based on a ajax search answer
body: <p>This problem has been frustrating me for a few days, and as I have found some help here for other problems on my project, it seems that someone could provide insight that I am overlooking.</p>
  
  <p>I admit I am very new to sql, javascript, jquery, ajax, css, and php.  I did 3 years of Computer science in college (15 years ago) While coding and logic don't change much, its syntax and handshakes, and where the computing is happening that I have either forgotten, overlooked, or am ignorant.</p>
  
  <p>I have completed an ajax based search submission.
  this search displays the target php in a div labeled .Admin
  this works excellently.</p>
  
  <p>Its answer is a display of a tree and the nodes above and below the searched node.</p>
  
  <p>I want to make each node in the tree a clickable link to the search results for its own node.</p>
  
  <p>In the php result I coded it to write in a html call to a javascript function and it generates a unique id for each , as well as its own function call. (whether or not that is a good idea its what i tried) I would think that it is then easy to get javascript to fill out the form with the created link using information it knows and resubmit as no refresh is needed to search again. I tried having the php write this script so that it is developed with the page, and I have tried writing the script and including it in the parent page, so that it is already loaded... I'm not sure where it should go or how it should be written.  </p>
  
  <p>I may be missing an integral part, or I'm trying to do more than I am supposed to with the code or not completely grasping where the operation is happening and need to rethink it entirely.</p>
  
  <p>I will try to include all relevant code, if its not enough, I will happily add. Thanks in advance.</p>
  
  <p>CallSearch.js</p>
  
  <pre><code>    $(document).ready(function() {
          $('#Search').submit(function(event) {
              $.ajax({
                  type        : 'POST', 
                  url         : 'MenuSearch.php',
                  data        : $( this ).serialize()
              })
              .done(function(results) {
                  $(".Admin").html(results);
              });
          event.preventDefault();
          }); 
      });
  </code></pre>
  
  <p>Menu.php (where my search form is)</p>
  
  <pre><code>    &lt;script src="/JS/CallSearch.js"&gt;&lt;/script&gt;
       ...
      &lt;li&gt;&lt;a&gt;Search&lt;/a&gt; &lt;input type= "text" name="Search" id="search_name" class="search-group"&gt;
                  &lt;ul&gt;
                      &lt;li&gt;&lt;input type="radio" name="Table"  id="TableRegion" value="R" class="search-group" checked&gt;&lt;label for="TableRegion"&gt;Region&lt;/label&gt;&lt;/li&gt;
                      &lt;li&gt;&lt;input type="radio" name="Table"  id="TableGrape" value="G" class="search-group"&gt;&lt;label for="TableGrape"&gt;Grape&lt;/label&gt;&lt;/li&gt;
                      &lt;li&gt;&lt;input type="radio" name="Table"  id="TableWine" value="W" class="search-group"&gt;&lt;label for="TableWine"&gt;Wine&lt;/label&gt;&lt;/li&gt;
                      &lt;li&gt;&lt;button type="submit" id="SubmitSearch"&gt;Submit&lt;/button&gt;&lt;/li&gt;
                  &lt;/ul&gt;
              &lt;/li&gt;
          &lt;/form&gt;
      ...
  </code></pre>
  
  <p>MenuSearch.php</p>
  
  <pre><code>    define('DB_SERVER', 'localhost');
      define('DB_USERNAME', 'Webpage');
      define('DB_PASSWORD', 'Guest');
      define('DB_DATABASE', 'database');
      $db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
      ...
      $sqlp ="SELECT * FROM region a JOIN Regionclosure b ON a.idregion=b.parent WHERE b.child= $idReg ORDER BY b.Depth DESC;";
      ...
      $resultp = mysqli_query($db, $sqlp);
              $string ="";
              while($row = mysqli_fetch_assoc($resultp)){
                  $Reg = $row["RegName"];
                  $Parent= $row["Parent"];
                  echo $string . "|___";
                  if($_SESSION["Level"] == 'A' or $_SESSION["Level"] == 'C') 
                      echo $Parent . " - ";
                  echo  "&lt;a href='#' id='$Reg'&gt;" . $Reg . "&lt;/a&gt;&lt;br&gt;";
                  echo "
                      &lt;Script&gt;
                          $('#$Reg').click(function(){ searchforReg('$Reg'); return false; });
                      &lt;/script&gt;";
                  $string .= "&amp;nbsp;&amp;nbsp;&amp;nbsp;";
              };
  </code></pre>
  
  <p>I have tried multiple iterations of a function for "searchforReg('$Reg'); to no avail... i feel like it should be something like :</p>
  
  <pre><code>    function "searchforReg(Region){
      SearchData= "Table=R&amp;Search="+$(Region).val;
      (function() {
          $.ajax({
              type        : 'POST', 
              url         : 'MenuSearch.php',
              data        :  SearchData
          })
          .done(function(results) {
              $(".Admin").html(results);
          });
      event.preventDefault();
          });
      });
  </code></pre>
  
  <p>This however results in activating the "action" search failing to send the answer to the  and opens the php (with no CSS... gasp.. its just not pretty) I feel like a simple javascript should be able to handle it, but i tried all of the different ways i could think of (or look up) to make it work to no avail.</p>
  
  <p>I realize I am playing in the "deep end," and I'm in my "water wings" so if you could have mercy and push me to the edge, I would appreciate it.</p>