I have a simple HTML form comprising of check boxes and text fields.
When I submit this form it submits the data back to the same page where I capture the data and write it to a text file.
What I'd like to try and do is write some of the check box and field data to one text file and the rest to another.
Can this be done ? if it can how do I say which fields are written to which file ?
Thanks
you send your form and after that you handle the data. For example:
$dog = $_POST["dog"];
$cat = $_POST["cat"];
$fileForDogs = fopen('path://to/first/file', 'a+'); //a+ means if not exst, create one and write on the end of file
fwrite($fileForDogs, $dog);
fclose($fileForDogs);
$fileForCats = fopen('path://to/second/file', 'a+'); //a+ means if not exst, create one and write on the end of file
fwrite($fileForCats, $cat);
fclose($fileForCats);
I would highly recommend to look at databases for example mysql to save data from form.
Related
I have been trying for months to figure out how to fix and create what I am envisioning which I know is possible to be done and probably is not hard to do.
I am trying to take a textarea that I have placed on a page of mine upload its contents into a database where people can view the information they uploaded. Here's an example.
Person A copy/pastes text into a text area at: http://example.com/textarea/
he clicks an upload/submit button and gets a link like this: http://example.com/A93KJUQ21.txt
Anyone that has access to that link will be able to click it and it will display the contents that were uploaded to it. Whatever Person A, B, C, D, etc uploads it will generate a new unique link to the information. Example of this would be as follows:
http ://example.com/A93KJUQ21.txt
http ://example.com/JKO2QN498.txt
http ://example.com/PMNR01NEQ.txt
and so on..
Here is the code I currently have
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['upload'])) {
$textarea = $_POST['paste-area'];
//Add validations
$odb = new PDO("mysql:dbname=dbname;host=localhost", "dbusername",
"mypasswordgoeshere");
$query = $odb->prepare("INSERT INTO submission (`textarea`) VALUES
(:textarea)"); //I'm just making up the structures
$query->bindParam(':textarea', $textarea, PDO::PARAM_STR);
$status = $query->execute(); //$status contains true or false
//Other codes...
}
}
?>
Your table needs two columns, the random string and the textarea contents. When the user submits the form, you need to create the random string and insert that into the DB along with the text area.
$string = uniqid();
$query = $db->prepare("INSERT INTO submission (id, textarea) VALUES (:string, :textarea)");
$query->bindParam(':string', $string);
$query->bindParam(':textarea', $textarea);
$query->execute();
echo "Link is <a href='http://example.com/lookup.php?id=$string'>href='http://example.com/lookup.php?id=$string</a>";
I've made the link point to a PHP script. You can't retrieve from the database using a .txt URL, that just tries to download a regular file. You need to point to a PHP script that fetches the textarea from the submission table.
If you want to make it seem like a .txt file, you could use a rewrite rule on the server, that rewrites the .txt URL to the equivalent .php URL.
why do you think you need a database for this?
<?php
if (!empty($_POST['text'])) {
$filename = uniqid().".txt";
file_put_contents($filename, $_POST['text']);
die("http://".$_SERVER['HTTP_HOST']."/$filename");
}
?>
<form method=post>
<textarea name=text></textarea>
<input type=submit>
</form>
I have a form that saves text into the MySQL database. The user does not need to register to save that form. How do I make it so that when someone submit's a form, it will display their text that THEY typed in a saved. I want it to be able to retrieve they're text that they submitted at that time and possibly give it a link like this: http://www.example.com/text115612 (the numbers are for different texts...) and make the retrieved text display on that page?
When the user submits the text, you can save the text together with an ID, in your example text115612.
After they've submitted, a server side script will redirect the user to the newly created text (Serverside, because the user can't predict what the ID will be).
If you wish to make the texts a little more private, you can make a harder ID so it's not possible for people to guess it.
What you need to do is retrieve the text and create new file
with unique name
$userttext = $textfromdb // get the user text
$file = fopen('text_file_name.txt','w') // open a new file
frwite($file,$textfromdb); // write to the file
fclose($file); // close the file
$link = "http://website.com/"."text_file_name.txt"; // create the website link
When user submits the form, you would do something like this.
//insert your text//
INSERT INTO table_name (text) VALUES ('text');
//get the primary key that was just inserted//
//column must be auto-increment//
$textid = SELECT LAST_INSERT_ID();
//you can also do this directly in PHP//
$textid = $mysqli->insert_id;
echo "http://www.example.com/" . $textid;
I have a CSV file that contains many data.
I use PHP to read and view it in WEB. I need to have a functionality like ADD, EDIT and DELETE. I already done the ADD using the a or append. I will add data in HTML Table and automatically it will add also in CSV file. I didn't use database here.
My problem now is the EDIT. What if i want to edit some data in CSV file using the HTML Table and like the ADD i want to automatically update it to my CSV file. How can i do that?
I already tried the w, w+, r+ but it didn't work. By the way I am using CODEIGNITER here.
Thank you in advance for the help.
I would keep the logic on server and do all the csv rendering eventdriven.
Here you have a good csv lib: https://github.com/goodby/csv
So the process would be something like this:
Client: Load csv preformated (via php) from server and display in html
Client: Do MVVM bindings etc.
Client: If an event is called (like add, update etc.) send
eventrequest to server
Server: Recieve event -> Process (with transactions if needed) -> reformat csv, save it
and send back to client
Client: Retrieve and apply MvvM bindings
Otherwise you need redundant csv logic in client and server whats not a fine approach.
Do you know what I mean or should I do you an example?
The csv library supports complex csv binding and easy appending like:
$csv = new parseCSV();
$csv->parse('yourcsv.csv');
$csv->data[1] = array('updatekey' => 'new Value');
$csv->save();
Client side example:
//retrieve your csv (view rendered or async with ajax
var csv;
var mutableDataArray = $.csv.toArray(csv);
//do manipulation here
//for example
mutableDataArray[0].name = 'A new name to first record';
//serialize back
var newCsv = "";
for(var i in mutableDataArray) {
var data = mutableDataArray[i].join(",");
newCsv += i < mutableDataArray[i].length ? data + "\n" : data;
}
//do something with newCsv
I have written a HTML form to collect user's inputs for an order and also a PHP program to receive the order when Submit button is pressed. In addition I got to update a text file stored on the web server to reflect the order items. Can anyone explain how am I to go about updating a text file stored on the server? Thanks..
You should lock the file to protect the file from getting clobbered by concurrent updates sent by several users. There is a full example of locking and writing to a file in the flock function documentation: http://es.php.net/manual/en/function.flock.php
Or, to save yourself the trouble, use a proper database. SQLite is easy to use and requires no setting up: http://es.php.net/manual/en/book.sqlite3.php
Use fwrite:
$fp = fopen('data.txt', 'w');
fwrite($fp, $yourData);
fclose($fp);
UPDATE:
If I understood you right you need something like this:
if(!empty($noOfApples)){
$fp = fopen('data.txt', 'w+');
$count=fread($fp,filesize('data.txt'));
$count+=$noOfApples;
fwrite($fp, $count);
fclose($fp);
}
Simplest way to do this AND preserve array structure of form fields is to dump $_POST via serialize.
Write example, after a user clicks submit:
file_put_contents('myfile.txt', serialize( $_POST ) );
Read example:
$data = unserialize(file_get_contents('myfile.txt'));
The form field would look something like:
<input type="text" name="myfield" value="<?php echo $data['myfield'] ?>" />
Alternatively, you can technically do $_POST = unseralize(file_get_contents(... but it will obviously overwrite anything that a user might input.
Just store the user input with the key which should be the input name and the value which should be the content user input. In this way, you can get the user input in an array.
Then you can translate the key-value pair into a string with serialize function. Now you can store the string into file with those code:
$fp = fopen('user{$id}.txt', 'w');//replace the {$id} with user id
fwrite($fp, $dateFromUser);
fclose($fp);
When you want to show the user input, just read the file and unserialize the string you get from the file and fill the input with the data which is stored in the array with the same key. When you want to update the user input just do the process above again.
I'm using CKeditor but I'm not working out how to create the php file that stores editing textareas in MySql DB.
I'm quite newbie in php/mysql..
javascript code of CKeditor calls is:
$(’#my_div’).ckeip({
e_url: ’test.php’,
data: {
example_var : ’example_value’,
example_var2 : ’example_value2’
}
)};
What I have to write in test.php to making it store data of ckeditor in MySql?
Should I create a new table in database first ?
thanks a lot
I think the best way to handle this is create a new MYSQL table with two columns. One as an id for the textarea and then the other column to store the text. For a basic tutorial about MYSQL and PHP check out W3School http://www.w3schools.com/PHP/php_mysql_intro.asp
I am a newbie to CKEditor, too. But it seems like it would be quite easy once you get the data from the editor into a javascript variable, like:
var editorData = $('my_div').ckeditorGet().getData();
You can then put editorData into a hidden input field and then submit it to the PHP file that you're going to create. On that PHP file you would take it with something like:
$theData = $_GET["name_of_the_hidden_input_field"];
//here comes the PHP code for inserting $theData into mysql