PHP database search issue - php

I have an issue with some code. It's a HTML file that sends a string to a PHP file which then searches an offline database and it returns a link to the html page. Each time I search it states there are no results found.
<?php $searchfor=$_GET['keyword'];
$contents=file_get_contents('/users/tutors/mhtest15/share/shakespeare/home.html', true);
if (stripos($contents, $searchfor) !==false) {
$startpos=stripos($contents, $searchfor);
$getcode=substr($contents, $startpos, 150);
$isolate=explode('"', $getcode);
$sendlinkback='https://ebooks.adelaide.edu.au/s/shakespeare/william/'.$isolate[1];
echo "<br> <a href ='".$sendlinkback ."'>The file $searchfor Exists here</a>";
}
else if ($isolate !=='$searchfor') {
echo "<li>no results to display</li>";
}
?>
<html>
<head>
<title>William Shakespeare Archive Search Engine</title>
</head>
<body>
<p>
<p>
<div id="container">
<center><img src="https://s-media-cache-ak0.pinimg.com/originals/fc/20/b4/fc20b40a4447af0bc71746bf47d2849e.jpg" alt="Shakespeare Image" height="400" width="350">
<h1>Shakespeare Search Engine</h1>
</center>
<div style="text-align: center">
<br>
<form action='test.php' method="GET" id="search" name="search">
<input class="button" type="submit" value="Search">
<input name='search' style="width:200px;font-size:14pt;" placeholder="Search term...">
</form>
</div>
</div>
</body>
</html>
Thanks in advance for any help provided.

The first issue is, you are checking the $_GET['keyword'] variable, but your input name is search.
Change:
$searchfor=$_GET['keyword'];
To:
$searchfor=$_GET['search'];
If you can show us the format of your html file would be great.
And change file path to this:
https://ebooks.adelaide.edu.au/s/shakespeare/william/
UPDATE:
Finally, something like this should work (but it's still ugly)
$searchfor=$_GET['search'];
$contents=file_get_contents('https://ebooks.adelaide.edu.au/s/shakespeare/william/', true);
if (stripos($contents, $searchfor) !==false) {
$startpos=stripos($contents, $searchfor);
$getcode=substr($contents, $startpos, 150);
$isolate=explode('"', $getcode);
$sendlinkback='https://ebooks.adelaide.edu.au/s/shakespeare/william/'.$isolate[0];
echo "<br> <a href ='".$sendlinkback ."'>The file ".preg_replace('/^>/','',strip_tags($isolate[1]))." Exists here</a>";
}

Related

Unable to write to txt file in php

I'm trying to simply overwrite a txt file with a number entered in a text box in PHP. Both the PHP and text file are in the html directory of my apache2 server. Every time it executes it just displays the die() string. I also tried using fwrite() with the same results. Any help would be much appreciated.
<HTML>
<TITLE>Home Automation Interface</TITLE>
<BODY>
<H1>Air Conditioning Power(Relay 1)</H1>
<H2>Turn AC On/Off</H2>
<H3>
<p>
<form action="relayToggle.php" method="get">
<input type="submit" value="AC Power">
</form>
</p>
<form method= "post" action = "homeInter.php">
<p>
<label for="acTemp">AC temperature (F):</label><br/>
<input type="text" id="temp" name="temp">
</p>
<button type="submit" name="submit" value="send">Change Temperature</button>
</form>
<p><strong>
<?php
$temp= $_POST['temp'];
echo $temp;
file_put_contents("/var/www/html/temp.txt", (string)$temp) or die("Unable to open file!");
?>
}
}
?>
</strong>
</p>
</H3>
</BODY>
</HTML>
Wrap your PHP code Ina valid checker to see if form was submitted
Like this:
if ($_POST):
//All functions go here
endif;
If the file is in the same directory as your PHP file just change the directory to the file name.file Extension in your file_put_content()

Php put content: Data erased

I have this simple web content editor.
<?php
if (isset($_POST['edit'])) {
if (file_put_contents('homecontent.txt', $_POST['homecontent']) !== FALSE)
echo '<p class="success">Home Content Saved Successfully!</p>', "\n";
}
$homecontent = file_get_contents('homecontent.txt');
if (isset($_POST['header'])) {
if (file_put_contents('headercontent.txt', $_POST['headercontent']) !== FALSE)
echo '<p class="success">Header Content Saved Successfully!</p>', "\n";
}
$headercontent = file_get_contents('headercontent.txt');
if (isset($_POST['cssfile'])) {
if (file_put_contents('style.css', $_POST['cssfile']) !== FALSE)
echo '<p class="success>Style.css Saved Successfully!</p>', "\n";
}
$cssfile = file_get_contents('style.css');
?>
<div id="forms">
<form method="post" action="">
<p>Here you can edit your homepage text:</p>
<textarea name="homecontent" id="homecontent" rows="20"><?php echo $homecontent?></textarea>
<p><buton type="submit" name="edit">Save changes</button></p>
</form>
<form method="post" action="">
<p>Here you can edit your header content:</p>
<textarea name="headercontent" id="headercontent" rows="10"><?php echo $headercontent?></textarea>
<p><button type="submit" name="header">Save changes</button></p>
</form> </div>
<form method="post" action="">
<p>Here you can edit style.css file:</p>
<textarea name="cssfile" id="cssfile" rows="10"><?php echo $css?></textarea>
<p><button type="submit" name="cssfile">Save changes</button></p>
</form>
The problem in this script is that 3rd form action is not getting executed. I get success message, but style.css file not written & it also erases any existing content. The first 2 form actions are working perfectly fine.
It cannot be a directory permission error because other 2 file are working.
Your submit button is named same as the textarea, so it overwrites it. Change the form to
<form method="post" action="">
<p>Here you can edit style.css file:</p>
<textarea name="cssfile" id="cssfilecontent" rows="10"><?php echo $cssfile; ?></textarea>
<p><button type="submit" name="cssfile">Save changes</button></p>
</form>
Up in the PHP section, you will have to change
if (isset($_POST['cssfile'])) {
if (file_put_contents('style.css', $_POST['cssfilecontent']) !== FALSE)
echo '<p class="success>Style.css Saved Successfully!</p>', "\n";
}
$cssfile = file_get_contents('style.css');
And, of course, <p><buton type="submit" in the first form misses a t. The </div> which is after the second form should be after the third, I suppose.
Thanks for all the suggestion. I finally got it working
if (isset($_POST['cssfile']))
{
if (file_put_contents('style.txt', $_POST['csscontent']) !== FALSE) echo '<p class="success">Style.css Saved Successfully!</p>', "\n";
}
$csscontent = file_get_contents('style.txt');
?>
<html><body>
<form method="post" action="">
<p>Here you can edit style.css file:</p>
<textarea name="csscontent" id="csscontent" rows="10"><?php echo $csscontent ?></textarea>
<p><button type="submit" name="cssfile">Save changes</button></p>
</form>
</body>
</html>
The textarea name & id should be the same. The same name should be entered on put_content $_POST

Printing html page and doing some action using 1 button

