0

This is the HTML code for my search form which ia m using to take input.

<div class="srcarea">
<form action="result_page.php" method="post">
    <input type="text" name="search" class="search" placeholder="What's On Your Mind Today?"></br>
    <input type="submit" name="" value="Go" class="srcbtn"> 
</form>
</div>

This is the PHP code whis searches the database for the result .

<?php

include 'connection.php';
$result ='';
$count='';
// Create connection
//$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
//echo "Connected successfully";
mysqli_select_db($conn,"search") or die ("Could Not find the db");
$output = "";

if(isset($_POST['search'])) {

    #$searchq = $_POST['search'];
    $searchq = mysqli_real_escape_string($conn,$_POST['search']);
    if($searchq !="")
    {
    $result=$query = mysqli_query($conn,"SELECT * FROM members WHERE firstname LIKE '%$searchq%' OR lastname LIKE '%$searchq%'");
    }
    if($result){
         $count =mysqli_num_rows($query);
    }

    if($count ==0)
    {
        $output = 'No Results Found !';
    }
    else
    {
        while($row = mysqli_fetch_array($query)){
            $fname = $row['firstname'];
            $lname = $row['lastname'];
            $id = $row['id'];
            $output .='<div>'.$fname.' '.$lname.'</div>';

        }

    }

}
//echo ($output);
?>

And I am displaying output of the search over this page where i get confirm resubmission dialogue i have looked for the possible solutions over here but i'm unable to fix the same.

<div class="header">
<?php
include 'header.php';
?>

</div>
<div id="res">
<?php
include 'search.php';
?>
<?php print("output")?>
</div>

I need to fix this.

Ashish
  • 31
  • 1
  • 1
  • 6
  • What exactly is the issue? Is the form submitted twice? – agrm Nov 26 '17 at 11:47
  • What is triggering a confirm resubmission dialog? When specifically does that happen? – David Nov 26 '17 at 11:48
  • 4
    Use `GET` for your search form instead. In general, use GET when you're fetching (getting) data and POST when you're sending (posting) data for storage. A search is fetching data with the search string as a filter. – M. Eriksson Nov 26 '17 at 11:48
  • @MagnusEriksson - I agree but I would add, that if you use `$_GET` for search forms you can have the added benefit of making them bookmarkable for your users. Most people over look this, but I see it as a almost necessary piece of usability. I falls under the "Ease of Use" category in UI development. – ArtisticPhoenix Nov 26 '17 at 12:07
  • In your PHP code for searching the database : rap the search code inside the submit input code( i.e search and display the results only when the submit button is clicked) – hans-könig Nov 26 '17 at 12:23
  • @agrm yes I think the form is submitted twice because the results i am getting are the same so unable to detect. – Ashish Nov 26 '17 at 13:58
  • @hans can you elaborate or if you can modify my search code accordingly it will be a great help . thank you – Ashish Nov 26 '17 at 13:59
  • @David whenever i refresh on the search results page I am facing this issue. I am unable to detect what is triggering it. – Ashish Nov 26 '17 at 14:02
  • @Ashish: When you *refresh the page*? Well then *you're* triggering it. When you refresh a POST request browsers ask you if you want to re-submit that request. Why are you refreshing the page? What are you expecting to happen and why? – David Nov 27 '17 at 12:34
  • @David I don't want the form to be resubmitted when the page is refreshed what I am trying to achieve is that whenever there is a refresh I don't want that warning. – Ashish Nov 27 '17 at 21:27
  • @Ashish: That's what refreshing the page does. It *re-sends* the last request. So if that's not what you're trying to do then it's not clear what else you expect refreshing the page to accomplish. One option could be to make the form a GET method instead of POST, then the browser might not show the warning. – David Nov 27 '17 at 21:41
  • @David I tried using GET method but still the same issue, by the way, thanks for your support. – Ashish Nov 28 '17 at 12:19

0 Answers0