PHP - display query search results in Bootstrap modal window












0















I have code below for a search bar that, upon clicking 'Search', loads a new page with the query results. How do I change it so instead of loading a new page, the query results open in a modal popup within the same page?



index.php



<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<form method="POST" action="search.php">
<input type="text" name="q" placeholder="Enter query"/>
<input type="submit" name="search" value="Search" />
</form>
</body>


search.php



<?php
error_reporting(E_ALL);
ini_set('display_errors',1);

include_once('db.php'); //Connect to database
if(isset($_POST['q'])){
$q = $_POST['q'];

//get required columns
$query = mysqli_query($conn, "SELECT * FROM `words` WHERE `englishWord` LIKE '%$q%' OR `yupikWord` LIKE '%$q%'") or die(mysqli_error($conn)); //check for query error
$count = mysqli_num_rows($query);
if($count == 0){
$output = '<h2>No result found</h2>';
}else{
while($row = mysqli_fetch_assoc($query)){
$output .= '<h2>'.$row['yupikWord'].'</h2><br>';
$output .= '<h2>'.$row['englishWord'].'</h2><br>';
$output .= '<h2>'.$row['audio'].'</h2><br>';
$audio_name = $row['audio'];
$output .= "<a href='audio/$audio_name'>$audio_name</a> ";
}
}
echo $output;
}else{
"Please add search parameter";
}

mysqli_close($conn);
?>









share|improve this question

























  • jQuery AJAX or XMLHTTPRequest should help you see this question

    – user10415043
    Nov 21 '18 at 0:26
















0















I have code below for a search bar that, upon clicking 'Search', loads a new page with the query results. How do I change it so instead of loading a new page, the query results open in a modal popup within the same page?



index.php



<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<form method="POST" action="search.php">
<input type="text" name="q" placeholder="Enter query"/>
<input type="submit" name="search" value="Search" />
</form>
</body>


search.php



<?php
error_reporting(E_ALL);
ini_set('display_errors',1);

include_once('db.php'); //Connect to database
if(isset($_POST['q'])){
$q = $_POST['q'];

//get required columns
$query = mysqli_query($conn, "SELECT * FROM `words` WHERE `englishWord` LIKE '%$q%' OR `yupikWord` LIKE '%$q%'") or die(mysqli_error($conn)); //check for query error
$count = mysqli_num_rows($query);
if($count == 0){
$output = '<h2>No result found</h2>';
}else{
while($row = mysqli_fetch_assoc($query)){
$output .= '<h2>'.$row['yupikWord'].'</h2><br>';
$output .= '<h2>'.$row['englishWord'].'</h2><br>';
$output .= '<h2>'.$row['audio'].'</h2><br>';
$audio_name = $row['audio'];
$output .= "<a href='audio/$audio_name'>$audio_name</a> ";
}
}
echo $output;
}else{
"Please add search parameter";
}

mysqli_close($conn);
?>









share|improve this question

























  • jQuery AJAX or XMLHTTPRequest should help you see this question

    – user10415043
    Nov 21 '18 at 0:26














0












0








0








I have code below for a search bar that, upon clicking 'Search', loads a new page with the query results. How do I change it so instead of loading a new page, the query results open in a modal popup within the same page?



index.php



<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<form method="POST" action="search.php">
<input type="text" name="q" placeholder="Enter query"/>
<input type="submit" name="search" value="Search" />
</form>
</body>


search.php



<?php
error_reporting(E_ALL);
ini_set('display_errors',1);

include_once('db.php'); //Connect to database
if(isset($_POST['q'])){
$q = $_POST['q'];

//get required columns
$query = mysqli_query($conn, "SELECT * FROM `words` WHERE `englishWord` LIKE '%$q%' OR `yupikWord` LIKE '%$q%'") or die(mysqli_error($conn)); //check for query error
$count = mysqli_num_rows($query);
if($count == 0){
$output = '<h2>No result found</h2>';
}else{
while($row = mysqli_fetch_assoc($query)){
$output .= '<h2>'.$row['yupikWord'].'</h2><br>';
$output .= '<h2>'.$row['englishWord'].'</h2><br>';
$output .= '<h2>'.$row['audio'].'</h2><br>';
$audio_name = $row['audio'];
$output .= "<a href='audio/$audio_name'>$audio_name</a> ";
}
}
echo $output;
}else{
"Please add search parameter";
}

mysqli_close($conn);
?>









share|improve this question
















I have code below for a search bar that, upon clicking 'Search', loads a new page with the query results. How do I change it so instead of loading a new page, the query results open in a modal popup within the same page?



index.php



<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<form method="POST" action="search.php">
<input type="text" name="q" placeholder="Enter query"/>
<input type="submit" name="search" value="Search" />
</form>
</body>


search.php



<?php
error_reporting(E_ALL);
ini_set('display_errors',1);

include_once('db.php'); //Connect to database
if(isset($_POST['q'])){
$q = $_POST['q'];

//get required columns
$query = mysqli_query($conn, "SELECT * FROM `words` WHERE `englishWord` LIKE '%$q%' OR `yupikWord` LIKE '%$q%'") or die(mysqli_error($conn)); //check for query error
$count = mysqli_num_rows($query);
if($count == 0){
$output = '<h2>No result found</h2>';
}else{
while($row = mysqli_fetch_assoc($query)){
$output .= '<h2>'.$row['yupikWord'].'</h2><br>';
$output .= '<h2>'.$row['englishWord'].'</h2><br>';
$output .= '<h2>'.$row['audio'].'</h2><br>';
$audio_name = $row['audio'];
$output .= "<a href='audio/$audio_name'>$audio_name</a> ";
}
}
echo $output;
}else{
"Please add search parameter";
}

mysqli_close($conn);
?>






php bootstrap-modal






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 0:26







user10415043

















asked Nov 21 '18 at 0:19









user10676901user10676901

44




44













  • jQuery AJAX or XMLHTTPRequest should help you see this question

    – user10415043
    Nov 21 '18 at 0:26



















  • jQuery AJAX or XMLHTTPRequest should help you see this question

    – user10415043
    Nov 21 '18 at 0:26

















jQuery AJAX or XMLHTTPRequest should help you see this question

– user10415043
Nov 21 '18 at 0:26





jQuery AJAX or XMLHTTPRequest should help you see this question

– user10415043
Nov 21 '18 at 0:26












2 Answers
2






active

oldest

votes


















0














You need to use JavaScript on your page to executed an AJAX call to search.php. That PHP file preferable returns JSON data, or complete HTML that can be added to the modal window.



Conceptually:




  1. Use JavaScript to executed AJAX POST to search.php

  2. Have search.php return the data in JSON format.

  3. Have JavaScript catch the returned data, iterate through it and create HTML elements.

  4. Use JavaScript to open a new modal window.

  5. Use JavaScript to add the HTML elements to the modal's body.


You don't necessarily need to use JavaScript to create the modal window. You can create it in plain HTML and fill it and open it using JavaScript.






