Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

PHP How do I display the search result in modal

rmj091619

New Coder
This the code I used but but nothing happens

[CODE title="index"] <form method="POST" action="#">

<input type="text" name="id" placeholder="Enter query"/>

<input type="button" name="search" value="Search">

</form>







<script>

$.ajax({

type:"POST",

url: "search.php",

success: function(data)

{

debugger $('#mymodal').modal('toggle');

$('#mymodal:visible .modal-content .modal-body').html(e);

}

});

</script>



<div class="modal" id="mymodal">

<div class="modal-dialog">

<div class="modal-content">

<div class="modal-header">

<h4 class="modal-title">Modal Heading</h4>

<button type="button" class="close" data-dismiss="modal">&times;</button>

</div>

<!-- Modal body -->

<div class="modal-body">

</div>

<div class="modal-footer">

<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>

</div>

</div>

</div>

</div>[/CODE]



search.php


[CODE title="SQL"]<?php

if (isset($_POST['search']) && isset($_POST['id']) && ($_POST['id'] != ""))

{



$id=$_POST['id'];

$query="SELECT * FROM `upload` WHERE `keywords` LIKE '%$k%' ";



@mysql_connect("localhost","root","") or die("error");

mysql_select_db("lol") or die ("error");



$query= mysql_query($query);

$numrows=mysql_num_rows($query);

if($numrows >0)

{

while ($row =mysql_fetch_assoc($query))

{



$keywords=$row['keywords'];

$name2=$row['name2'];

$URL=$row['URL'];

echo "<h2><a href='$URL'>$name2</a></h2><br/><br/>";



}

}

else

echo "no result found";

}

else

{

echo "please enter some value in search";

}

?>[/CODE]
 
Last edited:
There's quite a bit to unpack here, but you've got a couple things wrong with your form.

First of all, if you're using Ajax to get the search results, you don't need to actually use a form. Semantically it's right but it's unnecessary here. Secondly, your jQuery (from the looks of it) isn't actually doing much and won't run because it's not formatted correctly.

Here is your HTML:

HTML:
<form>

    <input type="text" name="query" id="query" placeholder="Enter query" />

    <button type="button" id="searchButton">Search</button>

</form>

<div class="modal" id="searchModel" style="display: none;">

    <div class="modal-dialog">

        <div class="modal-content">

            <div class="modal-header">

                <h4 class="modal-title">Modal Heading</h4>

                <button type="button" class="close" data-dismiss="modal">&times;</button>

            </div>

            <!-- Modal body -->

            <div class="modal-body">

            </div>

            <div class="modal-footer">

                <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>

            </div>

        </div>

    </div>

</div>

Here is your JavaScript (jQuery):

[CODE lang="javascript" title="JavaScript"]$( '#searchButton' ).click(function() {

// Get the search term query.
var theQuery = $( '#query' ).val();

// Run an Ajax request to search.php.
$.ajax( {

url: 'search.php',
data: {
query: theQuery
},
success: function( data ) {

alert( data );

$( '#searchModel' ).show();

}

} );

} );[/CODE]

Here is your php:

PHP:
<?php

// Catch empty post requests.
if ( ! isset( $_POST[ 'query' ] ) ) {

    return 0;

}

// Get the query from the post data.
$query = trim( $_POST[ 'query' ] );

// Return 0 if the query is empty.
if ( 1 > strlen( $query ) ) {

    return 0;

}

// Connect to the database.
@mysql_connect( 'locahost', 'root', 'root' ) or die();

// Select the database.
mysql_select_db( 'mydb' ) or die();

// Fetch the result from the database.
$response = mysql_query( 'SELECT * FROM tablename WHERE columnname LIKE %' . $query . '%'  );

// Count the rows.
$count = mysql_num_rows( $response );

if ( 0 < $count ) {

    // Return the response as JSON.
    return json_encode( $response );

} else {

    return 0;

}

I haven't tested this and you'll need to format the response, but it should get you on the right track to begin with though.
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom