HTML Form with a PHP post to .txt - php

I know this is repetitive but i do not know PHP at all.. And trying to learn in a time crunch is not working how can i post to a text file? This is what i tried and i get an internal server error...
<div id="signupform" class="sb-search clearfix">
<form method="post" id="contact" class="clearfix" action="/comingsoon/php/formfix.php" name="email">
<input class="sb-search-input" placeholder="Enter email ..." type="text" value="" name="email">
<input class="sb-search-submit" value="" type="submit" name="email">
<button class="formbutton" type="submit"><span class="fa fa-envelope-o"></span></button>
</form>
</div>
this is the PHP formfix.php...
<?php
if(isset($_POST['submit']))
{
$email = $_POST['email'];
$file = fopen("/comingsoon/json.txt",);
fwrite($file,$email);
fclose($file);
print_r(error_get_last());
}
?>
What am i doing wrong...

Here is the solution to fix your PHP post to TXT
HTML
<div id="signupform" class="sb-search clearfix">
<form method="post" id="contact" class="clearfix" action="comingsoon/php/formfix.php"> <!-- I remove name="email"-->
<input class="sb-search-input" placeholder="Enter email ..." type="text" value="" name="email">
<input class="sb-search-submit" value="" type="submit" name="email1">
<button class="formbutton" type="submit"><span class="fa fa-envelope-o"></span></button>
</form>
</div>
In your formfix.php should be like this.
<?php
if(isset($_POST['email']) && isset($_POST['email1'])) {
$data = $_POST['email'] . '-' . $_POST['email1'] . "\n";
$ret = file_put_contents('json.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo "$ret bytes written to file";
}
}
else {
die('no post data to process');
}
?>
And you will get this result
I already do a test and it's work
Take a note please be aware on file location. And json.txt path must be at formfix.php .
Regards :)

Maybe my English is not good, but I will try to explain is to you as my best.
You should alter your HTML like this:
<div id="signupform" class="sb-search clearfix">
<form method="post" id="contact" class="clearfix" action="/comingsoon/php/formfix.php">
<input class="sb-search-input" placeholder="Enter email ..." type="text" name="email">
<input class="sb-search-submit" type="submit" >
</form>
A name of this "submit" isn't necessary。And so is the form。
Then,your PHP file should look like this:
<?php
if(isset($_POST['email']))
{
$email = $_POST['email'];
$file = fopen("/comingsoon/json.txt",);
fwrite($file,$email);
fclose($file);
print_r(error_get_last());
}
?>
Because the data that post to your server is only "email".
Good Luck to you!

Related

PHP How to use variable from one if statement in another?

