Submit button results in data loss on the page?

A

Anonymous

Guest
Hello,
I have this form and I want every time I click on the button called Display Examples to have something displayed. It works but when i click on the button I loose the text generated by clicking on the 1st button that is called submit.

Code:
<textarea name="query" id="query" class="mytext" rows="140">
                                        
   <?php echo htmlentities($_GET['query']); ?> 
     </textarea>
    <br>
      <input type="submit" value="submit" id="SB1" name="submit">

                                      </td>

                                       <td> 

                      
                             <?php
                                         
                                      error_reporting(E_ERROR | E_PARSE);

                                  if (isset($_GET['submit'])){
                                     $_SESSION['$words'] = $_GET['query'];

                                        //min length of the string
                                        //$min_length = 1;

                                        $b = explode(' ',(trim($words)));

                                        $lines = explode("\n", $_POST['query']);

                                        foreach ($lines as $k=>$v) if(empty($v)) unset($lines[$k]);

                                        //echo "<span ><font size='4'></font> </span></div>";
                                        echo ("{$_SESSION['$words']}"."<br />");;

                                      //foreach($b as $w)
                                              
                                                //{ echo "<span ><font size='4'>$w</font>  </span>";  } 



                                        }



                                 ?>

                                    
                                          
                                   </td>

                                  <td> </td></th>

                                  <tr><th colspan="4"><br><br><hr></hr><h2>Concordance for: <div id="word" style="background-color:#66c2ff"></div></h2></th></tr>
                                  <tr><td colspan="4" bgcolor="#FFFFFF">

                                   



                          <form method='GET' action=""><input type='submit' value='Display Examples' name='display' id='display'></input></form>
                             


                                   </td>

                                   <td></td>
                                   <td></td>
                                 </tr>



                                 </td>
                                 <td></td>
                                 <td> </td>
                                </tr>

      

                                </tr>


                                 </td>
                              </tr>

                                                   </form>

                           </table>

                           <?php



                                 if(isset($_GET["display"])){

                                  //$query=$_GET[$_SESSION['$words'];
                                //echo $query;


                                $file = "https://users.cs.cf.ac.uk/KurtevaA/AdaLovelace/first.html";
                                
                                $searchfor = "In";

                                // the following line prevents the browser from parsing this as HTML.
                                header('Content-Type: text/plain');

                                // get the file contents, assuming the file to be readable (and exist)
                                $contents = file_get_contents($file);
                                // escape special characters in the query
                                $pattern = preg_quote($searchfor, '/');
                                // finalise the regular expression, matching the whole line
                                $pattern = "/^.*$pattern.*\$/m";
                                // search, and store all matching occurences in $matches
                                if(preg_match_all($pattern, $contents, $matches)){
                                   //echo "Found matches:";
                                   echo implode($matches[0]);
                                }
                                else{
                                  echo "No matches found";
                                }


}


                                  ?>

                        </th>
                     </tr>

                  </table>
 
AnMK, I am not 100% clear on what you described or trying to do.
However, both of your buttons are submit buttons. That means that the page will redirect / refresh when either button is pushed so you will loose all the form values that were in the fields.

Rather use a javascript / jquery ajax call to display the example data, that way you can change the page content without the page refreshing.
 
Back
Top