I have a Print button which I want to do 2 actions when clicked:
Updating my database.
Printing the HTML page.
This is what I've done so far, but it's not working:
<form action="" method="POST">
<body >
<?php
$n=$_POST['ID'];
$a=implode("</br>",$n);
list($add, $ward) = explode("(!#!)", $a);
?>
<div id="container">
<p id="address">
<?php echo"$address";?>
</p>
<p id="ward">
<?php echo"$ward";?>
</p>
</div>
<input type="submit" name="Print" value="Print" />
<?php
if(isset($_POST['Print']))
{
?><script>javascript:window.print()</script><?php
mysql_query("UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1");
}?>
<div id="footer">
</div>
</form>
After using this print button , my database is getting updated, but the print window is showing error(i.e the variables posted from other page are showing errors).
Can anyone please help me print and update at same time with this Print button?
If your page does not have it, add form tags. They are mandatory unless you want to AJAX the data. Also remove the semi-colon from the end of the sql
<form action="" method="POST">
<input type="submit" name="Print" value="Print" />
</form>
<?php
if(isset($_POST['Print'])) {
mysql_query("UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1");
?><script>window.print();</script>
<?php } ?>
UPDATE: Perhaps you mean this, but I do not want to keep correcting HTML.
<?php
$n=$_POST["ID"];
$a=implode("</br>",$n);
list($add,$ward)=explode("(!#!)", $a);
?>
<body>
<div id="headerbg">
<div id="header-e1"><a align="left" href="escalationReport.php">Back </a>
</div>
<div id="header-e3"><a align="right" href="logout.php">Logout </a>
</div>
<h1><p>Issue Notice</h1>
</div>
<div id="container">
<p id="address">
<?php echo "$address";?>
</p>
<p id="ward">
<?php echo "$ward";?>
</p>
</div>
<form action="" method="POST">
<input type="submit" name="Print" value="Print" />
</form>
<?php if(isset($_POST[ 'Print'])) {
mysql_query( "UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1");
?>
<script>
window.print();
</script>
<?php } ?>
<div id="footer"></div>
</body>
you have an error in the update query, you have placed semicolon inside the query so please remove that so the query will be like this.
Make sure your form method is POST
mysql_query("UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1");
Make sure you are using input type button inside the form tag. And use post method for form.

How do you create a php file editor in php