I have a little problem.
I'm making form that submits simple article to database and then displays it on admin.php page.
Everything works fine, except image.
First i upload it to my server and then I try to add string(path of this image) to database.
However I can't use variable $filepath in second if statement.
I can echo it after image upload but can't use in other if.
Could u help me?
Thanks in advance.
Here's code:
<?php session_start();
if (!isset($_SESSION['logged-in']))
{
header('Location: index.php');
exit();
}
?>
<?php
include('db_connect.php');
if(isset($_POST['btn_upload'])) {
$filetmp = $_FILES["file_img"]["tmp_name"];
$filename = $_FILES["file_img"]["name"];
$filetype = $_FILES["file_img"]["type"];
$filepath = "photo/".$filename;
move_uploaded_file($filetmp,$filepath);
$result = mysqli_query($mysqli, "INSERT INTO upload_img (img_name,img_path,img_type) VALUES ('$filename','$filepath','$filetype')");
echo '<img src="' . $filepath . '" alt="">';
echo $filepath;
}
if ( isset($_POST['add']) ) {
$title = strip_tags($_POST['title']);
$content = strip_tags($_POST['content']);
$image = strip_tags($filepath);
$statement = $mysqli->prepare("INSERT bikes (title,image,content) VALUES (?,?,?)");
$statement->bind_param("sss",$title,$image,$content);
$statement->execute();
$statement->close();
header('Location: admin.php');
}
?>
<form method="post" action="" class="ui form">
<div class="required field">
<label>Title</label>
<input type="text" name="title" id="title">
</div>
<div class="required field">
<label>Content</label>
<textarea name="content" id="content" cols="30" rows="10"></textarea>
</div>
<div class="required field">
<label>Image</label>
<input type="text" name="image" id="image">
</div>
<input type="submit" class="ui primary button" id="add" name="add" value="Add article"></input>
</form>
<form action="addbike.php" method="post" enctype="multipart/form-data">
<input type="file" name="file_img" />
<input type="submit" name="btn_upload" value="Upload">
</form>
Since both of your forms work independently(with co-relation with each other whatsoever), $filepath variable won't be accessible in the second if block. However, what you can do is, merge both of your forms together so that all of your form's textual data and image would be accessible in a single submit button.
HTML:
<form method="post" action="" class="ui form" enctype="multipart/form-data">
<div class="required field">
<label>Title</label>
<input type="text" name="title" id="title">
</div>
<div class="required field">
<label>Content</label>
<textarea name="content" id="content" cols="30" rows="10"></textarea>
</div>
<div class="required field">
<label>Image</label>
<input type="text" name="image" id="image">
</div>
<input type="file" name="file_img" />
<input type="submit" class="ui primary button" id="add" name="add" value="Add article"></input>
</form>
PHP:
if (isset($_POST['add']) && is_uploaded_file($_FILES["file_img"]["tmp_name"])) {
$filetmp = $_FILES["file_img"]["tmp_name"];
$filename = $_FILES["file_img"]["name"];
$filetype = $_FILES["file_img"]["type"];
$filepath = "photo/".$filename;
if(move_uploaded_file($filetmp, $filepath)){
$result = mysqli_query($mysqli, "INSERT INTO upload_img (img_name,img_path,img_type) VALUES ('$filename','$filepath','$filetype')");
if($result){
$title = strip_tags($_POST['title']);
$content = strip_tags($_POST['content']);
$image = strip_tags($filepath);
$statement = $mysqli->prepare("INSERT bikes (title,image,content) VALUES (?,?,?)");
$statement->bind_param("sss",$title,$image,$content);
$statement->execute();
$statement->close();
header('Location: admin.php');
exit();
}else{
// error handling
}
}else{
// error handling
}
}
btn_upload is mutually exclusive to add, so the if statement which executes for btn_upload doesn't also run for add.
If you want it to be a two-phase process (upload image, then add) then you have to set a hidden input whose value is the filepath.
However, ideally the user would fill in name and provide an image and then submit them together. In order to do that, place all inputs inside the same form element and remote the "Add" button.

if(isset($_POST['submit'])) function responds to button but not to input type=submit

