edit a file via php - php

I've look around, and not found anything yet. So I'm going to ask a question, sorry if this is 'nooby'
What I want to do is this:
Have a page with a form on it, IE: Form.php
and I want to allow myself to edit another page, IE: index.php
Kind of like a really BASIC two page CMS, edit it on the form.php page, and then it saves on index.php doesn't overwrite, but saves it under the current post that's already there.
Sorry for the 'vague' question, however want to do this fast :P

You can do it the way Patashu said with a database but if your only going to be editing one page you can do it with http://php.net/manual/en/function.fopen.php.
On the Form.php have it open index.php using PHP then have a textarea field that echos out the index.php and then using the Fopen function save over index.php. Make sure you secure Form.php with a password or even using a database. It also depends what programming language you will have inside index.php

Instead of editing HTML or PHP using a PHP file, you should make a database, store content in it that you want to be dynamic, and retrieve from the database when the PHP for the dynamic page executes. Go read about SQL :)
(reposting so it can be chosen as selected answer, if you want!)

Related

How to properly communicate a parent PHP with embeeded PHP?

You may remember that not long ago I asked you for some help with objects transfering through $_SESSION in PHP (more: Data loss during sending via $_SESSION from one script to another ). Currently I'm reworking the whole project and I don't have a foggiest idea how to do it properly. I mean, how to do it "according to the art", not "at any cost / the easiest (but nasty) way".
Here's the mockup of what I want to achieve (animated gif):
As you can see, I want my website to display at startup only the navigation, in which I want user to set his starting parameters. In the future, the navigation bar will have ability to be collapsed (to extend the data display section's height). When user sets the parameters (or not - for default view) and clicks "FILTER AND SORT" (which probably will be renamed to "GENERATE"), the second section should appear with data filtered and sorted as user defined.
Data is read from CSV file and stored in PHP Objects defined by my custom classes I've shown you in the question linked above. So I need to communicate data between 5 files:
FRONTEND:
index.php <-- my main file handling the website
FRONTEND-BACKEND:
navigation.php <-- file that shall be displayed in the navigation div
data.php <-- file that shall be displayed in the data section div / iframe (?)
popup.php <-- file that appears in a div over the index.php when certain DataBox is clicked; handled with tinybox2.
BACKEND:
classes.php <-- file with definitions of classes (properties and methods)
tinybox2 "library".
My communication flow is as to be:
navigation.php displayed on the top of index.php sends filtering and sorting parameters to data.php, making it appear on the bottom of index.php.
data.php and popup.php shall use classes contained in classes.php constructed from CSV file datafile.csv.
When a DataBox is clicked in data.php, popup.php appears over index.php content to display extended object data.
So, how should I do this, to make it properly? Which option will be best: $_POST, $_SESSION or maybe something else? Maybe due to embeeding all files in / over index.php it is possible to store all data in such a way, that no communication is needed? How to embeed files - with include, require or maybe other way?
There are lots of assisting questions, but still, the most important is: how to do it properly? Thank you for your help in advance :)
PS For certainty, please find below one additional usecases:
UC1: Standard use of display system
User enters website with the system
User chooses parameters for filtering and sorting
User starts generating view
Data is being read from *.csv file
During above, data is being filtered and sorted
Data is being displayed by the system to the user
Extensions:
5a. No data to display: system displays empty data section
6a. User want's to filter and sort again: back to step 2.
UC2 (OPTIONAL): User wants to share the data
UC1
User chooses an option to share data
System displays question if user wants to send it to printer, or e-mail
User chooses option (for this case: e-mail)
System asks for e-mail address
User provides e-mail address
System sends an e-mail with the current data.php data as it is displayed on website.
PPS I know I should show you some code snippets, but my current code is a mess with lots of interchanging html, php and comments, that cannot be cut of from the system (or would take me ages to clean it up to show it here). I'm asking you for help mostly, because I really want to remodel the current solution, therefore I'm rewriting the code from zero, using old one as a hint, not a template. Like Microsoft did with Windows Vista and Windows 7 :P Hope you'd understand ^^'
Okay, so here it is. The ultimate answer to a question some understands as too wide.
What was the case:
I wanted to achieve a two-section page, where the first (partent) section takes user input and uses it to filter the data in the second section. The problem was with providing parameters from the parent side to the invoked one.
What I had to change in my conception:
At first I thought about passing data with $_SESSION or other PHP way, BUT there is one major problem with such way of thinking:
As PHP is server side interpreted, it cannot dynamically add another page without refresh!
I know I didn't intent a new lightbulb or explore America, but it's still this conclusion what leaded me to accept the fact, that JavaScript usage is inevitable. I think most beginners will find this important: you need to completely change your way of thinking, as website ain't the same thing as traditional application. After this, it finally came to me (thanks to #Yoshi) that my initial problems with jQuery weren't a good reason to hold back from using it and I had to find out what they were.
Solution comes here:
Okay, so what is the solution?
I've started up with finding out what was wrong with my jQuery and... it was wrong way of interpreting "what is the scope of code included into php". If I include view/header.htm file into index.php that lays in the root directory, it will behave as it was in the root directory, not view directory. And that was the stupidest, as well as not uncommon error that one can make.
After finding this out (thank you #Yoshi again!):
I've created backend (model) for my app that can:
Read data from file
Put data into a class-based storage (that will in the future be then exported into a php file as "pseudo-database")
Then I've created a kind-of controller, that can use the model to pass data to a variable in my view.
At least I've created a view for my site, with the "parent site" (it has a backend part with a main-controller, and a frontend part with a view), and a "invoked site" that is opened through jQuery request, and contains a filtering backend and the main data view.
These 3 not-so-easy at first, but now so-so-essential, steps took me here. Please find some php-pseudocode below:
model_Data.php
<?php
// Class for single instance of data storage
model_DataSet.php
<?php
// Class for set of data instances storage
model_ReadData.php
<?php
// Code for reading CSV file with fgetcsv function
controller_DataMaster.php
<?php
require_once 'model_Data.php';
require_once 'model_DataSet.php';
require_once 'model_ReadData.php';
// Read data from file to a variable
$reader = new ReadData();
$reader->setConfiguration(//some setup);
$tmp_data = $reader->read("filename");
// Put data into the class
$dataSet = DataSet($tmp_data);
return $dataSet;
index.php (which is the main controller!)
<?php
header('Content-Type: text/html; charset="UTF-8"');
include 'view/header.htm';
header.htm contains HTML headers, includes of jQuery and jQuery-UI (for sake of datepicker) as well as css references, and (of course) physical header of the website with the input form displayed.
view.php
<?php
$import = include 'controller_DataMaster.php';
$usableData = $import->getAllDataSingles();
if (isset($_POST['data_from_the_form']))
{
// Do something!!!
}
?>
<table>
<tr>
<?php $i = 1; foreach($usableData as $singleData): ?>
<td><pre><?php print_r($singleData->getAllProperties()); ?></pre></td>
<?php if ($i++ % 3 == 0): ?>
</tr><tr>
<?php endif; ?>
<?php endforeach; ?>
</tr>
</table>
And this way you can achieve something that looks like this (currently - without any CSS applied):
So here it is. An easy, not very long, not very short answer to my own question. Case closed :)

Call a post inside an include ( i think )

I'm trying to learn PHP, and to do so im trying to create a code to register new products, i've made an loggin access where the person to do so needs to be connected, so inside of the cpanel I did differents sections, like add client, add product...
So i did an index in the _cpanel folde where i have my main html and to not repeat the same html i put an include inside this index so i can call others php files inside.
I dont know if its correctly to do like this BUT, as i said Im new at programming..
But everything works pretty good, until i need to call my post in my url..
like.. to add a new product i need to go to :
localhost -> connect... to go to localhost/_cpanel
if i click in add a new produt, i go to localhost/_cpanel/?page=folder/addnewproduct.php
and inside this php file i put a form with an action="?go=addnewproduct"
so, i did everything to put all the info in my db, but when i submit it goes to
localhost/_cpanel/?go=addnewproduct and nothing happens..
i tried to change the action like action="folder/?page=addnewproduct.php&?go=addnewproduct" but it doesnt work either.
BUT i made it works changing to action"folder/addnewproduct.php?go=newproduct
but to do so i need to go to "addnewproduct.php", and this page only is an included page inside my index..
i could call back to the index page after adding a new product, so that it doesnt show this page only, but when posting it loads fast to the page and after than it go back to my _cpanel.
i tried to find solutions but, didnt find... i think its just a problem with the way i put it in my action, maybe theres a way to do it and i dont know it yet.
I don't think I get what you're wanting exactly, but here you go:
If you're trying to redirect the request, you may just use the header function as follows:
header('Location: target/url.php');
If you want to make a external post request with php, you may use curl, as suggested here:
PHP + curl, HTTP POST sample code?

completely replace an HTML file with another using PHP

I am working on a html form, which I only want to be usable once at all.
So if one person has filled it in, another person should not be able to fill it in again. Not even the same person, and not even if he or she clicks on going back in his or her browser.
So far I have multiple ways in mind what could work:
Use PHP to completely replace the form.html page with another .html file, which I have already stored on the server for this purpose, or
Use PHP to partly replace the form.html page, even though it has no added value over completely replacing the html page, or
Adding a hidden field to my form.html file (which I have to change to a .php page in this case), posting a "1" to a database which I can retrieve everytime the form.php page loads in a way that it shows the form when a 0 is set, and doesn't when a 1 is set. In this case I also wonder if there isn't a simpeler solution to create a database just to store one "1".
I know how to use google, and usually I always manage to find a solution, but now I am feeling clueless. Please someone help me with a simple solution.
I have used the rename command at the end of my mail script, and this way it works:
mail($to, $subject, $comment, $headers);
rename('../mailtest/replace/emailform.html', '../mailtest/emailform.html');
?>

How to make a url parameter stick in php

Okay php newbie here, please bear with me. I am not sure if this is a redundant question but here goes. I have a reference code i want to stick to my url. example: site.com/index.php?refcode=123. That's fine right? we can put anything on there. Naturally the visitor goes to the index page. But if the visitor then clicks on other buttons that leads to other pages in my site, the parameter is gone. Like I want to track which code the visitor has when he sends me an email when he later decides to go to my contact page. How can this be done with php? or can this be done with jquery?
You would be best off saving the url variable into a session variable instead. The session variable will stick with the user so you have access to it no matter what page they go to.
$_SESSION['refcode'] = $_GET['refcode'];
Make sure to use session_start()
But if you do want to do it the way you have asked you can modify all urls on your page and add:
'?'.$_SERVER['QUERY_STRING']
This will add the query string to your url so the next page they go to would still have it. But that does seem like a lot more work.

How to share a form between 2 websites

I have a form which I'd like to share between 2 different websites. The form is submitted and the data is entered into the database.
Right now each website has its own copy of the script and its own database, so when I want to make updates to the form, I have to make those changes twice. Is there a way to share the form between the 2 websites, yet make it look like its being served by each website like normal. I'm also wondering if it's possible to make all the data go to one database.
The basic options would be...
You could use an html iframe to show the same form on multiple websites.
You could copy the form code between sites
If both websites are on the same server, you may be able to get a php include to include the form (this is possible in some cases even if they are not)
You can certainlly get the database to share information, just ensure the user you use to connect to it is allowed to connect from anywhere - not just localhost and you can connect to the database remotely.
You could include the form inside the other website as an iframe.
There is a short tutorial here on howto do that.
In case the form is displayed inside a whole complex page i recommend placing the form inside its own page and iclude it in both websites using an iframe.
depends what you are looking for, if you use the same process script behind it, do what Mouhannad said, and add a field "return_url" so you come back on the right page.
if it is that you want to send the fields to 2 different locations, create a proxy script and post it by a curl to both locations.
you can simply make both forms (in both websites) point to the same php page (in one of the websites)
<form action="http://www.example.com/action.php" ... >
Then check for the url reference to send the user back to the same page
The user won't feel any difference and you will have the database connection in one place
good luck!
Edit:
I thought your main concerns were the backend code since you mentioned the database. You could use iframes as suggested by the others but I always try to avoid them as much as I can. You can find lots of material online about why you should avoid them.
The other solution is use cURL to get the form's html code from the external page and keep using my above suggestion to change the action url path. This way you have the same code both for the backend and frontend. The downside is that you are making an additional request to get the form's html code which adds to the performance of your website - caching should improve that!
You can use CURL to simulate form submitting on another host, just send POST request with $_POST data.

Categories