Display search result on another page - php

can you help me with displaying output from search form ?
My search form is in sidebar and i would like to display result on main content div.
Navigation is done by switch
this is form in sidebar
<form name="searchform" method="get" action="index.php?page=search">
<input type="text" name="searchword" size="15" />
<input type="submit" name="search" value="Click" class="formbutton" />
</form>
this is switch that should include output to the main content:
switch($_GET['page']){
case "search":
include("includes/search.php");
break;
}
and this is included search.php which should display result:
<?php
include('connect.php');
if(isset($_GET['search'])){
$search_value = $_GET['searchword'];
$query = " SELECT * FROM Articles WHERE keywords LIKE '$search_value'";
$run = mysql_query($query);
while($row=mysql_fetch_array($run)){
$post_id = $row['id'];
$post_title = $row['title'];
echo "<p><a href='index.php?page=post&&post=$post_id'>$post_title</a></p></ br>";
}
}
?>

What about if you specify a different page entirely in the form action instead of index.php? What you did is tell the form to use the index.php and send the GET data to it. If that doesn't help then be more specific on what you want

Related

Bug in form working with PHP, MySQL and HTML

guys. I was going ok with my app until I got to this type of form, written below. I have 2 items in this table and the ID's are "1" and "2". Even though, if I press "Submit" on the "1" ID item, it prints me "2" every time. Does anyone have a clue what might be the problem? Thank you.
<form method="POST">
<?php
$query = "SELECT * FROM table";
while($row = $query->fetch_array())
{
$id = $row['id'];
?>
<input type="hidden" name="id" value="<?php echo $id; ?>" >
<input type="submit" name="submit">
<?php } ?>
</form>
<?php
if(isset($_POST['submit']))
{
echo $_POST['id'];
}
?>
You're outputting two items into the same form with the same name. When you click the Submit button, it gathers all of the fields in the form based on the name attribute and sends them on to wherever the form is being posted.
If you want to have a button that submits each ID with a separate button, you'd want to try having a new form for each ID. There are other ways of doing this too, but based on your current code, try something like this:
<?php
$query = "SELECT * FROM table";
while($row = $query->fetch_array())
{
$id = $row['id'];
?>
<form method="POST">
<input type="hidden" name="id" value="<?php echo $id; ?>" >
<input type="submit" name="submit">
</form>
<?php } ?>
<?php
if(isset($_POST['submit']))
{
echo $_POST['id'];
}
?>

Get sql data using search box in wordpress