Well, as the title already says, I'm not getting it. Probebly doing something very stupid.
This is the form that I'm using. Its not that special. For testing purpose it now has a <button action=submit...> and a <input type=submit...> line.
<div id="cd-login">
<form method="POST" action="index.php" class="cd-form">
<p class="fieldset">
<label class="image-replace cd-email" for="signin-email">E-mail</label>
<input name="login" class="full-width has-padding has-border" type="text" placeholder="Username or E-Mail">
<span class="cd-error-message">Error message here!</span>
</p>
<p class="fieldset">
<label class="image-replace cd-password" for="signin-password">Password</label>
<input name="password" class="full-width has-padding has-border" type="password" placeholder="Password">
<span class="cd-error-message">Error message here!</span>
</p>
<p class="fieldset">
<input type="checkbox" name="remember_me" id="remember-me"/>
<label for="remember_me" onclick="document.getElementById('remember_me').click();">Remember Me</label>
</p>
<p class="fieldset">
<input class="full-width" type="submit" name="submit" value="Login" />
<br />
<button name="submit">Log In</button>
</p>
</form>
It then goes to the PHP code which is alligned above the HTML code:
<?php
require "config.php";
if(isset($_POST['submit'])){
$identification = $_POST['login'];
$password = $_POST['password'];
if($identification == "" || $password == ""){
$msg = array("Error", "Username / Password Wrong !");
}else{
$login = \Fr\LS::login($identification, $password, isset($_POST['remember_me']));
if($login === false){
$msg = array("Error", "Username / Password Wrong !");
}else if(is_array($login) && $login['status'] == "blocked"){
$msg = array("Error", "Too many login attempts. You can attempt login after ". $login['minutes'] ." minutes (". $login['seconds'] ." seconds)");
}
}
}
?>
When I smack my finger on the button it works, but I want to get it to work with an input line.
EDIT: Doesn't work in IE, Mozilla or Chrome. The <input type=submit ...> is clickeble. But smashing the enter-key doesn't work either.
replace
<button name="submit">Log In</button>
by :
<input name="submit" type="submit" value="Log In"/>
and eliminate the other input over :)
When I gave each button/input a unique name, and then checked for that name in POST in your index.php page, I was able to detect which button was pressed.
In principle, it's the name of the item on the form that helps us find it in the POST variables. When troubleshooting forms like this, you can always var dump the POST array and see what the computer is receiving on the processing page.
As an example, a slightly modified copy of your code is presented below. I ran your form in the following html.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<div id="cd-login">
<form method="POST" action="index.php" class="cd-form">
<p class="fieldset">
<label class="image-replace cd-email" for="signin-email">E-mail</label>
<input name="login" class="full-width has-padding has-border" type="text" placeholder="Username or E-Mail">
<span class="cd-error-message">Error message here!</span>
</p>
<p class="fieldset">
<label class="image-replace cd-password" for="signin-password">Password</label>
<input name="password" class="full-width has-padding has-border" type="password" placeholder="Password">
<span class="cd-error-message">Error message here!</span>
</p>
<p class="fieldset">
<input type="checkbox" name="remember_me" id="remember-me"/>
<label for="remember_me" onclick="document.getElementById('remember_me').click();">Remember Me</label>
</p>
<p class="fieldset">
<input class="full-width" type="submit" name="submit1" value="Login" />
<br />
<button name="submit2">Log In</button>
</p>
</form>
</body>
</html>
Then, I modified the processing page to catch that name in POST with the isset and provide some printing that will show the change is working. I commented out that config.php because I did not have it.
<?php
//require "config.php";
if(isset($_POST['submit1'])){
print ("<p>Hello from Submit1</p>");
var_dump($_POST);
$identification = $_POST['login'];
$password = $_POST['password'];
if($identification == "" || $password == ""){
$msg = array("Error", "Username / Password Wrong !");
}else{
$login = \Fr\LS::login($identification, $password, isset($_POST['remember_me']));
if($login === false){
$msg = array("Error", "Username / Password Wrong !");
}else if(is_array($login) && $login['status'] == "blocked"){
$msg = array("Error", "Too many login attempts. You can attempt login after ". $login['minutes'] ." minutes (". $login['seconds'] ." seconds)");
}
}
} else {
print ("<p>Submit1 was not recognized.</p>");
var_dump($_POST);
}
?>
When I ran that code, I could show the difference in the two buttons.

PHP two forms, one page