share|improve this answer































    0














    Welcome to stackoverflow. Here's my solution for that, so first, you have to capture the form submission, the technique is doing GET request using jQuery which I assume you already using jQuery since you are using bootstrap and bootstrap uses jQuery



    $('form').submit(function(e){
    e.preventDefault() // do not submit form
    // do get request
    $.get( 'search.php', { q : },function(e){
    // then show the modal first
    $('#mymodal').modal('show');
    // then put the results there
    $('#mymodal:visible .modal-container .modal-body').html(e);
    });
    });





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403564%2fphp-display-query-search-results-in-bootstrap-modal-window%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      You need to use JavaScript on your page to executed an AJAX call to search.php. That PHP file preferable returns JSON data, or complete HTML that can be added to the modal window.



      Conceptually:




      1. Use JavaScript to executed AJAX POST to search.php

      2. Have search.php return the data in JSON format.

      3. Have JavaScript catch the returned data, iterate through it and create HTML elements.

      4. Use JavaScript to open a new modal window.

      5. Use JavaScript to add the HTML elements to the modal's body.


      You don't necessarily need to use JavaScript to create the modal window. You can create it in plain HTML and fill it and open it using JavaScript.






      share|improve this answer




























        0














        You need to use JavaScript on your page to executed an AJAX call to search.php. That PHP file preferable returns JSON data, or complete HTML that can be added to the modal window.



        Conceptually:




        1. Use JavaScript to executed AJAX POST to search.php

        2. Have search.php return the data in JSON format.

        3. Have JavaScript catch the returned data, iterate through it and create HTML elements.

        4. Use JavaScript to open a new modal window.

        5. Use JavaScript to add the HTML elements to the modal's body.


        You don't necessarily need to use JavaScript to create the modal window. You can create it in plain HTML and fill it and open it using JavaScript.






        share|improve this answer


























          0












          0








          0







          You need to use JavaScript on your page to executed an AJAX call to search.php. That PHP file preferable returns JSON data, or complete HTML that can be added to the modal window.



          Conceptually:




          1. Use JavaScript to executed AJAX POST to search.php

          2. Have search.php return the data in JSON format.

          3. Have JavaScript catch the returned data, iterate through it and create HTML elements.

          4. Use JavaScript to open a new modal window.

          5. Use JavaScript to add the HTML elements to the modal's body.


          You don't necessarily need to use JavaScript to create the modal window. You can create it in plain HTML and fill it and open it using JavaScript.






          share|improve this answer













          You need to use JavaScript on your page to executed an AJAX call to search.php. That PHP file preferable returns JSON data, or complete HTML that can be added to the modal window.



          Conceptually:




          1. Use JavaScript to executed AJAX POST to search.php

          2. Have search.php return the data in JSON format.

          3. Have JavaScript catch the returned data, iterate through it and create HTML elements.

          4. Use JavaScript to open a new modal window.

          5. Use JavaScript to add the HTML elements to the modal's body.


          You don't necessarily need to use JavaScript to create the modal window. You can create it in plain HTML and fill it and open it using JavaScript.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 0:35









          bartbart

          7,551175781




          7,551175781

























              0














              Welcome to stackoverflow. Here's my solution for that, so first, you have to capture the form submission, the technique is doing GET request using jQuery which I assume you already using jQuery since you are using bootstrap and bootstrap uses jQuery



              $('form').submit(function(e){
              e.preventDefault() // do not submit form
              // do get request
              $.get( 'search.php', { q : },function(e){
              // then show the modal first
              $('#mymodal').modal('show');
              // then put the results there
              $('#mymodal:visible .modal-container .modal-body').html(e);
              });
              });





              share|improve this answer




























                0














                Welcome to stackoverflow. Here's my solution for that, so first, you have to capture the form submission, the technique is doing GET request using jQuery which I assume you already using jQuery since you are using bootstrap and bootstrap uses jQuery



                $('form').submit(function(e){
                e.preventDefault() // do not submit form
                // do get request
                $.get( 'search.php', { q : },function(e){
                // then show the modal first
                $('#mymodal').modal('show');
                // then put the results there
                $('#mymodal:visible .modal-container .modal-body').html(e);
                });
                });





                share|improve this answer


























                  0












                  0








                  0







                  Welcome to stackoverflow. Here's my solution for that, so first, you have to capture the form submission, the technique is doing GET request using jQuery which I assume you already using jQuery since you are using bootstrap and bootstrap uses jQuery



                  $('form').submit(function(e){
                  e.preventDefault() // do not submit form
                  // do get request
                  $.get( 'search.php', { q : },function(e){
                  // then show the modal first
                  $('#mymodal').modal('show');
                  // then put the results there
                  $('#mymodal:visible .modal-container .modal-body').html(e);
                  });
                  });





                  share|improve this answer













                  Welcome to stackoverflow. Here's my solution for that, so first, you have to capture the form submission, the technique is doing GET request using jQuery which I assume you already using jQuery since you are using bootstrap and bootstrap uses jQuery



                  $('form').submit(function(e){
                  e.preventDefault() // do not submit form
                  // do get request
                  $.get( 'search.php', { q : },function(e){
                  // then show the modal first
                  $('#mymodal').modal('show');
                  // then put the results there
                  $('#mymodal:visible .modal-container .modal-body').html(e);
                  });
                  });






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 '18 at 0:44









                  Juliver GalletoJuliver Galleto

                  3,288154796




                  3,288154796






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403564%2fphp-display-query-search-results-in-bootstrap-modal-window%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith