Undefined index: submit in E:\xampp\htdocs\FA2\search.php on lin - php

Undefined index: submit in E:\xampp\htdocs\FA2\search.php on line2
<?php
$button = $_GET [ 'submit' ];
$search = $_GET [ 'search' ];
if( !$button )
echo "you didn't submit a keyword";
else {
if( strlen( $search ) <= 1 )
echo "Search term too short";
else {
echo "You searched for <b> $search </b> <hr size='1' > </ br > ";
mysql_connect( "localhost","root","") ;
mysql_select_db("fa");
$search_exploded = explode ( " ", $search );
$x = 0;
foreach( $search_exploded as $search_each ) {
$x++;
$construct = "";
if( $x == 1 )
$construct .="keywords LIKE '%$search_each%'";
else $construct .="AND keywords LIKE '%$search_each%'";
}
$construct = " SELECT * FROM schoolname WHERE $construct ";
$run = mysql_query( $construct ); $foundnum = mysql_num_rows($run);
if ($foundnum == 0)
echo "Sorry, there are no matching result for <b> $search </b>. </br> </br> 1. Try more general words. for example: If you want to search 'how to create a website' then use general keyword like 'create' 'website' </br> 2. Try different words with similar meaning </br> 3. Please check your spelling";
else {
echo "$foundnum results found !<p>";
while( $runrows = mysql_fetch_assoc( $run ) ) {
$title = $runrows ['title'];
$desc = $runrows ['description'];
$url = $runrows ['url'];
echo "<a href='$url'> <b> $title </b> </a> <br> $desc <br> <a href='$url'> $url </a> <p>";
}
}
}
}
?>
Form code:
<form action="search.php" method="get" enctype="multipart/form-data">
<input class="wow fadeInRight" data-wow-delay="0.5s" type="text" name="search" placeholder="Seach School Here" required/>
<input class="wow fadeInLeft" data-wow-delay="0.5s" type="submit" value="GET STARTED" name="submit"/>
</form>

When you do the get request it looks like the submit value is not set, this makes is null and you get that error.

First check
isset
:
<?php
if(isset($_GET)){
$button = $_GET [ 'submit' ];
$search = $_GET [ 'search' ];
if( !$button )
......
....
.....
}

As #Sami Kuhmonen said, the fault was enctype="multipart/form-data"...
This only works for POST data, because you can't send a file trough GET
So the form should be:
<form action="search.php" method="get">
<input class="wow fadeInRight" data-wow-delay="0.5s" type="text" name="search" placeholder="Seach School Here" required/>
<input class="wow fadeInLeft" data-wow-delay="0.5s" type="submit" value="GET STARTED" name="submit"/>
</form>

First of all remove those spaces
$_GET['submit'] to $_GET['submit']
And instead use
$_REQUEST['submit']
It contains all parameters submitted via GET or POST method

Related

How to select certain term depending on search?

