Submit button results in data loss on the page?

Links for php scripts

Moderators: egami, macek, gesf

Post Reply
AnMK
New php-forum User
New php-forum User
Posts: 3
Joined: Thu Apr 12, 2018 1:08 pm

Tue Apr 24, 2018 2:51 pm

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: Select all

<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> 

                      <!--on submitt process the input in textarea 1-->
                             <?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>

chdevelopment
New php-forum User
New php-forum User
Posts: 3
Joined: Tue Jun 26, 2018 3:28 am
Contact:

Tue Jun 26, 2018 3:43 am

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.

Post Reply