Simple HTML/PHP Refuses to work - php

Here is poster dot php
<?php
$FILENAME = "poster.php";
$txt = "data.txt";
if (isset($_POST['field1']) && isset($_POST['field2'])) { // check if both
fields are set
$fh = fopen($txt, 'a');
$txt=$_POST['field1'].' - '.$_POST['field2'];
fwrite($fh,$txt); // Write information to the file
fclose($fh); // Close the file
}
?>
Here is my HTML form
<!DOCTYPE html>
<html>
<head>
<title>Field 1 & 2</title>
</head>
<body>
<form action="poster.php" method="post">
What is your name?<br>
<input type="text" name="field1"><br>
<input type="text" name="field2"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
I tried everything to make this work when i put it in a debugger it says the file should end on the first line. When I run the file all i get is a white screen. I'm in desperate need of help.

You are not writing anything after appending your form data in your poster.php file. You can get the file contents in the following way...
Html form:
<!DOCTYPE html>
<html>
<head>
<title>Field 1 & 2</title>
</head>
<body>
<form action="poster.php" method="post">
What is your name?<br>
<input type="text" name="field1"><br>
<input type="text" name="field2"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
and in poster.php
<?php
$FILENAME = "poster.php";
$txt = "data.txt";
if (isset($_POST['field1']) && isset($_POST['field2'])) { // check if both
//fields are set
$fh = fopen($txt, 'a');
$txt=$_POST['field1'].' - '.$_POST['field2'].PHP_EOL;
fwrite($fh,$txt); // Write information to the file
fclose($fh); // Close the file
echo file_get_contents("data.txt");
}
?>
As per requirement, write PHP code and HTML code inside a single file and remove the action attribute of the form in the following code ...
<?php
//$FILENAME = "poster.php";
$txt = "data.txt";
if (isset($_POST['field1']) && isset($_POST['field2'])) { // check if both
//fields are set
$fh = fopen($txt, 'a');
$txt=$_POST['field1'].' - '.$_POST['field2'].PHP_EOL ;
fwrite($fh,$txt); // Write information to the file
fclose($fh); // Close the file
//echo file_get_contents("data.txt");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Field 1 & 2</title>
</head>
<body>
<form action="" method="post">
What is your name?<br>
<input type="text" name="field1"><br>
<input type="text" name="field2"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

action attribute of form should be file name where you submit your form.
<form action="action.php" method="post">
Here it is 'action.php' but you have to submit it on poster dot php.

please check it:
<form action="poster.php" method="post">
Form action will be your main file name(poster.php) and after form post, you can easily get all the form fields data on poster.php file.
you wotre this file name -> poster dot php means poster.php ?

Related

I cant upload files

I made a site, and I wand to upload files through a php form, but when I try to do so, I have the following error:405 Not Allowed
nginx. Can anyone help me?
I have checked my php.ini file, and file_uploads were on by default
<!DOCTYPE html>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<form method="post">
Enter Your Text Here:<br>
<input type="text" name="textdata"><br>
<input type="submit" name="submit">
</form>
</body>
</html>
<?php
if(isset($_POST['textdata']))
{
$data=$_POST['textdata'];
$fp = fopen('data.txt', 'a');
fwrite($fp, $data);
fclose($fp);
}
?>

How can I get the output for form without the page changing

I am trying to get information from textarea to be converted to a .py file.
Here's my file https://jsfiddle.net/girlwhocancode/68mb49gq/
<form action=action.php method="post">
<center>
<textarea placeholder="Code you want to execute in python..."></textarea>
</center><br/>
<center><input type="submit" class="button_example"></center>
</form>
and this is my php file:
<?php
$path = #PATH;
if (isset($_POST['field1'])) {
$fh = fopen($path,"a+");
$string = $_POST['field1'];
fwrite($fh,$string); // Write information to the file
fclose($fh); // Close the file
}
?>
For some reason my php isn't working and I have no idea how to keep the same webpage after submitting
Try this:
<form action="action.php" method="post">
<center>
<textarea name="script" placeholder="Code you want to execute in python..."></textarea> //added name
</center><br/>
<center><input name="submit" type="submit" class="button_example"></center>
</form> // added name submit
in php
<?php
$path = #PATH;
if (isset($_POST['submit'])) {
$fh = fopen($path,"a+");
$string = $_POST['script'];
fwrite($fh,$string); // Write information to the file
fclose($fh); // Close the file
}
?>
Remember that in php $_POST/$_GET keys are always your form field
names

HTML Form not posting to PHP file

I am currently creating an HTML form that has 2 fields; name and an address. It also has a way of selecting one of 2 options. The form will either be used to look up the address of a person. In this case, only the name field is required to be filled out. The other option is to add a person to the file. In this case both fields need to be filled out. For some reason, I am not able to get the values that inputted from the form into my PHP file. Please help.
Here is my HTML Form
<html>
<head>
<title> Form </title>
</head>
<body>
<form action="action_page.php" method="post">
<div>
<label for="name">Name: </label>
<input id="name" type="text" name="name"><br>
</div>
<div>
<label for=address">Address: </label>
<input id="address" type="text" name="address"><br>
<input type="radio" name="action" value="lookup">Lookup<br>
<input type="radio" name="action" value="add">Add<br>
<input type="submit" name="submit"><br>
</form>
</body>
</html>
Here is my PHP file
<html>
<head>
<title> PHP </title>
</head>
<body>
<?php
$name = $_POST['name'];
echo "<p>",$_POST["name"],"</p>";
echo "<p>",$_POST["action"],"</p>";
echo "<p>",$_POST["address"],"</p>";
$address = array();
if($_POST["action"]=="lookup"){
$fh = fopen("address.txt","r") or die("No such file found.");
while(!feof($fh)) {
$line = fgets($fh);
$info = explode("|",$line);
$address[$info[0]]=$info[1];
}
if(array_key_exists($_POST["name"],$address)) {
echo "<p>",$_POST["name"],"<p>";
echo "<p>",$address[$_POST["name"]],"</p>";
}
?>
<body>
</html>
The error was in
echo "<p>",$_POST["name"],"</p>";
It should be
echo "<p>".$_POST["name"]."</p>";
and same for others

How to upload input to a text file in php

I want to upload the contents written in the text box in a text file using php. But also they should only be written if the string contains a particular pattern.
here's my php code
<?php
$data = $_POST["text"];
public function copy(){
if (strpos($data, '123') !== false){
$myfile = "uploads/mydata.txt";
$fh = fopen($myfile, 'w') or die("can't open file");
fwrite($fh, $data);
fclose($fh);
}
}
?>
It says unexpected public on line 4.
and my form is:
<?php require_once('tally.php') ?>
<!DOCTYPE html>
<html>
<head>
<title>
</title>
</head>
<body>
<form action="index.php" method="post">
Input the first record
<input type="text" name="text">
<input type="submit" name="submit" action="submit">
Submit
</submit>
</form>
</body>
</html>
Thanks for helping
why are you using fopen, just use file_put_contents.
The input name is text and in php you are trying to receive $_POST['data'].
Change name of input to data or change the php code and get $_POST['text'].
also your submit button is not xhtml or html5 standard compliant. change it to no need for closing tag or additional text.
I cannot see where are you calling the copy function.
code should be something like this.
<?php
$data = $_POST["data"];
if(!empty($data)) {
copy2($data);
}
function copy2($data){
if (strpos($data, '123') !== false){
$myfile = "uploads/mydata.txt";
$fh = fopen($myfile, 'w') or die("can't open file");
fwrite($fh, $data);
fclose($fh);
}
}
?>
HTML goes like this:
<?php require_once('tally.php') ?>
<!DOCTYPE html>
<html>
<head>
<title>
</title>
</head>
<body>
<form action="index.php" method="post">
Input the first record
<input type="text" name="text">
<input type="submit" value="submit">
</form>
</body>
</html>
Remove the word public if this function is not in a class.
public function copy(){
becomes
function copy(){
There is not tag associated with data. Please add:
<input type="text" name="data">

PHP write data from HTML to text file

I need to write some data from HTML form to data.txt file (the txt file have 777 permissions)
The HTML that I use is index.html:
<form action="index.php" method="POST">
<input name="field1" type="text" />
<input name="field2" type="text" />
<input type="submit" name="submit" value="Save Data">
And the PHP file is index.php:
<?php
$txt = "data.txt";
if (isset($_POST['field1']) && isset($_POST['field2'])) { // check if both fields are set
$fh = fopen($txt, 'a');
$txt=$_POST['field1'].' - '.$_POST['field2'];
fwrite($fh,$txt); // Write information to the file
fclose($fh); // Close the file
}
?>
But when I use the form to save the data, it show me a message box to download the index.php
what I am doing wrong?

Categories