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
Related
would it be possible to have a html/php template on index.php say for example (a news webpage template and then anyone can edit the title, paragraphs only, then on submit it then sends the webpage with the data stored to a paste bin like url so who ever visits that url say http://localhost/news/jjeh3bndjks they would only be able to view to content and not edit.
I would like to use something like this
<?php
if ($_POST) {
$pasteID = uniqid();
$paste = fopen("pastes/".$pasteID.".php", "w");
$contents = $_POST['pasteContents'];
fwrite($paste, $contents);
header('Location: /pastes/'.$pasteID.'.php');
}
?>
<form action="" method="POST">
<input type="text" name="pasteContents" placeholder="write here" />
<button type="submit" tabindex="0">submit</button>
</form>
but for some reason when i add another input box or try to send anymore data it fails or just gives me the last input given is there a way to send a whole page this way?
any help would be appreciated
You can use file_get_contents with the following code:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
parse_str(file_get_contents('php://input'));
echo param1 . '<br />' . param2;
} else {
?>
<form method="post">
<input type="text" name="param1" value="param1" />
<input type="text" name="param2" value="param2" />
<input type="submit" value="submit" />
</form>
<?php } ?>
(You can test it here)
Although, I did success to use $_POST too:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
echo $_POST['param1'] . '<br />' . $_POST['param2'];
} else {
?>
<form method="post">
<input type="text" name="param1" value="param1" />
<input type="text" name="param2" value="param2" />
<input type="submit" value="submit" />
</form>
<?php } ?>
Here
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>";
}
<form id="form" name="form" method="post" action="">
<div class="jquery-script-clear"></div>
<h1>BARCODE GENERATOR</h1>
<div id="generator"> Please fill in the code :
<input type="text" name="barcodeValue" id="barcodeValue" value="1234"><br> <br>
</div>
<div id="submit">
<input type="button" onclick="generateBarcode();" value=" Generate "> <input type="button" onclick="printDiv('print')" value="Print" />
</div>
</form>
<?php
$barcodeValue = $_POST["barcodeValue"];
$save = file_get_contents("save.txt");
$save = "$barcodeValue" . $save;
file_put_contents("save.txt", $save);
echo $save;
?>
sample picture
How to save the input data in save.txt file. When i clicked generate button the text file not showing in same folder.
The problem with your code is you have no submit button so your form was not actually posting when you pressed the button. if you look at my edits you can see I changed the button from input type="button" to type="submit". That allows for the form to submit back to the same php script.
Your script also was causing errors because you accessed $_POST["barcodeValue"] without checking if it existed. You also have to check if the save.txt exists before reading from it. If analyze my edits you can see how checking if the variables are available will help quite a bit.
<form id="form" name="form" method="post" action="">
<div class="jquery-script-clear"></div>
<h1>BARCODE GENERATOR</h1>
<div id="generator"> Please fill in the code :
<input type="text" name="barcodeValue" id="barcodeValue" value="1234"><br> <br>
</div>
<div id="submit">
<input type="submit" value=" Generate ">
</div>
</form>
<?php
if(isset($_POST["barcodeValue"]))
{
$barcodeValue = $_POST["barcodeValue"];
if(file_exists("save.txt"))
$save = file_get_contents("save.txt");
else
$save = "";
$save = $barcodeValue . $save;
file_put_contents("save.txt", $save);
echo $save;
}
?>
Let me know if you need more help
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>
Can anyone please help me on this:
I am trying to add a classname/ change the css of div using jquery; after I click submit on the form. Means, after clicking submit and when the page reloads this classname/ changes should stick to the target element. I am using php to get the values of form submitted. (no ajax, but if necessary I can modify my code). I tried to do this by jquery but when the page reloads the changes are gone !
Thanks.
update: Here is the sample code(so.php itself) I am working on. after form is submitted and page is reloaded; say I want to add a class to input that is clicked and manage the style in CSS:
<html>
<head>
</head>
<body>
<form id="myForm" action="so.php" method="get">
<input name="Load" type="submit" value="Google" />
<input name="Load" type="submit" value="MSN" />
<input name="Load" type="submit" value="Yahoo" />
</form>
<div id="container">
<?php
if (isset($_GET['Load'])) {
$value = $_GET['Load'];
echo "<iframe width=\"560\" height=\"349\" src=\"http://www.{$value}.com\"></iframe>";
}
?>
</div>
</body>
</html>
update2: I tried the logic provided by #NoPyGod and it worked. But here is the thing. I am trying to add a class to the button that is selected as well as update the text in the div with id "hello" with the value of input that is clicked or in active state once the form is submitted and page is reloaded. This is the one I ended up but once form is submitted
all the inputs are having the same class name as well the text in div is also not updated.
<html>
<head>
<?php if (isset($_GET['Load'])): ?>
<script>
$(document).ready(function() {
$('#hello').text('Hello');
});
</script>
<?php endif; ?>
</head>
<body>
<form id="myForm" action="so.php" method="get">
<input class="<?php if (isset($_GET['Load'])) { echo "some_class"; } if (isset($_GET['Load1'])) { echo "some_class1"; }?>" name="Load" type="submit" value="Google" />
<input class="<?php if (isset($_GET['Load'])) { echo "some_class"; } if (isset($_GET['Load1'])) { echo "some_class1"; }?>" name="Load1" type="submit" value="Rediff" />
<input class="<?php if (isset($_GET['Load'])) { echo "some_class"; } if (isset($_GET['Load1'])) { echo "some_class1"; }?>" name="Load" type="submit" value="Yahoo" />
<input class="<?php if (isset($_GET['Load'])) { echo "some_class"; } if (isset($_GET['Load1'])) { echo "some_class1"; }?>" name="Load1" type="submit" value="Sify" />
</form>
<div id="container" >
<?php
if (isset($_GET['Load'])) {
$value = $_GET['Load'];
echo "<iframe width=\"560\" height=\"349\" src=\"http://www.{$value}.com\"></iframe>";
}
if (isset($_GET['Load1'])) {
$value = $_GET['Load1'];
echo "<iframe width=\"560\" height=\"349\" src=\"http://www.{$value}.com\"></iframe>";
}
?>
</div>
<div id="hello"></div>
</body>
</html>
When the page is submitted, set a variable like so
$was_submitted = true;
Then in your HTML do something like this
<div id="myid" class="<?php if ($was_submitted) { echo "some_class"; } ?>">
Or if you absolutely insist on using jQuery to change it, include this in your html --
<?php if ($was_submitted): ?>
<script>
$(document).ready(function() {
$("#myid").addClass("some_class");
});
</script>
<?php endif; ?>
Updated:
<html>
<head>
</head>
<body>
<form id="myForm" action="so.php" method="get">
<input name="Load" type="submit" value="Google" />
<input name="Load" type="submit" value="MSN" />
<input name="Load" type="submit" value="Yahoo" />
</form>
<!-- I changed this line below -->
<div id="container" class=<?php if (isset($_GET['Load'])) { echo "some_class"; } ?>>
<?php
if (isset($_GET['Load'])) {
$value = $_GET['Load'];
echo "<iframe width=\"560\" height=\"349\" src=\"http://www.{$value}.com\"></iframe>";
}
?>
</div>
</body>
</html>