从HTML编写textarea到PHP文件

从HTML编写textarea到PHP文件

问题描述:

I'm new to & so please go easy on me!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Add</title>
</head>

<font size="3">

<div id="TextBox">add</div>
<form name="theform" action="add.php" method="get">
<div id="TextBox-Field" style="margin-top:20px;"><textarea class="TextBox" type="text" name="TextBox" required></textarea></div>

<form name="theform" action="DisplayText.html">
<input type="submit" value="Show">
</font> 
</body>

I've written this in HTML and i would like it to be able to save the text that the user enters into a file. Preferably I would like that to be done with add.php rather than adding php code to this, if possible?

I'm slightly confused as to where to start, i've googled and watched tutorials but still do not understand

Thank You!

Step 1:

Change this

<form name="theform" action="DisplayText.html">

to

<form name="theform" action="add.php" method="post">

Step 2:

add.php

<?php
if(isset($_POST['TextBox']))
{
 file_put_contents('somefile.txt',$_POST['TextBox'],FILE_APPEND);
 echo "Data written successfully";
}

if you want to save that than you need to create a databse and then create a table of same number of columns as the data entered html elements you using then on add.php make a connection : in mysql:

 <?php
// Create connection
   mysqli_connect(host,username,password,dbname);
   //for eg $con=mysqli_connect("example.com","peter","abc123","my_db");

 // Check connection
 if (mysqli_connect_errno())
 {
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }

 $var1 = $_GET['TextBox'];
 mysqli_query($con,"INSERT INTO <tablename> (<columnname>) 
 VALUES ($var1)");

?>

You are missing to close html tag.......like forgot to open body tag and to close form

<font size="3">

<div id="TextBox">add</div>
<form name="theform" action="add.php" method="get">
<div id="TextBox-Field" style="margin-top:20px;"><textarea class="TextBox" type="text" name="TextBox" required></textarea></div>

<form name="theform" action="DisplayText.html">
<input type="submit" value="Show">
</font> 
</body>

so replace above code and place following code.it will help you

 <body>
<div id="TextBox" style="font-size:3;">add</div>
<form name="theform" action="add.php" method="get">

<div id="TextBox-Field" style="margin-top:20px;  style="font-size:3;">
<textarea class="TextBox" type="text" name="TextBox" required></textarea>
</div>

 </form>

<form name="theform" action="DisplayText.html">
<input type="submit" value="Show">
</form> 
</body>