So in school we're learning about OOP in PHP and for our assignment we need to use 2 forms. this is the first time I'm using 2 forms in one page and I've been trying to figure out how to check which form is being submitted and create the according object.
Apparently, afer looking at some other questions, just using if (!empty($_POST['SubmitButtonName'])) should work, but it doesnt.
Hope someone can help me and tell me what I'm doing wrong :)
PHP:
if (!empty($_POST['sportwgn']))
{
try
{
$sport->Merk = $_POST['merk'];
$sport->AantalPassagiers = $_POST['AantalPassagiers'];
$sport->AantalDeuren = $_POST['AantalDeuren'];
$sport->Stereo = isset($_POST['stereo']) ? true : false;
$sport->Save();
$succes= "Uw sportwagen is gereserveerd!";
}
catch( Exception $e)
{
$error = $e->getMessage();
}
}
if (!empty($_POST['vrachtwgn']))
{
try
{
$vracht->Merk = $_POST['merk'];
$vracht->AantalPassagiers = $_POST['AantalPassagiers'];
$vracht->AantalDeuren = $_POST['AantalDeuren'];
$vracht->MaxLast = $_POST['MaxLast'];
$vracht->Save();
$succes= "Uw vrachtwagen is gereserveerd!";
}
catch( Exception $e)
{
$error = $e->getMessage();
}
}
Forms:
<form action="" method="post">
<label for="merk">merk</label>
<input type="text" id="merk" name="merk">
<br>
<label for="AantalPassagiers">Aantal passagiers</label>
<input type="number" min="2" max="4" id="AantalPassagiers" name="AantalPassagiers">
<br>
<label for="AantalDeuren">Aantal deuren</label>
<input type="number" min="1" max="5" id="AantalDeuren" name="AantalDeuren">
<br>
<label for="stereo">Stereo?</label>
<input type="checkbox" name="stereo" id="stereo" value="stereo"><br>
<br></div><div class="box">
<button type="submit" name="sportwgn">Reserveer</button></div>
</form>
</div>
</div>
<div id="container">
<h1 class="box">Reserveer een Vrachtwagen!</h1>
<div id="content">
<form action="" method="post">
<label for="merk">merk</label>
<input type="text" id="merk" name="merk">
<br>
<label for="AantalPassagiers">Aantal passagiers</label>
<input type="number" min="2" max="4" id="AantalPassagiers" name="AantalPassagiers">
<br>
<label for="AantalDeuren">Aantal deuren</label>
<input type="number" min="1" max="5" id="AantalDeuren" name="AantalDeuren">
<br>
<label for="MaxLast">Max last</label>
<input type="number" min="1" max="5" id="MaxLast" name="MaxLast"><br>
<br></div><div class="box">
<button type="submit" name="vrachtwgn">Reserveer</button></div>
</form>
Since your forms post on the same page (...action=""...) divide your code in php side to two actions by the submit button.
for the forms with
<button type="submit" name="sportwgn">Reserveer</button></div>
use
if(isset($_POST['sportwgn'])) {
// your code
}
and for
<button type="submit" name="vrachtwgn">Reserveer</button></div>
use
if(isset($_POST['vrachtwgn'])) {
// your code
}
You can use if(isset($_POST['buttonName'])) to check if it's in the post values.
Use action attribute to submit forms to different destinations.
<form action="firstForm.php" method="post">
...
</form>
<form action="secondForm.php" method="post">
...
</form>
And create 2 files for handling form posting.

POST not working with no signs of error

haven't programmed PHP in a while but I
have to assemble something for a client really fast.
I've set up 2 forms with POST but when I go to the next file it's just blank space, for some reason POST isn't being registered but is set cause I'm not getting an error echo.
Hese's the forms:
<form action="Funkcije.php" method="post" name="AddFromDB">
<input type="text" placeholder="Šifra Art" name="ArtNo">
<input type="submit" value="Dodaj">
</form>
<br>
<div id="newItem">
<form action="Funkcije.php" method="post" name="AddNew">
<input type="text" placeholder="Šifra" name="Art">
<input type="text" placeholder="Ime Proizvoda" name="ImeProizvoda">
<input type="text" placeholder="Dobavljač" name="Dobava">
<input type="text" placeholder="Cijena" name="Cijena">
<input type="submit" value="Dodaj">
</form>
</div>
And here's the 2nd file:
if(isset($_POST["AddFromDB"], $_POST["ArtNo"])){
addExisting ($_POST["ArtNo"]);
}
else if(isset($_POST["AddNew"], $_POST["Art"], $_POST["ImeProizvoda"], $_POST["Dobava"], $_POST["Cijena"])){
newItem ($_POST["Art"] && $_POST["ImeProizvoda"] && $_POST["Dobava"] && $_POST["Cijena"]);
}
else if (!isset ($_POST)){
echo "error";
}
So, by code I should be getting an error if POST is not set but I get nothing. Just a blank space.
here, you must be give a name to the submit button to check which form is POST like this...
<form method="post" name="AddFromDB">
<input type="text" placeholder="Šifra Art" name="ArtNo">
<input type="submit" value="Dodaj" name="form1">
</form>
<br>
<div id="newItem">
<form method="post" name="AddNew">
<input type="text" placeholder="Šifra" name="Art">
<input type="text" placeholder="Ime Proizvoda" name="ImeProizvoda">
<input type="text" placeholder="Dobavljač" name="Dobava">
<input type="text" placeholder="Cijena" name="Cijena">
<input type="submit" value="Dodaj" name="form2">
</form>
</div>
<?php
if(isset($_POST["form1"], $_POST["ArtNo"])){
echo "1";
}
else if(isset($_POST["form2"], $_POST["Art"], $_POST["ImeProizvoda"], $_POST["Dobava"], $_POST["Cijena"])){
echo "2";
}
else{
echo "error";
}
?>
now, this work fine..
thank you.. enjoy coding...

Form Submit to change/modify session variables in PHP

At the top I have a session start and the session ID can be passed across all the webpages because the ID is the same on all, this also includes a session variable called 'username' with the value of Guest.
I want to change this variable to the one entered in the form.
This is my first post so sorry for any mistakes.
<form class="form1" method="post" action="./" id="form1">
<fieldset>
<ul>
<p>Please enter your username to continue to the webshop.</p>
<label for="name">User Name:</label><span><input type="text" name="username" placeholder="User Name" class="required" role="input" aria-required="true"/></span>
<input class="submit .transparentButton" value="Next" type="submit" name="Submit"/>
</ul>
<br/>
</fieldset>
</form>
<?
if (isset($_POST['Submit'])) {
$_SESSION['username'] = $_POST['username'];
}
?>
Thanks
You need to take out ./ is in the action="" quotes like below (this is because you are using the same file to process the form)... and always start your php opening with <?php
<form class="form1" method="post" action="" id="form1">
<fieldset>
<ul>
<p>Please enter your username to continue to the webshop.</p>
<label for="name">User Name:</label><span><input type="text" name="username" placeholder="User Name" class="required" role="input" aria-required="true"/></span>
<input class="submit .transparentButton" value="Next" type="submit" name="Submit"/>
</ul>
<br/>
</fieldset>
</form>
<?php
if (isset($_POST['Submit'])) {
$_SESSION['username'] = $_POST['username'];
}
?>
And if you want to test it try something like this:
<?php
if (isset($_POST['Submit'])) {
$_SESSION['username'] = $_POST['username'];
// Use the following code to print out the variables.
echo 'Session: '.$_SESSION['username'];
echo '<br>';
echo 'POST: '.$_POST['username'];
}
?>
Tested the code after making the changes, and it works fine... look:
Update answer based of requests in the comments
<form class="form1" method="post" action="./" id="form1">
<fieldset>
<ul>
<p>Please enter your username to continue to the webshop.</p>
<label for="name">User Name:</label><span><input type="text" name="username" placeholder="User Name" class="required" role="input" aria-required="true"/></span>
<input class="submit .transparentButton" value="Next" type="submit" name="Submit"/>
</ul>
<br/>
</fieldset>
</form>
PUT THIS IN YOUR index.php
<?php
if (isset($_POST['Submit'])) {
$_SESSION['username'] = $_POST['username'];
// Use the following code to print out the variables.
echo 'Session: '.$_SESSION['username'];
echo '<br>';
echo 'POST: '.$_POST['username'];
}
?>

Categories