What I want to do is using search box to find data in WordPress database and display it in a page,using contact form 7. I upload data in a wp database. I use some PHP code to display data in a page and it works, but I don't know how to do it using a search box.
<?php
global $wpdb;
$result = $wpdb->get_results("SELECT * FROM it_testtable");
echo "ID"." "."Name"."<br><br>";
foreach($result as $row)
{
echo $row->id." ".$row->name."<br>";
}
?>
I want when I enter the an id I get names. Please help me. Thank you.
Step 1 Add the following to your Header.php file
<div class="header-search"><?php get_search_form();?></div>
Step 2 Add the following to your Style.css file
.header-search{display:none}
#media screen and (min-width:600px){
.header-search{
display:block;
float:right;
margin-top:0px;
margin-right:3px;
}
}
Show search query in search box
If you have just performed a search, you can show the last query in the search box:
<form method="get" id="searchform" action="<?php bloginfo('url'); ?>/">
<div>
<input type="text" value="<?php the_search_query(); ?>" name="s" id="s" />
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>
Show search query in results page
You can display the search string on search result pages
<p>You searched for "<?php echo esc_html( get_search_query( false ) ); ?> ". Here are the results:</p>
for reference: About search box query
You can go either this or other option,because there is only name
and ID in your row
$mylink = $wpdb->get_row( "SELECT * FROM {$wpdb->links} WHERE link_id
= 10" );
Else if you want to show it generically,
$fivesdrafts = $wpdb->get_results(
"SELECT link_ID,name FROM {$wpdb->links} WHERE link_ID = 5"
);
foreach ( $fivesdrafts as $fivesdraft )
{
echo $fivesdraft->name;
}

Loading comments from Database

I am developing a simple forum for my php project. I have a viewpost.php file which displays all the posts and has a read more button.like this.
<?php
foreach($rows as $output){
echo '<div id="dd">';
$uid = $output['uid'];
$result2 = $conn->prepare("SELECT username FROM users WHERE uid = '$uid'");
$result2->execute();
$rows2 = $result2->fetchAll();
foreach($rows2 as $username_row){
echo 'POST BY '.$username_row['username'].':';}
echo '<h3>Title:'.$output['title'].'</h4>';
?>
<form action="comments.php" method="post">
<input type="submit" value="Read more" name="">
</form>
<?php
echo '</div>';
}
?>
now I want to load post and comments in comments.php. but I want post_id to retrieve post,title and comments from the database. I wonder how to pass the post_id from viewpost.php to comments.php when the user clicks on the readmore button.any help will be really appreciable am a newbee.
You can add a input to your form
<input type="hidden" value="<?php echo $output['id'] ?>" name="id" />
and in your comments.php file use $_POST['id']
or use URL like :
<a href="comments.php?id='<?php echo $output['id'] ; ?>'">
and in your comments.php file use $_GET['id']

Search results are not returning from sql query using search button

I'm trying to get a search functionality working. When the user clicks in a box and searches something the page should be able to display the results with the word included, otherwise there is a simple message that no result could be found. At the moment nothing is displaying (no errors either) and I do not know why.
Here is what I have so far. Any help would be appreciated.
1) On my index.php page there is the search box and search button.
<li>
<div id = "search">
<form id = "search-form" action="search_results.php" method="post">
<input type="text" name= "find" placeholder= "Search..." />
<input type="button" name = "search" value="search" />
</form>
</div>
</li>
2) When the user types what they want, they are taken to search_results.php where the searching happens. A list of product names should be returned if the search was successful.
<div class = "main-content">
<h1> Search Results </h1>
<?php
include 'dbconnect.php';
$output ='';
if (isset($_get['find'])){
$searchq = $_get['find'];
$query = mysqli_query($con, "SELECT * FROM products WHERE prodname LIKE '%". $searchq . "%'");
$count = mysqli_num_rows($query);
if($count == 0){
echo "There was no search results !";
}
else{
while($row = mysqli_fetch_array($query)){
$prodname = $row['prodname'];
$output ='<div> '.$prodname.'</div>';
echo $output;
}
}
} // end of outer if
mysqli_close($con);
?>
</div>
You are doing post and using the $_get in your search_results.php file which is wrong method.
As you are using the post method in your action you should get the values accordingly
i.e.,
if (isset($_POST['find'])){
$searchq = $_POST['find'];
#Your If Condition
}
else
{
#Your Else Part
}

sql retrieve data from table and make them links

I am generating web pages from database. Now my question is:
I have 1000 records(names) in my database(MySql).
I have made a search box in a page and when i enter any name or a part of name that is in my DB all the name's should come up.
Eg-
SELECT * FROM table where name like '%$find%'
Now i want to show the selected names(fetched through the query) on the new page so that when i click on any of the name a new page should open up and all the data related to that selected name (present in the table belonging to the database)to be shown on that page with navigation buttons, what query should i use to perform it.
In short i want to make my page like Google search page.
My first page is like this
<html>
<body >
<h2>Search</h2>
<form name="search" method="post" action="second.php">
Search Name: <input type="text" name="find" id="find" />
<input type="submit" name="search" value="search" />
</form>
</body>
</html>
Second page is somewhat like this
<html>
<head>
<script>
function favBrowser()
{
var mylist=document.getElementById("opt");
document.getElementById("favorite").value=mylist.options[mylist.selectedIndex].text;
}
</script>
</head>
<body>
<form method="get">
<?php
$find = $_REQUEST['find'];
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("data", $con);
$result = mysql_query("SELECT * FROM table where name like '%$find%'");
$result_rows = mysql_num_rows($result);
while($row = mysql_fetch_array($result))
{
// $names[] = $row['name'];
// echo $names[0];
// echo "$row[name]. $row[id] <a href='data.php?edit=$row[name]'>edit</a><br />";
$_name = $row['name'];
echo "Name : <input type='text' name='name' value='$_name' size='30'>";
echo "<br />";
}
}
mysql_close($con);
?>
<!--</select>
<input type ="submit" value="submit">
<p>Your selected name is: <input type="hidden" name="fun" id="favorite" size="30">
</p>
-->
</body>
</html>
Well, simplified, on the first page you'll have something like:
while($row = mysql_fetch_array($result))
{
$_name = $row['name'];
echo '<a href="second_page.php?name='.strip_tags($_name)'" target="_BLANK"'.'</a>';
}
and on the second page you have name, passed as URL parameter, on which you then do another database look up to get the contacts details and populate the various fields:
$_name = $GET['name'];
Please remember to add the required escapes or rather use PDO / mysqli
But the question is how will you make all the names as links and then fetch their result on next page .. right ?

Categories