Jump to content
Sign in to follow this  
absorr

Creating Html With Html

Recommended Posts

i want to make it so that what you put in an HTML forum when sent will add to a separate HTML page with the input from the forum inserted into code. For example, on a forum in typeit.html you type "My Name" into the name text box and "This Is My Message" into the message box. You clock submit and it sends it to the PHP page which sends <b>My Name</b><br><p>This Is My message</p><br> to a separate HTML page called mytxt.htmlNote that was an example and what the names of pages and forums are above are different in reality.I already have a code to simple print the text as it is but i don't know how to make code with PHP.Here is the code that I have so far: in photo_main.html <form action="upload_file.php" method="post"enctype="multipart/form-data"><input type="file" name="file" id="file" /> <br />Title:<INPUT TYPE="text" NAME="fname"><br>Description:<INPUT TYPE="text" NAME="age"><br>Photo Album:<INPUT TYPE="text" NAME="alb"><br><input type="submit" name="submit" value="Submit" /></form> in upload_file.php <?php if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("images/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "images/" . $_FILES["file"]["name"]); echo "Stored in: " . "images/" . $_FILES["file"]["name"]; $a_str = array($_POST["fname"],$_POST["age"],$_POST["alb"],$_FILES["file"]["name"]); $contents = implode(PHP_EOL, $a_str); $contents .= PHP_EOL . PHP_EOL; file_put_contents("photos.txt", $contents, FILE_APPEND); print("|$contents|"); } }?> I want to make it so that instead of text going to a txt file I want code going to an HTML file so that i don't have to keep manually putting in uploaded photos. I am going to use an iframe to put in the HTML to a page. If there is a better way than an iframe that would be great too. I hope I was not confusing.

Share this post


Link to post
Share on other sites

---Deleted--- wouldn't work

Edited by Krewe

Share this post


Link to post
Share on other sites

Why don't you use a database?

Share this post


Link to post
Share on other sites

Basically, when the person submits the data from the form, you can have a PHP script insert that information into a table in a database. Then, on the separate HTML page (or anywhere), you can query the database again to retrieve that information and display it dynamically. This way you don't have to have loads and loads of static HTML files lying around. Have a look at the W3Schools tutorial: http://w3schools.com/php/php_mysql_intro.asp. Try getting it to work without images first (just the name and message).

Share this post


Link to post
Share on other sites

This confuses me. How does this help me create an HTML element with this? I don't think you understand what I'm asking for. I want it so that when you press submit on photos_main.html the php file puts <img src="(photo)"><br><b>(title here)</b><br><p>(description)</p> in an html file

Edited by absorr

Share this post


Link to post
Share on other sites

Oh you were refering to the iframe part. Thanks for that but i wanted s better solution for iframe for some thing else, not this issue, but thats one less post to this forum I have to make. Can you help with the main issue now?

Share this post


Link to post
Share on other sites

It's much more flexible if you save your data to a database like Synook suggested and then have your page read the data that's in there to generate the img tag dynamically, but if you're really set on having it write static files instead then you can use any of the file functions to work with files. You can write an entire file using file_put_contents, you can open a file with fopen and then write to it using fwrite, etc. That's less flexible then having your pages read the data from the database and dynamically build the page though. http://www.php.net/manual/en/function.file-put-contents.phphttp://www.php.net/manual/en/function.fopen.phphttp://www.php.net/manual/en/function.fwrite.php

Share this post


Link to post
Share on other sites

The point of a dynamic data source like a database is that you don't need the new HTML file. Rather, you just have one HTML (PHP) file with the markup you so desire, and then the information is pulled from the database. E.g., mytxt.php:

<!-- rest of HTML --><?php$person = $_GET['person']; // get the person you want the page generated for$data = get_data_from_database($person); // using the functions in the tutorialecho "<img src=\"{$data['photo_path']}\"><br><b>{$data['name']}</b><br><p>{$data['description']}</p>";?><!-- rest of HTML -->

Then when you want the page for "Synook", you could just request mytxt.php?person=Synook, and similarly for everyone else. Furthermore, this can be generalised to just load everyone's information at once. By going down your "new file and then include everything", you are severely overcomplicating and restricting things. By using a database you can solve both problems at once, and you don't need to create new files all the time. Do you really think any real-world application uses the scheme you are thinking of?

Share this post


Link to post
Share on other sites

Check justsomeguy's post for that — you basically can just use file_put_contents() like you already are. You can write HTML tags to the document just as you write normal text — it makes no difference. E.g.

file_put_contents("mytxt.html", "<b>{$_POST['fname']}</b><br><p>{$_POST['age']}</p><br>");

Share this post


Link to post
Share on other sites
Check justsomeguy's post for that — you basically can just use file_put_contents() like you already are. You can write HTML tags to the document just as you write normal text — it makes no difference. E.g.
file_put_contents("mytxt.html", "<b>{$_POST['fname']}</b><br><p>{$_POST['age']}</p><br>");

THANK YOU! That worked perfectly!

Share this post


Link to post
Share on other sites

Ok, now I tried doing that with the image and it won't let it. Reading the error report, I discovered it is because when it sees the quotation marks in <img src="example"> it thinks that the text part of the string ended and gets confused with the {} you have to put the uploaded file in. How do I make it so that it will see " as text to add in instead of part of the code?

Share this post


Link to post
Share on other sites

You can escape them by prefixing them with the backslash character:

echo "<img src=\"example\">";

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...