My php page has the HTML and PHP all on the same page.
For the HTML section, user data is inputted and posted to the PHP section.
</div>
</header>
<pre>
</pre>
<h1 style="font-family: 'Lucida Sans Typewriter'"> Note entry:</h1>
<form method="post">
<div>
<div style="margin-right:5px;">
<input type="text" class="fileName" name="fileName" id="fileName" size="35"
value="untitled">
<input type="text" name="noteData" id="notes" size="250">
<input type="Submit" name="Submit" value="Save">
</div>
</div>
</form>
As for the PHP section, I check to see if a POST has occured, then save file and filename to disk, yet this does not occur.
if($_POST){
$noteName = $_POST['fileName'];
$noteData = $_POST['notes'];
$notes = fopen('' + $noteName,"wb");
fwrite($notes,$noteData);
fclose($notes);
}
The errors thrown are here:
https://gyazo.com/99646e2705ed7ea927d27c67cddb87b4
Any suggestions? I need this finished soon and cannot for the life of me discover why this is deciding not to work.
You have to use names to get data using $_POST. And use . to concatenate words in php. So change the line as:
if($_POST){
$noteName = $_POST['fileName'];
$noteData = $_POST['noteData'];
$notes = fopen('' . $noteName,"wb");
fwrite($notes,$noteData);
fclose($notes);
}
You should use names, not ID. Try $_POST[‘noteData’].
Related
Previously I wrote a PHP code that includes an HTML form. So basically, the user inputs info in the HTML form and if all necessary input is filled, then the PHP file directs information to another PHP file. This is the HTML form I made This is the PHP File code (I will only put one variable of the form so that it's easier to understand:
<?php
session_start();
include('config/db_connect.php');
$Telephone = '';
$errors = array('Telephone' => '');
if(isset($_POST['submit'])){
// check Telephone
if(empty($_POST['Telephone'])){
$errors['Telephone'] = 'A Telephone number is required';
} else{
$Telephone = $_POST['Telephone'];
$_SESSION['Telephone'] = $Telephone;
if(!filter_var($Telephone, FILTER_VALIDATE_INT)){
$errors['Telephone'] = 'Telephone must be a valid Telephone number';
}
if(array_filter($errors)){
//echo 'errors in form';
} else {
// escape sql chars
header('Location: OTP.php');
}
} // end POST check
?>
<!DOCTYPE html>
<html>
<?php include('templates/header.php'); ?>
<section class="container grey-text">
<h4 class="center">Add a form</h4>
<body>
<form class="white" action="add.php" method="POST">
<label style="font-size: 16px">Your Telephone</label>
<input type="text" name="Telephone" value="<?php echo htmlspecialchars($Telephone) ?>">
<div class="red-text"><?php echo $errors['Telephone']; ?></div><p></p>
<div class="center">
<input type="submit" name="submit" value="Submit" class="btn brand z-depth-0">
</div>
</form>
</body>
</section>
<?php include('templates/footer.php'); ?>
</html>
Anyway, due to some reason, now I need to make the HTML form in a separate HTML file. So when users inputs the data in that form, the data has to be transferred to the PHP file I mentioned above and the innit['submit'] has to take place to further process it from there. In other words, previously the HTML form within the PHP file was filled and when 'submit' was clicked, the PHP file redirected it to another PHP file, but now the HTML form is in a HTML file itself and I want that when the form in that file has a click on 'submit', the PHP file is forced to follow the same procedure as it did previously. However, I cannot make the HTML file transfer the info to PHP and the PHP to directly go into the innit['submit'] function.
Here's the HTML part of the code (in the new HTML file):
<form action="work/add.php" method="POST">
<div id="Group_206">
<div id="Text_ca">
<span>電話號碼</span>
</div>
<input class="TextBox03" type="text" id="Telephone" name="Telephone">
<div id="Phone_name_position">
</div>
<div id="Group_159_m">
<input type="submit" value="Submit" >
</div>
The answer is as given in the comment by brombeer:
if(isset($_POST['submit'])){ will never be triggered, there is no
HTML element with name="submit" – brombeer
would it be possible to have a html/php template on index.php say for example (a news webpage template and then anyone can edit the title, paragraphs only, then on submit it then sends the webpage with the data stored to a paste bin like url so who ever visits that url say http://localhost/news/jjeh3bndjks they would only be able to view to content and not edit.
I would like to use something like this
<?php
if ($_POST) {
$pasteID = uniqid();
$paste = fopen("pastes/".$pasteID.".php", "w");
$contents = $_POST['pasteContents'];
fwrite($paste, $contents);
header('Location: /pastes/'.$pasteID.'.php');
}
?>
<form action="" method="POST">
<input type="text" name="pasteContents" placeholder="write here" />
<button type="submit" tabindex="0">submit</button>
</form>
but for some reason when i add another input box or try to send anymore data it fails or just gives me the last input given is there a way to send a whole page this way?
any help would be appreciated
You can use file_get_contents with the following code:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
parse_str(file_get_contents('php://input'));
echo param1 . '<br />' . param2;
} else {
?>
<form method="post">
<input type="text" name="param1" value="param1" />
<input type="text" name="param2" value="param2" />
<input type="submit" value="submit" />
</form>
<?php } ?>
(You can test it here)
Although, I did success to use $_POST too:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
echo $_POST['param1'] . '<br />' . $_POST['param2'];
} else {
?>
<form method="post">
<input type="text" name="param1" value="param1" />
<input type="text" name="param2" value="param2" />
<input type="submit" value="submit" />
</form>
<?php } ?>
Here
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$fn=$_POST["filename"];
$content=$_POST["txt"];
$fp= fopen("abc.txt", "w") or die("unable to open file");
fclose($fp);
}
?>
<html>
<head>
<title>Files</title>
</head>
<body>
<center>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post" id="show">
<input type="text" name="filename" placeholder="File Name"><br>
<textarea name="txt" id="txtarea" id="txtarea" rows="20" cols="50" placeholder="Text goes here..."></textarea><br>
<input id="ixtbtn" type="submit" id="tabuttton" value="Done">
</form>
</center>
</body>
</html>
Php is automatically getting called on page load. But I need it to be called when submit button is pressed.
<?php
if (isset($_POST['submitted']))
{
$fn=$_POST["filename"];
$content=$_POST["txt"];
$fp= fopen("abc.txt", "w") or die("unable to open file");
fclose($fp);
}
?>
<html>
<head>
<title>Files</title>
</head>
<body>
<center>
<?php echo "<form action='" . htmlspecialchars(#_SERVER['PHP_SELF']) . "' method='post' id='show'>"; ?>
<input type="text" name="filename" placeholder="File Name"><br>
<textarea name="txt" id="txtarea" id="txtarea" rows="20" cols="50" placeholder="Text goes here..."></textarea><br>
<input id="ixtbtn" type="submit" id="tabuttton" value="Done" name='submitted'>
</form>
</center>
</body>
</html>
Played with your code a bit and noticed two things: (1) your statement is outside your php code block and in straight html. (2) On my system (Firefox or Safari on OSX) that meant that the php $_SERVER['PHP_SELF'] was never translated back to php, it was treated as text and the post command kept posting to the server looking for a URL starting with the php If I use echo inside php to output your HTML, the variables ($_SERVER) are not substituted and go in as straight text. So I put your html into a string variable first to translate the variables and then echoed your html. Saved the file as an PHP file instead of as an html file and your code worked just fine. So, four net changes.
Move the closing php statement (?>) to the end of the file
copy the HTML into a php string
$outputString = "";
$outputString = $outputString.'<!DOCTYPE html>';
$outputString = $outputString.'<html><head><meta charset="utf-8"><title>Files</title>';
$outputString = $outputString.'</head><body><center>';
$outputString = $outputString.'<form action="'.$_SERVER['PHP_SELF'].'" method="post" id="show">';
$outputString = $outputString.'<input type="text" name="filename" placeholder="File Name"><br>';
$outputString = $outputString.'<textarea name="txt" id="txtarea" id="txtarea" rows="20" cols="50" placeholder="Text goes here..."></textarea><br>';
$outputString = $outputString.'<input id="ixtbtn" type="submit" id="tabuttton" name="myButton" value="Done">';
$outputString = $outputString.'</form></center></body></html>';
Echo the string
Save as .php instead of .html
I have issue saving textarea to file. I used POST method to send the form to the other page then, in the next page I can't include the textarea content with the file Im not sure what is the problem.
Is there any idea about what is the problem?
Here are the two pages:
page1:
<!DOCTYPE HTML>
<html>
<head>
<title>Save</title>
</head>
<body>
<form action="page2.php" method="post">
<span>name:</span>
<input type="text" name="name"><br>
<span>file extension: </span>
<select name="ext" id="ext">
<option value=".txt">.txt</option>
<option value=".doc">.doc</option>
</select>
<textarea name="txt1" id="txt1" cols="15" rows="10"></textarea>
<br>
<input type="submit" name="submit" id="submit" value="Save">
</form>
<br>
</body>
</html>
-page2.php
$txt1 = $_POST['txt1']; //textarea
$name = $_POST['name'];
$ext = $_POST['ext']; //choose from multiple extensions
if ($ext == '.txt') // In case if I want to add more than extension.
{
$file = "'. $name$ext.'" ;
$output = "$txt1";
file_put_contents($file, $output);
$text = file_get_contents($file);
header("Content-Description: File Transfer");
header("Content-Type: application/text/plain");
header("Content-Disposition: attachment; filename=".basename($file));
ob_clean();
flush();
readfile($file);
exit;
}
Without seeing your html I can't be sure of what the problem is. But its been my experience that when your having trouble accessing POST vars on the server side that it's probably a simple spelling error. Make sure the name attributes in your form line up with your POST vars. Just my two cents.
I have no idea what did you mean with your code, so, I'd just rewrite it
To save a file on the server you need these 2 lines
$name = basename($_POST['name']).'.txt';
file_put_contents($name, $_POST['txt1']);
$file = "'. $name$ext.'";
should be:
$file = $name.$ext;
At least that's what I had to change to get it to work on my server.
You need to add an id to the form, then add the form Id to the textarea element. For example:
<form action="page2.php" method="post" id="myform">
<textarea name="txt1" id="txt1" cols="15" rows="10" form="myform"></textarea>
Try using the wrap element in your textarea
<textarea name="txt1" id="txt1" cols="15" rows="10"></textarea>
add wrap
<textarea name="txt1" id="txt1" cols="15" rows="10" wrap="virtual"></textarea>
you can also use wrap: off, hard, soft and physical
In your database make sure the field txt1 is defined properly (i.e type text).
have a form that a name has been entered and need to add it to xml file.
index.html
<form name="form" action="insert.php" method="post">
<label for="name">Name:</label> <br />
<input type="text" name="name" id="name" /> <br />
<button type="submit" id="button">Submit</button>
<br />
<span id="validate"></span>
</form>
insert.php
header('Location:index.php');
$xmldoc = new DOMDocument();
$xmldoc->load('recentUploads.xml');
$Name = $_POST['name'];
$root = $xmldoc->firstChild;
$fileName = $xmldoc->createElement('name');
$root->appendChild($fileName);
$newText1 = $xmldoc->createTextNode($Name);
$fileName->appendChild($newText1);
$xmldoc->save('recentUploads.xml');
but i can not add anything to the xml file?
Help!
You are placing a lot of burden on poor variable $fileName:
$fileName = $_POST['name'];
$fileName = $xmldoc-> createElement('name');
On the other hand, $Name is not defined when you use it in line
$newText1 = $xmldoc->createTextNode($Name);
Me thinks these two incidents are related and one $fileName should actually be a $Name.
http://www.php.net/manual/en/domdocument.save.php
Are you remembering to save() it back?
I don't see that in your example code...
Do you need to call $xmldoc->saveXML();?