I am trying to create a php file that can edit other php files on my website. I am able to do this except for when there is html in the php file that I want to edit. Since I am using a textarea to display/edit the php file contents, what I have built does not work when there is a textarea tag in the php file that I want to edit. What I have so far is below. The solution does not need to resemble this.
<?php
if ($_POST['file_text']){
file_put_contents($_POST['filename'], $_POST['file_text']);
$filename = $_POST['filename'];
echo "<script>
window.location = '_editor.php?filenm=$filename'
</script>";
}
else {
$myfilename = $_GET['filenm'];
if(file_exists($myfilename)){
$file_text= file_get_contents($myfilename);
}
echo "
<h3>$myfilename</h3>
<form name='input' action='_editor.php?filenm=$myfilename' method='post'>
<textarea name='contrib_entrybox' id='contrib_entrybox' rows='50' cols='180'>
$file_text
</textarea>";
?>
<?php
// configuration
$url = 'http://domain.com/backend/editor.php';
$yourfilePath = '/path/to/txt/file';
// check if form has been submitted
if (isset($_POST['text'])){
// save the text contents
file_put_contents($yourfilePath, $_POST['text']);
// redirect to form again
header(sprintf('Location: %s', $url));
printf('Moved.', htmlspecialchars($url));
exit();
}
// read the textfile
$text = file_get_contents($yourfilePath);
?>
<!-- HTML form -->
<form action="" method="post">
<textarea name="text"><?php echo htmlspecialchars($text) ?></textarea>
<input type="submit" />
<input type="reset" />
</form>
<div id="sample">
<script type="text/javascript" src="http://js.nicedit.com/nicEdit-latest.js"></script> <script type="text/javascript">
bkLib.onDomLoaded(function() { nicEditors.allTextAreas() });
</script>
<h4>
Second Textarea
</h4>
<textarea name="area2" style="width: 100%;">
Some Initial Content was in this textarea
</textarea><br />
</div>

Why does my entire code disappear if I remove my form tag?

NOTE: Here is the fiddle, and since that looks awful, Here is the full screen result
Here is the fiddle after I do my changes on my computer and generate the html, and here is the full screen
Note on the fiddle:
I added the entire code into the fiddle since it won't work unless it's the whole code.
Thanks to anyone who will give hints! My only problem here is the one in the title, nothing else. :)
=====
I'm working on an undergraduate project to create an interface that accesses a database (though I'm not a CS student). My code is behaving very strangely. I have code for one window that displays one large div encasing three divs side byside, which is structured this way:
<div id="container">
<div id="window1">
<form></form> //my buttons
</div>
<div id="window2">
<form></form> //my body
</div>
<div id="window3">
//I havent written anything here yet
</div>
</div>
But I realized it makes more sense for me to encase them all in just one form. Like this:
<div id="container">
<form>
<div id="tab1">
</div>
<div id="tab2">
</div>
<div id="tab3">
</div>
</form>
</div>
So in the first case, this is what my website looks like:
But after I change it:
What's weird is, if I put forms in my forms, it starts working again! So this works:
<div id="container">
<form>
<div id="tab1">
<form></form>
</div>
<div id="tab2">
<form></form>
</div>
<div id="tab3">
</div>
</form>
</div>
If you need to see my code, I actually am using PHP to generate the html since it's a lot, so (this is the method that doesn't work):
<div id="search_separator" class="upper_tab">
<form action="" method="post">
<div id="button_container">
<?php
$tbs_a = 1;
$tbs_name = 'table_selector';
$printRetain_button = '';
$fg = input::get($tbs_name);
echo '<label for="',$tbs_name,'"></label>';
foreach($table_arrays AS $ta => $table){
$tbs_val = "table".$tbs_a;
if($tbs_a==1){
echo '<div class="button"><input type="radio" class="tabl_selector" name="',$tbs_name,'" value="',$tbs_val,'" checked="default"/><div class="table_selector">',$ta,'</div></div>';
}else{
echo '<div class="button"><input type="radio" class="tabl_selector" name="',$tbs_name,'" value="',$tbs_val,'"';
if($tbs_val == $fg){
echo ' checked="checked"';
}//make a separate echo for table1 radio button with the default turned on.
echo '/><div class="table_selector">',$ta,'</div></div>';
}
$tbs_a++;
}
?>
<!--<input type="submit" name="submit" value="submit" />-->
</div>
<div id="search_container" class="content">
<?php
$a = 1;
$search = DB::getInstance();
foreach($table_arrays AS $t_a => $table){
$y = 1;
echo '<div id="table',$a,'_content" class="table_content">';
echo "<h2>",$t_a,'</h2><table align="center" id="actualhtmltable">';
foreach($table AS $t => $field){
$name = "table".$a."_field".$y;
$val_sticky = escape(input::get($name));
$val_find = $search->get($t_a, array($t, '=', input::get($name)));
//$val_find = $search->get('user_credentials', array('user_id', '=', 28))->results();
if(!empty($val_find)){
$val = array();
foreach($val_find[0] AS $vfz){
$val[] = $vfz;
}
} elseif(!empty($val_sticky)){
$valu = $val_sticky;
} else {
$valu = "";
}
echo '<tr><div><td>',$t,'</td><td><label for="',$name,'"><input type="text" class="entryfields" name="',$name,'" value="';
if(!empty($val[$y-1])){
echo $val[$y-1];
} else {
echo $valu;
}
echo '" /></label></td></div></tr>';
$y++;
}
echo '</table></div>';
$a++;
}
?>
<div class="Tableformbutton">
<div class="FindModeButtons">
<input type="Submit" name="find" value="Find" id="find" class="findupdateinsert" />
<input type="Submit" name="update" value="Update" id="update" class="findupdateinsert" />
</div>
<input type="Submit" name="insert" value="Insert" id="insert" class="findupdateinsert" />
</div>
</div>
<div id="other_container">
<!--find mode on and other functionalities-->
</div>
<div class="hassubmit" id="searchsubmit_container">
<input type="button" id="findmodetoggler" name="query_submit" value="Toggle Find Mode" class="top_buttons" />
<a href="#" class="tip"><input="button" name="Help" value="Help" class="top_buttons" id="help" />
<span> Click "Toggle Find Mode" to toggle between find/update and insert buttons.<br />
If find mode is on, you will be able to look for existing records and update them. Otherwise, you can only insert new entries.<br />
Find mode is off by default.</span>
<p>Need Help?</p>
</a>
</div>
</form>
</div>
As far as I can tell, it is supposed to work! I don't see anything in my HTML, and as for my CSS, I have styled nothing about forms in there, only divs and labels and input tags. What could be wrong?
Thanks to anyone who'll give hints or ideas!
I found the solution, which really is just so simple. But, I don't know why it happened this way.
I added an id="search_form" into the form I need, then used CSS to say .search_form{ height: 100% } as apparently, when the form was made, it made the height of everything into just a few pixels. This is weird, but at least I found the answer.

Categories