How do you create a php file editor in php - 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>

Related

Creating a php redirect to a html slide

Hi I am trying to create a php statement the redirects you back to a set slide on my html slideshow on another page. However, although the data inserts into my database, it will not redirect back to the slide on my html page.
I have already tried this using html anchor pints, but they arent working.
my php file...
<?php
$url = 'Details.html';
$anchor = 'slide3';
if(mysqli_query($con,$sql)){
echo 'Request Submitted';
echo<<<JS
<script type="text/javascript">
document.location.href="$url?$query_string#$anchor";
</script>
JS;
} else {
die("Error: " . mysqli_error($con));
}
?>
and the slide I am trying to link it to...
<div class="mySlides w3-container w3-blue" id="slide3">
<h1 class="w3-wide w3-center"><b>BUSINESS DETAILS</b></h1>
<div class="w3-center w3-container w3-content w3-padding-16">
<form action="Addition.php" method="post">
Business Name : <input type="text" name="bname">
<br>
<br>
Business Type : <input type="text" name="btype">
<br>
<br>
<button class="w3-button w3-green"
onclick="plusDivs(1)">Submit and Continue</button>
</form>
</div>
</div>
https://www.php.net/manual/en/function.header.php
Don't mix PHP and JS unless you have a good reason to. Use header('Location: name_of_file.html'); and finally exit; or die(); to redirect.
<?php
$url = 'Details.html#slide3';
if (...){
header('Location: ' . $url);
exit;
}

putting html tags value into php variable

i wanna store html tags value in a variable then send them to database, is there any way for this?
function start(){
$title = $_POST['title'];
$text = $_POST['content'];
$src = $_POST['thumb'];
if(!empty($title) && !empty($text) && isset($_POST['sendpostbtn'])){
try{
include ("../config.php");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO `post` (`title`, `content` , `src`) VALUES ('$title','$text','$src')";
$pdo->exec($sql);
?>
<script type="text/javascript">
alert("successes")
</script>
<?php
}catch(PDOException $e){
echo $sql . "<br>" . $e->getMessage();
}
}else{
?>
<script type="text/javascript">
alert("somethings wrong")
</script>
<?php
}
}
?>
and this is my html code in the same file
<div class="sendpostBox">
<div class="lastpostTitle">
<p>new post</p>
</div>
<form action="sendpost.php" method="post">
<label>title</label>
<input type="text" name="title">
<label>src</label>
<input type="text" name="thumb">
<label>content</label>
<textarea name="content"></textarea>
<input type="submit" name="sendpostbtn" value="send">
</form>
</div>
I've created a form and then i want to send these data to database in the same page, i mean i don't want to open another page for this.
thanks
I've created a form and then i want to send these data to database in
the same page, i mean i don't want to open another page for this.
Simply empty the form action.
Example:
<form action="" method="POST">
......
....
..
</form>

PHP Forms With JQuery Requiring to press submit twice

So I want to replace the form submission with a thank you message after you submit, I need the PHP because this will eventually deal with databases in that php, however right now... The only way it works, is that is Type in a name, press submit. It goes back to a blank form, enter nothing (nothing in address) and submit again and it works...
right now the only way i could think of making it work would be some dummy checkbox where when checked value changes the post is sent. However i don't think that will pass with my groupmates
wondering how i can make it only have to submit once.
Index.PHP
<!DOCTYPE HTML>
<html>
<head>
<?php include_once "thankyou.php"; ?>
<script type="text/javascript" src="jquery-3.1.0.js"></script>
<script type="text/javascript">
$("document").ready(function() {
$("#ContactUs_Submit").click(function(evt) {
<?php $inputName = $_POST["ContactUs_Name"]; ?>
$("#ContactUs_CommentsDiv").replaceWith("<?php
thankyou($inputName); ?>");
return false;
});
});
</script>
</head>
<body>
<div id="ContactUs_CommentsDiv">
<form method="post">
<!-- WITH JQUERY USE SINGLE URL, WITH PAGES-->
<label for="ContactUs_Name">Name: </label>
<input type="text" name="ContactUs_Name" id="ContactUs_Name" />
<br/>
<Label for="ContactUs_Email">Email: </Label>
<input type="email" name="ContactUs_Email" id="ContactUs_Email" />
<br/>
<input id="ContactUs_Submit" type="submit">
</form>
</div>
</body>
</html>
thankyou.php
<?php
function thankyou($name) {
echo "<p> Thank you for your input ";
//if ($_POST["ContactUs_Name"] != "") {
// echo " " . $_POST["ContactUs_Name"];
// }
if ($name != ""){
echo $name;
}
echo "!";
}
?>
Can you not just use AJAX to do this?

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

PHP GET and POST - not working

I am a beginner with php and have the following code for getting values from a form and inserting it into a csv file as an array
PHP:
if(isset($_GET['submitted'])){
$csvData = [$_GET["contract"],$_GET["article"],$_GET["specs"]];
$fp = fopen("order.csv","a");
if($fp) {
fputcsv($fp,$csvData); // Write information to the file
fclose($fp); // Close the file
}
}
HTML :
<form action="add.php" method="GET" >
<label class="wsite-form-label" >Contract No <span class="form-required">*</span></label>
<div class="wsite-form-input-container">
<input name="contract" id='contract'>
</div>
<label class="wsite-form-label" >Article <span class="form-required">*</span></label>
<div class="wsite-form-input-container">
<input name="article">
</div>
<label class="wsite-form-label"> SPECS<span class="form-required">*</span></label>
<input name="specs">
<button type='submit' name="submitted" value="true" >Submit </button>
</form>
Have tried both get and post but still not able to get inserted value to my php. please help.
Here is the code
<?php
if(isset($_GET['submitted']))
{
$csvData = array($_GET["contract"],$_GET["article"],$_GET["specs"]);
$fp = fopen("order.csv","a");
if($fp)
fputcsv($fp,$csvData);
file fclose($fp); // Close the file
}
?>
<html>
<head>
<title>dfsdfdsdfsdfs</title>
</head>
<body>
<form action="test.php" method="GET" >
/* Your Form */
</form>
</body>
</html>
You can check like this:
if(!empty($_GET["contract"]) && !empty($_GET["article"]) && !empty($_GET["specs"])){
$csvData = array($_GET["contract"],$_GET["article"],$_GET["specs"]);
$fp = fopen("order.csv","a");
if($fp) {
fputcsv($fp,$csvData); // Write information to the file
fclose($fp); // Close the file
}
}
It seems that there is no errors in your code. You can check this by using
var_dump($csvData);
if it works then check the file pointer $fp
You can find more details about fopen() here

Categories