So I am wanting the user to be able to search by either keyword or ID number. If they search "test" right now for example it will pull all the entries with test which is what I want it to do for the keyword part of the search. However, I also want the user to be able to search my specific a specific ID# and just pulling that specific entry. I am unsure how I would go about doing this. I tried doing some sort of OR statement but it did not pull any entries.
Search box form
<div class ="search" id="browse">
<p> Find your appointment below or search by keyword</p>
<form id="" class="searchbar" action="searchAppt.php" method="get">
<input type="text" name="terms" size="40" class = "sbar" placeholder="Search by issue keyword or ID" oninput="validity.valid||(value='');"
onblur="if (this.value == '') {
this.value = 'Enter keyword or ID';
}"
onfocus="if (this.value == 'Enter keyword or ID') {
this.value = '';
}"/>
<button type="submit" class = "btn">Search</button>
</form>
</div>
searchAppt.php
if (filter_has_var(INPUT_GET, "terms")) {
$terms_str = filter_input(INPUT_GET, 'terms', FILTER_SANITIZE_STRING);
} else {
echo "There were no appointments found.";
include ('includes/footer.php');
exit;
}
//explode the search terms into an array
$terms = explode(" ", $terms_str);
$sql = "SELECT * FROM appointments WHERE 1";
foreach ($terms as $term) {
$sql .= " AND email = '". $_SESSION['email'] ."' AND issue LIKE '%$term%' OR id ='%term%'
";
}
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<br /><br /><center><h1>My Ticket(s)</h1><br />
<div class='table'>
<div class='tr'>
<div class='td'><b>Ticket #</b></div>
<div class='td'><b>Issue</b></div>
<div class='td'><b>Date</b></div>
<div class='td'><b>Ticket Details</b></div>
</div>";
// output data of each row
while($row = $result->fetch_assoc()) {
$starttimepast = strtotime($row["start_time"]); //converts date time received from MySQL into a string
$datepast = date("m/d/y", $starttimepast);
echo "<div class='tr'>
<div class='td'>".$row["id"]."</div>
<div class='td'>".$row["issue"]."</div>
<div class='td'>".$datepast."</div>
<div class='td'><form action='ticketdetails.php' method='post'>
<input type='hidden' name='id' value='".$row["id"]."'>
<input type='submit' value='Ticket Details'></form>
</div>
</div>";
}
echo "</div>";
echo "<br /><center><a href='myProfile.php'><h4>Go back to my profile</h4></a></center>";
include ('includes/footer.php');
} else {
echo "<br /> <br /><center><h3>Your search <i>'$terms_str'</i> did not match any appointments</h3></center>";
echo "<center><a href='myProfile.php'><h4>Go back to my profile</h4></a></center>";
echo "<br />";
exit;
}
?>
<?php
// clean up resultsets when we're done with them!
$query->close();
// close the connection.
$conn->close();
Perhaps it will help to explicitly group the terms:
$sql = "SELECT * FROM appointments WHERE email = '" . S_SESSION['email'] . "'";
$exprs = array();
foreach ($terms as $term) {
$exprs[] = "(issue LIKE '%$term%' OR id LIKE '%$term%')";
}
if (!empty($exprs)) {
$sql .= ' AND (' . join(' OR ', $exprs) . ')';
}
The result in this case will include records that matched any of the terms.
Note: It would be good to use a DB API like laravel/PDO/mysqli to simplify the query building and properly escape the values.

Searching multiple terms with php

