How to make search results appear on another page? - php

I have a big search on my home-page and when the user types in the text fields and clicks submit I want the results from my database to appear on another site in this case 'searchresults.php.' In this case 'really.html' is my homepage.
Here is my code:
What am I doing wrong?
Really.html
<center>
<form action="searchresults.php" method="post">
<input type="text" size="35" value="Job Title e.g. Assistant Manager"
style="background- color:white; border:
solid 1px #6E6E6E; height: 30px; font-size:18px;
vertical-align:9px;color:#bbb"
onfocus="if(this.value == 'Job Title e.g. Assistant Manager'){this.value =
'';this.style.color='#000'}" />
<input type="text" size="35" value="Location e.g. Manchester"
style="background- color:white; border:
solid 1px #6E6E6E; height: 30px; font-size:18px;
vertical-align:9px;color:#bbb"
onfocus="if(this.value == 'Location e.g. Manchester'){this.value =
'';this.style.color='#000'}" />
<input type="image" src="but.tiff" alt="Submit" width="60">
</form>
Searchresults.php
<html>
<body>
<?php
if(strlen(trim($_POST['search'])) > 0) {
//all of your php code for the search
$search = "%" . $_POST["search"] . "%";
$searchterm = "%" . $_POST["searchterm"] . "%";
mysql_connect ("", "", "");
mysql_select_db ("");
if (!empty($_POST["search_string"]))
{
}
$query = "SELECT name,location,msg FROM contact WHERE name LIKE '$search' AND
location LIKE '$searchterm'";
$result = mysql_query ($query);
if ($result) {
while ($row = mysql_fetch_array ($result)) {
echo "<br>$row[0]<br>";
echo "$row[1]<br>";
echo "$row[2]<br>";
}
}
}
?>
</body>
</html>
Thanks!

You should add attr name to your input.
For example,
<input type="text" name="search" ... />
<input type="text" name="searchterm" ... />
Also don't forget about escaping input data using mysqL_escape_string function

The query is probably returning 0 results. Instead of
if ($result) {
Try
if (mysql_num_rows($result) >= 1) {
And in your query try...
$query = "SELECT name,location,msg FROM contact WHERE name LIKE '%$search%' AND
location LIKE '%$searchterm%'";
This will be less strict and return a better result set.

Related

Trouble controlling the loops

If pressed delet , All images are deleted ,I do not want that.
I just want to delete one image.
$get_img = "SELECT * FROM img";
$run_img = mysqli_query($db,$get_img);
while ($row_img= mysqli_fetch_array($run_img)){
if (isset($_POST['delete'])){
$delete= "DELETE FROM img WHERE id = {$row_img['id']} ";
$query = mysqli_query ($db , $delete);
}
echo '
<div style="width:200px; height:200px; border: 5px solid red; margin:10px; float: right;">
<img src="images/'.$row_pro['c_img'].'" width="200" height="200" /><br />
<form action="get.php" method="POST">
<input type="submit" name="delet" value="delet" />
</form>
</div>
';
}
Just a little explain, we start talking Arabic because Mostafa is not good in english, so i wrote a little explain in English and Arabic below to help to help both languages users.
while($row_img = mysqli_fetch_array($run_img)){
//explain [1]
echo '
<div style="width:200px; height:200px; border: 5px solid red; margin:10px; float: right;">
<img src="images/'.$row_img['c_img'].'" width="200" height="200" /><br />
<form action="?" method="POST">
<input name="id" value="'.$row_img['id'].'" hidden><!-- //explain [2] -->
<input type="submit" name="delet" value="delet" />
</form>
</div>
';
}
if(isset($_POST['delet'])){
$id = intval($_POST['id']);//explain [3]
$delete= "DELETE FROM img WHERE id = '$id'";
$query = mysqli_query($db, $delete);
if($query){
echo 'Deleted Successfully!';
}
}
explain [1] Remove the query from inside while its bad to put it there
explain [2] Then i made a hidden input called id to get the id from it
explain [3] Make the hidden sent id to $id variable and intval it for more security, you can read about it
.
explain [1] اول حاجة شلت الكويري إلي انت كاتبها جوا ال while
explain [2] بعدين سويت زر مخفي مع كل صورة إسمه أي دي يعطينا الاي دي الخاص فيها
explain [3] بعدين بستقبل الاي دي إلى بالزر المخفي على متغير جديد وبسويله شوية حماية.
I hope this helps you :)
Grab your isset and delete query out of while
Something like that. I did not checked all details but maybe it's good for inspiration.
$get_img = "SELECT * FROM img";
$run_img = mysqli_query($db, $get_img);
while ($row_img = mysqli_fetch_array($run_img))
{
echo '<div>
<img src="images/' . $row_pro['c_img'] . '" /><br />
</div>';
}
echo '<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="submit" name="delete" value="delete" />
</form>';
if (isset($_POST['delete']))
{
$delete= "DELETE FROM img WHERE id = :id", ['id'=>$row_pro['id']];
$query = mysqli_query($db, $delete);
}
Hope you'll figured out.

Search multiple Criteria - And/or search in PHP

Not sure what I am doing wrong here. I would like to create a search that allows the user to do an and or search.
However when I use the below code, if I type in Brown as Colour1 it will return all results, same as post code.
The goal is to allow the user to search multiple fields to return a match. So Colour1 and Postcode
<html>
<head>
<title> Logo Search</title>
<style type="text/css">
table {
background-color: #FCF;
}
th {
width: 250px;
text-align: left;
}
</style>
</head>
<body>
<h1> National Logo Search</h1>
<form method="post" action="singlesearch2.php">
<input type="hidden" name="submitted" value="true"/>
<label>Colour 1: <input type="text" name="criteria" /></label>
<label>Colour 2: <input type="text" name="criteria2" /></label>
<label>PostCode: <input type="text" name="criteria3" /></label>
<label>Suburb: <input type="text" name="criteria4" /></label>
<input type="submit" />
</form>
<?php
if (isset($_POST['submitted'])) {
// connect to the database
include('connect.php');
//echo "connected " ;
$criteria = $_POST['criteria'];
$query = "SELECT * FROM `Mainlist` WHERE (`Colour1`like '%$criteria%')
or
('Colour2' like '%$criteria2%')
or
('PostCode' = '%$criteria3%')
or
('Suburb' like '%$criteria4%')
LIMIT 0,5";
$result = mysqli_query($dbcon, $query) or die(' but there was an error getting data');
echo "<table>";
echo "<tr> <th>School</th> <th>State</th> <th>Suburb</th> <th>PostCode</th> <th>Logo</th> <th>Uniform</th></tr>";
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<tr><td>";
echo $row['School'];
echo "</td><td>";
echo $row['State'];
echo "</td><td>";
echo $row['Suburb'];
echo "</td><td>";
echo $row['PostCode'];
echo "</td><td><img src=\"data:image/jpeg;base64,";
echo base64_encode($row['Logo']);
echo "\" /></td></td>";
echo "</td><td><img src=\"data:image/jpeg;base64,";
echo base64_encode($row['Uniform']);
echo "\" /></td></td>";
}
echo "</table>";
}// end of main if statment
?>
</body>
</html>
I can get it to work correctly when I use a dropdown list to select the criteria however I would like them to have multiple options to filter results.
<form method="post" action="multisearch.php">
<input type="hidden" name="submitted" value="true"/>
<label>Search Category:
<select name="category">
<option value="Colour1">Main Colour</option>
<option value="Colour2">Secondary Colour</option>
<option value="PostCode">Post Code</option>
</select>
<label>Search Criteria: <input type="text" name="criteria" /></label>
<input type="submit" />
</form>
<?php
if (isset($_POST['submitted'])) {
// connect to the database
include('connect.php');
echo "connected " ;
$category = $_POST['category'];
$criteria = $_POST['criteria'];
$query = "SELECT * FROM `Mainlist` WHERE $category LIKE '%$criteria%'";
$result = mysqli_query($dbcon, $query) or die(' but there was an error getting data');
In general you run your query with AND condition because it has criteria and you have it means that you have to show value by matching each criteria with receptive column. but it also depends on users or client how they want to show their field.
In short it doesn't have any rule how to show.
Played around with it for a bit and found the answer. I needed to define the criteria. see below code
<?php
if (isset($_POST['submitted'])) {
// connect to the database
include('connect.php');
//echo "connected " ;
$criteria = $_POST['criteria'];
$criteria2 = $_POST['criteria2'];
$criteria3 = $_POST['criteria3'];
$criteria4 = $_POST['criteria4'];
$criteria5 = $_POST['criteria5'];
$query = "SELECT * FROM `Mainlist` WHERE (`Colour1`like '%$criteria%') and (`Colour2`like '%$criteria2%')
and (`PostCode`like '%$criteria3%') and (`Suburb`like '%$criteria4%') and (`State`like '%$criteria5%')

Radio button and search text box in php

I have my input form:
<form style="font-size: 10pt; font-family: 'Courier New'; color:black;font-weight:600;margin-left:15px;line-height:25px" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="radio" id="a" name="search" value="EntryId" checked>EntryId <font color="blue">(get Vocab information)</font>
<br />
<input type="radio" id="b" name="search" value="EntryId1">EntryId <font color="blue">(get disease name, protein name)</font>
<br />
<input type="radio" id="c" name="search" value="UniprotId">UniprotId <font color="blue">(get Gene name, Dna Seq)</font>
<br />
<input type="radio" id="d" name="search" value="Genename">Genename <font color="blue">(get Gene information)</font>
<br />
<input type="radio" id="e" name="search" value="EntryId3">EntryId <font color="blue">(get HTML, PubMed information)</font>
<br /><br /><br />
<input style="height:30px; width: 300px; border:black 1px solid" type="text" name="Search" >
<input style="height:30px" type="submit" value="Go">
</form>
In this form I am using six radio buttons and each one is representing a stored procedure which is stored in MySQL. If I select a radio button and enter a value in text box (which will be the parameter for my selected procedure) I want output for that procedure.
PHP code:
<?php
$id='';
$query='';
if (isset($_POST['Search']))
{
$id=$_POST['Search'];
}
echo "<table>";
//connect to database
$connection = mysqli_connect("localhost", "root", "", "mohammad_prostatecancer");
//run the store proc
if (isset($_POST['EntryId1']))
{
$query= "call DiseaseVocab(".'"'.$id.'")';
}
if (isset($_POST['EntryId2']))
{
$query= "call DiseaseProtein(".'"'.$id.'")';
}
if (isset($_POST['UniprotId']))
{
$query= "call getGene(".'"'.$id.'")';
}
if (isset($_POST['Genename']))
{
$query= "call Getname(".'"'.$id.'")';
}
if (isset($_POST['EntryId3']))
{
$query= "call Getdata(".'"'.$id.'")';
}
$result = mysqli_query($connection, $query) or die("Query fail: " . mysqli_error());
//loop the result set
echo "<tbody>";
// point to the beginning of the array
$check = mysqli_data_seek($result, 0);
?>
<tr>
<td><b>EntryId</b></td>
<td><b>Vocabid</b></td>
<td><b>VocabSourceName</b></td>
</tr>
<?php
while ($rownew = mysqli_fetch_assoc($result)) {
echo "<tr>";
foreach($rownew as $k => $v)
{
echo "<td>".$v."</td>";
}
echo "</tr>"."<br>";
}
echo "</tbody></table>";
?>
I am not getting any result. I get these warnings:
Warning: mysqli_query(): Empty query
Warning: mysqli_error() expects exactly 1 parameter, 0 given
You need the connection in the error, do it like this and then you can see the error.
mysqli_error($connection)
query is empty because you are checking
`if (isset($_POST['EntryId1']))` instead of `if (isset($_POST['search']) && $_POST['search']=='EntryId1')`
and switch case would be b`enter code here`etter in this scenario.

Search Bar with Location dropdown sumit both results - PHP / mySQL

I have a basic search bar that searches my database using keywords (k). I am trying to submit both location and search result but the problem is when I submit the search results I get "search.php?k=builder&k=New+York" and not "search.php?k=builder+New+York" how do I correct this?
HTML
<form action="search.php" method="get" style="margin:0 auto; text-align:center;">
<input type="text" name="k" size="10" style="width:40%;"/>
<div id="the-basics">
<input class="typeahead" type="text" name="k" placeholder="States of USA">
</div>
<input type="submit" value="Search" style="width:100px;">
</form>
Search Bar PHP Code:
<?php
if(!empty($_GET['k'])){
//if search bar is empty
}else{
echo "'<meta http-equiv='refresh' content='0;url=index.php'>";
//redirect back to index.php
}
$k = $_GET['k']; //k stands for keyword
$i = 0;
$terms = explode(" ", $k);
$query ="SELECT * FROM record WHERE ";
foreach ($terms as $each) {
$i++;
if ($i == 1)
$query .= "company_name LIKE '%$each%' " . "OR website LIKE '%$each%' " . "OR email LIKE '%$each%' " . "OR tel LIKE '%$each%' " . "OR description LIKE '%$each%'" . "OR location LIKE '%$each%'" . "OR description LIKE '%$each%'";
}
$connection = mysql_connect('localhost', 'username', 'password');
mysql_select_db('DB_name');
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0){
while ($row = mysql_fetch_assoc($query)) { { ?>
..........search result shows here........
Hi you need appaly some JS
<script>
function submitForm(obj){
var search = $('input[name="k"]).val();
var state= $('#state').val();
$('input[name="k"]).val(search+" "+state )
obj.submit();
}
</script>
<form action="search.php" method="get" style="margin:0 auto; text-align:center;" onsubmit=submitForm(this)>
<input type="text" name="k" size="10" style="width:40%;"/>
<div id="the-basics">
<input class="typeahead" type="text" id="state" placeholder="States of USA">
</div>
<input type="submit" value="Search" style="width:100px;">
</form>

Need assistance with disabling checkboxes

I currently have a form as you will see below. For simplicity sake the form is condensed to the checkboxes that I'm working with. What I want to have happen is for a user to optionally click one or the other. Depending on which one is clicked, the other one should be disabled. I noticed that user "dinah" posted a similar question before and that this js fiddle was provided http://jsfiddle.net/A5TGf/19/ . However, for some reason when I applied my form id and switched the blacklist names around it wasn't working with my code. Any suggestions? Thanks
HTML/PHP
<form name="insertTicket" id="insertTicket" action="index.php" method="POST" >
<div id="ticket_hidden" style="text-align: center; clear:both;">
<div id="visible_div" style="float: left;">
<input type="checkbox" name="escalated" id ="escalated" value="Yes" onclick="doInput(this);" tabindex="18">Escalate
</div>
<div id="hidden_div" style="float: left; display:none;">
<?php
$dept = $_SESSION['dept_id'];
$get_email = mysql_query("Select email_name, dept_id, copy_user from emails where escalated = 'yes'");
while(($email = mysql_fetch_assoc($get_email)))
{
$department = explode(",", $email['dept_id']);
if(in_array($dept, $department, TRUE))
{
echo '
<input type="checkbox" name="escalated_to[]" id="escalated_to[]" value="'.$email['email_name'].'" />'.$email['email_name'].'
';
if($email['copy_user'] == 'YES')
{
echo '
<input type="text" name="emails[]" style="width: 175px; height: 20px;" placeholder="To:">
';
}
}
}
?>
</div>
<div id="visible_divX" style="float: left;">
<input type="checkbox" name="email" id="email" value="Yes" onclick="doInputs(this);" tabindex="20">Send Email </div></td></tr>
<div id="hidden_divX" style="float: left; display:none; ">
<?php
$dept = $_SESSION['dept_id'];
$get_email = mysql_query("Select email_name, dept_id, copy_user from emails where escalated != 'yes'");
while(($email = mysql_fetch_assoc($get_email)))
{
$department = explode(",", $email['dept_id']);
if(in_array($dept, $department, TRUE))
{
echo '
<input type="checkbox" name="emailed_to[]" id="emailed_to[]" value="'.$email['email_name'].'" />'.$email['email_name'].'
';
if($email['copy_user'] == 'YES')
{
echo '
<input type="text" name="emails[]" style="width: 175px; height: 20px;" placeholder="To:">
';
}
}
}
?>
</div>
</form>
If you need to disable a checkbox by Javascript, you just need one line:
document.getElementById("foo").setAttribute("disabled","disabled")
and to enable it again,
document.getElementById("foo").removeAttribute("disabled")
I don't think you need to use jQuery for such task...
Good luck!

Categories