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;
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 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.
I have a website at localhost:8888/documents/index.php.
In this file, I load a random row from my MySQL table. I have around 1000 rows, and it will randomly pick one row, and return some data from that row into my page. For example, if it chooses row 467 with:
name = George
age = 23
key = fe4v6
It will show on my index.php something like: hello, George (23 years old). The key is a random, unique variable for each row.
What I want: I want my URL in the address bar to update automatically on refresh with the key attribute. So I want: localhost:8888/documents/index.php?key=fe4v6. When I refresh the page, I want it to update the URL with the new key value for the corresponding row in my MySQL database.
I don't work with forms. I make a connection with my database and I use queries and fetch_assoc() on the random row. In my index.php I show the name like this:
<?php echo $row['name'] ?>
I don't know how easy or difficult this is, maybe there is a much easier solution. I would like to hear from you, thanks!
A quick solution would be to use a header redirect
if(!isset($_GET['UniqueKey'] {
header('location: localhost:8888/documents/index.php?UniqueKey='.$row['name']);
}
The code firsts checks if UniqueKey is set, so it doesn't redirect infinitely.
You can use header as follows. Add this snippet after you have the row value to be passed in as the query string parameter.
<?php header('Location: localhost:8888/documents/index.php?key='.$row['key']); ?>
You could make use of the History API if you only need to support fairly recent browsers.
<script>
var path = "<?php echo "localhost:8888/documents/index.php?key=".$row['key']; ?>";
var page_title = "Custom Title";
history.pushState({}, page_title, path);
document.title = page_title;
</script>
This will update the path to add the key as soon as this code is run in the browser (i.e. during the page load). No reload is required so there's no need to program any logic to prevent a new random row being assigned.
I am having a difficulty of finding a answer that is simple to follow and understand... I making a site (on dreamweaver cs5) and the frontpage of the site has a select city and a dropdown of all the major cities and a email text box.
I have a few questions and those are as follows:
What is my next step now that I have the html pretty much done... do I connect it to a database?
What php script would I need to make sere both fields (the city and email) are filled? and where would I enter this php script?
Here is some of my code in case you were wondering:
http://answers.yahoo.com/question/index?qid=20110611111223AAeAnrT (had to put it on here because overflow wouldn't let me put in code..)
Just a quick starter, to get your form up.
On creating your PHP script, I don't know much about Dreamweaver, but if you have PHP installed on your server, you should be able to create it in the same directory as your HTML. All of the form elements should be in a form tag, and should point to the PHP, such as <form method="POST" action="dosomething.php"> before and </form> after.
Also, a "Please select your city..." might be nice to have on the form, like <option value="unset">Please select your city...</option>. (Also, all of the options in the sample should have a value attribute).
I don't know how much you have learned about PHP, so I'm going to try to start with the basics. The PHP script would be a plain-text file with the extension .php, such as dosomething.php. Inside of the script, the PHP code needs to be surrounded by the PHP start and end tags, <?php and ?>.
The values inputed into the form should be accessible with the $_POST variables in PHP, so in the script $_POST['select'] will be set to the current value. I recommend setting the names to something you can remember, such as selectedCity and emailAddress.
In our PHP script, we will want to get the variables from our form, and check to see if they are both filled. Then the data will get written to the database. I have created a sample snippet below that is commented, but extra security should be added, and this code should not be used as-is.
<?php
$city = $_POST['selectedCity']; // Get the city the user selected from the form
$addr = $_POST['emailAddress']; // Save the email address the user entered
if($city == "unset")
{
// Stops user if a city hasn't been selected
die("Please select a city."); // Stop executing code, and tell user to go back and select a city
}
if($addr == "")
{
// Stops user if the email address is blank (also would be good to make sure email address is correct, like user#domain.com)
die("Please enter a valid email address");
}
if(!file_exists("../mailinglist.sqlite"))
{
// Creates the database if it doesn't exist
// The database should be outside the document root (meaning you can't access it through the web)
$db = sqlite_open("../mailinglist.sqlite"); // Opens the database, creates it if non-existent (it is)
sqlite_query("CREATE TABLE users (city, email)"); // Creates a table for users
}
else
{
$db = sqlite_open("../mailinglist.sqlite"); // Opens the database if it exists
}
sqlite_query("INSERT INTO users (city, email) VALUES ('".sqlite_escape_string($city)."','".sqlite_escape_string($city)."')"); // Add the new user to the database
?>
(Anything that you need help with that is in blue should be searchable on the PHP Documentation)
The code above will take the output from the HTML form, check to make sure that it is not empty, and enter it into a database, creating a new database if it does not exist. Again, this is just a starter, and the code needs to be improved before taking it live.
Hope this helps!
I'm trying to use the Joomla framework to make a create in the main content table.[http://docs.joomla.org/How_to_use_the_JTable_class] This works fine except that some data comes from posted variables and some from logic that happens when a file is uploaded moments before (store the random image name of a jpg)
$data=&JRequest::get('post');
this takes a ref to the posted values and I want to add to this Array or Object my field. The code I have makes the new record but the column images, doesnt get my string inserted.
I am trying to do something like$data=&JRequest::get('post');
$newdata=(array)$data;
array_push($newdata,"images"=>"Dog");
i make newdata as data is a ref to the posted variables and i suspect wont there fore allow me to add values to $data.
I'm a flash guy normally not a php and my knowledge is letting me down here.
Thanks for any help
Right, first thing:
$data=&JRequest::get('post');
$data is an array, you do not have to cast it. To add another element to the array as described in the comments do this:
$data['images'] = 'cats';
If you are using normal SQL to do the insert then you would do something like this to get the last inserted id e.g. the id of the row you just inserted:
$db = $this->getDBO();
$query = 'Some sql';
$db->setQuery($query);
if (!$db->query()) {
JError::raiseWarning(100, 'Insert failed - '.$db->getErrorMsg());
}
$id = $db->insertid();
If you are developing in Joomla I suggest you use the db functions provided to you rather than mysql_insert_id();
[EDIT]
If you want to use store then you can get the last inserted id like so:
$row->bind($data);
$row->check();
$row->store();
$lastId = $row->id;