PHP SELF is automatically called on page load - php

<?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

Related

How do you make button save form data on a csv file and redirect you to a new page at the same time?

I am trying to make a button that saves form data on a file and redirects you afterwards, but although the save function works just fine, the redirection doesn't happen. I've tried only href and formaction so far. Any suggestions?
Paste bin : pastebin
Thank you for your time reading this !
the code:
<html>
<head></head>
<footer>
<form method="post">
<input type="radio" id="html" name="fav_language" value="private_number" checked="checked">
<label for="html">private_number</label><br>
<input type="radio" id="css" name="fav_language" value="clieant_number">
<label for="css">clieant_number</label><br>
<input type="text" id="fname" name="fname" maxlength="11" autocomplete="off" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*?)\..*/g, '$1');" required >
<button type="submit" name="BUY"><i class="arrow right"></i></button>
</form>
</footer>
<?php
$numurs = $_POST['fname'];
$Partners = $_POST['fav_language'];
if ($numurs){
$f = fopen('numurs.csv', 'w');
fputcsv($f, Array($numurs, $Partners));
fclose($f);
}
?>
</body>
</html>
You can use PHP's header() function for this. Change the PHP part to this
<?php
$numurs = $_POST['fname'];
$Partners = $_POST['fav_language'];
if ($numurs){
$f = fopen('numurs.csv', 'w');
fputcsv($f, Array($numurs, $Partners));
fclose($f);
header('Location: '.$_SERVER['REQUEST_URI']);
exit;
}
?>

send a php webpage to folder and dispay the content as html

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

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

HTML form submission, adding info to a list on the page

I am writing an intranet site in HTML, and what online users to be able to add in their own infomation (such as skype name) so that I dont have to go through and add it all myself for the 1000 people at the company, and also so that when new starters come in, they can add their own infomation themselves.
I currently have a 'Users' page with a link in it to another page where there is a form code in it looking like this:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<label>Please type in a message
<input type="text" name="msg" id="msg" />
</label>
<label>and your name
<input type="text" name="name" id="name" />
</label>
<p>
<label>Submit
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
</p>
</form>
<?php
$msg = $_POST["msg"];
$name = $_POST["name"];
$posts = file_get_contents("users.html");
$posts = "$msg - $name\n" . $posts;
file_put_contents("users.html", $posts);
echo $posts;
?>
I want the infomation users put into this form to be automatically put onto a list on the 'users.html' page, visible to everyone. I had hoped the above code would achieve this, but I cant get it to work.
Also, how would I specify whereabouts in the 'users.html' page the infomation is put? I want it to be amongst the:
<div id="content">
<div class="content_item">
..coding, so that it loads into a list inside the webpage.
Many Thanks in advance :)
Don't try to edit users.html with each submission. That way lies madness.
Store the submitted data in a database. Use the PDO library to do this.
Have the page that displays that data pull it from the database on demand.
Although the use of database would be better and files tend to grow rather large with time, am answering your actual question as per your posted code.
To show the values that were written from a form (seperate from below), you could set up a page with this inside it:
<div id="content">
<?php echo file_get_contents("users.html"); ?>
<div class="content_item">
And the seperate page form would be like the following (tested)
<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Page Title Here</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<label>Please type in a message
<input type="text" name="msg" id="msg" />
</label>
<label>and your name
<input type="text" name="name" id="name" />
</label>
<p>
<label>Submit
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
</p>
</form>
</body>
</html>
<?php
if(isset($_POST['submit']) && empty($_POST['msg']) && empty($_POST['name']) ) {
die("All fields need to be filled, please try again.");
}
if(isset($_POST['submit'])) {
$msg = $_POST["msg"];
$name = $_POST["name"];
$posts = $msg . "-" . $name;
$file = "users.html";
$fp = fopen($file, "a") or die("Couldn't open $file for writing!");
fwrite($fp, $posts . "\n<br>") or die("Couldn't write values to file!");
fclose($fp);
}
echo $posts;
echo "<hr>Previous messages<hr>";
$file = "users.html";
if (file_exists($file)) {
$fp = file_get_contents($file, "r") or die("Couldn't open $file for writing!");
echo $fp;
exit;
}
else {
echo "The file is empty";
}
?>

Can't save textarea to file using php?

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).

Categories