I'm trying to get my search form to search for multiple terms.For example "Black dog".In my $keywords they are separated with commas (Black, dog).I know that i can just enter (Black dog, ) in my $keywords but I want them to be separated with commas.If I put single term (dog) it returns result for only that term.If I put 2 (Black dog) it returns 0 results.
<form class="form-inline pull-xs-right" action="search.php" method="POST" >
<input class="form-control" type="text" name="search" placeholder="Search">
<button class="btn btn-success-outline" name="searchh" type="submit" value=">>" >Search</button>
</form>
<?php
$i=null;
$search=null;
$result_query=null;
if(isset($_POST['search'])){
$get_value = addslashes($_POST['search']);
$search = mysqli_real_escape_string($con,"$get_value");
?>
<h2>Showing results for
<?php echo $_POST['search'] ?></h2>
<?php
if($search==''){
echo "<center><b>Please write something in the search box!</b></center>";
exit();
}
$search = $_POST['search'];
$terms = explode(" ", $search);
foreach ($terms as $each) {
$i++;
if ($i == 1)
$result_query .= "keywords LIKE '%$each%' ";
else
$result_query .= "OR keywords LIKE '%$each%' ";
}
$result_query = "SELECT * FROM content WHERE keywords LIKE '%".$search."%'";
$run_result = mysqli_query($con,$result_query);
// echo mysqli_num_rows($run_result);
// exit;
if(mysqli_num_rows($run_result)<1){
echo "<center>Sorry no matches found</center>";
exit();
}
while($row_result=mysqli_fetch_array($run_result))
{
$name=$row_result['name'];
$keywords=$row_result['keywords'];
$image=$row_result['image'];
$link=$row_result['link'];
?>

Search Engine Script - Undefined variables

I have a small problem in my code and I can't figure out what is it..
I'm trying to make a sample search engine tool in PHP , everything works good until i've tryied to search some posts from database... When I do that , it says I don't have $header and $bio declared ...
<?php
include('connection.php');
$query =mysqli_real_escape_string($dbc, $_POST['query']);
$q = mysqli_query($dbc, "SELECT id FROM search WHERE header LIKE '%$query%' OR bio LIKE '%$query%'");
$num = mysqli_num_rows($q);
echo $num;
if(!$query){
echo "Enter a query...";
} else {
if($num != 0)
{
echo "<hr>";
while ($fetch = mysqli_fetch_assoc($q)){
$id = $fetch['id'];
$header = $fetch['header'];
$bio = $fetch['bio'];
echo "<strong>" . $header . "</strong>";
echo "<blockquote><p>" . $bio . "</p></blockquote>";
echo "<hr>";
}
} else {
echo "No results where found .. ";
}
}
?>
and the form
<div style = "width:300px; margin:auto;">
<h1> Add Search Criteria</h1>
<p> Type a header and bio below to add to search engine</p>
<p>
<input id="header" name = "header" type="text" placeholder="header" style="width:100%;">
</p>
<p>
<textarea id="bio" name="bio" cols="40" rows="7" placeholder="Write a bio.."></textarea>
</p>
<p>
<center>
<button id="submit">Submit Search</button>
</center>
</p>
<div id="add_error" style="text-align:center"></div>
<hr>
<h1>Search The Database</h1>
<p>Please type something to search to database</p>
<p>
<input name = "query" id="query" type="text" placeholder="search">
<button id="search">Search</button>
</p>
<div id="search_error">
</div>
</div>
this is what it outputs
Notice: Undefined index: header in C:\wamp64\www\mywebsite\Search\search.php on line 25
Call Stack
Time Memory Function Location
1 0.0021 242472 {main}( ) ...\search.php:0
( ! ) Notice: Undefined index: bio in C:\wamp64\www\mywebsite\Search\search.php on line 26
Call Stack
Time Memory Function Location
1 0.0021 242472 {main}( ) ...\search.php:0
Add header and bio columns in your select query.
$q = mysqli_query($dbc, "SELECT id, header, bio FROM search WHERE header LIKE '%$query%' OR bio LIKE '%$query%'");

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>

creating a find and replace application in php

i have a php code :
<?php
$offset=0;
if(isset($_POST['text']) && isset($_POST['searchfor']) && isset($_POST['replacewith']))
{
$text=$_POST['text'];
$search=$_POST['searchfor'];
$replace=$_POST['replacewith'];
$search_length=strlen($search);
if(!empty($text)&&!empty($search)&&!empty($replace))
{
while($strpos=strpos($text,$search,$offset))
{
$offset=$strpos + $search_length;
$text=substr_replace($text,$replace,$strpos,$search_length);
}
echo $text;
}
else
{
echo 'pls fill in all fields';
}
}
?>
<form action='string_search.php' method='POST'>
<textarea name='text' rows='6' cols='30'></textarea><br><br>
Search For:
<input type='text' name='searchfor'><br><br>
Replace with:
<input type='text' name='replacewith'><br><br>
<input type='submit' value='find and replace'>
</form>
When i type a string.Forexample 'i found my dog' in textarea and search for 'dog' in 'search for' then replace with 'cat' in 'replace' then submit it will out put 'i found my cat'.However it always output the original string ('i found my dog').I don't know why ?
Why do you use:
while($strpos=strpos($text,$search,$offset)) {
$offset=$strpos + $search_length;
$text=substr_replace($text,$replace,$strpos,$search_length);
}
echo $text;
??
I think its the same if you just use:
$strpos=strpos($text,$search);
$text=substr_replace($text,$replace,$strpos,$search_length);
echo $text;
And probably is your while who is doing the bad work. I don't know, it's a suggestion. With this, your code should work.
<?php
$offset = 0;
if (isset($_POST['user_input_box']) && isset($_POST['search']) && isset($_POST['replace'])) {
$user_input_box = $_POST['user_input_box'];
$search = $_POST['search'];
$replace =$_POST['replace'];
//To check whether above code work or not
// echo $user_input_box = $_POST['user_input_box'];
// echo $search = $_POST['search'];
// echo $replace = $_POST['replace'];
$search_length = strlen($search);
if (!empty($_POST['user_input_box']) && !empty($_POST['search']) && !empty($_POST['replace'])) {
while ($string_position=strpos($user_input_box, $search ,$offset)) {
// to check while and strpos
// echo $string_position."<br>";
// echo $offset = $string_position + $search_length;
$offset = $string_position + $search_length;
$user_input_box = substr_replace($user_input_box, $replace,$string_position,$search_length );
}
echo $user_input_box;
}
else{
echo "Please fill the complete fields.";
}
}
?>
<form method="POST" action="eleven0.php">
<textarea cols="34" rows="12" name="user_input_box"></textarea>
<br><br>
<label>Search For: </label> <br>
<input type="text" name="search"><br><br>
<label>Replace With: </label><br>
<input type="text" name="replace"><br><br>
<input type="submit" value="Find and Replace">
</form>

Categories