How to keep values in re-opened form? - php

I have two PHP files: index.php with a form, and data.php for further data manipulation.
Here is index.php:
<?php
session_start();
require_once("../index.conf");
$language = new Language();
$lang = $language->getLanguage(#$_POST['lang']);
?>
...
<form name="myForm" action="data.php" method="post" onsubmit="return validateForm()">
<input type="text" name="title" placeholder="e.g.: my_title" value="<?php echo isset($_POST['title']) ? $_POST['title'] : '' ?>">
...
<button class="btn_r" name="submit" type="submit">
<?php echo $lang['submit-button']; ?>
</button>
Here is data.php:
// success message
echo sprintf('
<div class="success">Good job! Your file <em>'.$file.'</em> was successfully created with this HTML content:<br>
<form name="goto_preview" method="post">
<input type="hidden" name="img_title" value="'.$title.'">
<button class="btn_l" name="reset" type="submit" name="logout" formaction="preview.php">PREVIEW RESULTS</button>
<button class="btn_r" name="submit" type="submit" name="continue" formaction="index.php">CORRECT DATA</button>
</form>
</div>',$img_name);
I try to return the user to the form, with the original values filled in if correction is needed. But the form always opens empty. What is wrong with my code?

Nothing is wrong with your code. That's just the way php forms work, you're redirecting to a new page therefore the form isn't filled out. To change that you could pass the POST arguments that you receive in the data.php and pass them back to index.php where you set them as default values (if present)

Related

PHP Submit button doesn't have any effect (PhpStorm)

I updated the question.
Since the last code was pretty complex and even after fixing the stuff it didn't work, I executed the below simple code to check if things work. Even this code doesn't work. Whenever I click on the submit button, it again returns a 404 error.
Yes, I placed the PHP code in the body as well to check if this work but it doesn't.
<?php
if(isset($_POST['submit'])) {
echo("Done!!!!");
} else {
?>
<html>
<head>
<title>Echo results!</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input name="submit" type="submit" value="submit"/>
</form>
<?php
}
?>
</body>
</html>
Try giving the button_create as name of the submit button
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
if(isset($_POST['button_create'])) {
<td><input type="submit" name="button_create" id="button_create" value="Create Table!"></td>
change these lines see how you go from there
There are a couple of things wrong here, method should be POST instead of GET. The name attribute of text fields should be used when receiving the values. The submit button name should be used to check whether the button is clicked or not. See the example given below.
<?php
if (isset($_POST['submit'])) {
$ex1 = $_POST['ex1'];
$ex2 = $_POST['ex2'];
echo $ex1 . " " . $ex2;
}
?>
<form action="" method="post">
Ex1 value: <input name="ex1" type="text" />
Ex2 value: <input name="ex2" type="text" />
<input name="submit" type="submit" />
</form>
Echo results!
<?php
if(isset($_POST['submit'])) {
echo("Done!!!!");
} else {
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input name="submit" type="submit" value="submit"/>
</form>
<?php
}
?>
this is for your updated question

Conflict On Two Post on Single Page Form(HTML) and PHP

I need to keep two forms for login and Logout in one single page including PHP and HTML like
<html>
<body>
<form action="" method="post">
<input type="text" name="username">
<input type="submit" name="SubmitButton" value="Get in"/>
</form>
<form action="" method="post">
<input type="submit" name="logoutButton" value="Logout"/>
</form>
</body>
</html>
on PHP part I have
<?php
if(isset($_POST['SubmitButton'])){
$inputuser = $_POST['username'];
$_SESSION['user'] = 'A';
}
if(isset($_POST['logoutButton'])){
unset($_SESSION['user']);
header('Location: http://somewhere.com/');
}
As I said I have to keep everything on one single page but this looks like causing conflict between the POST(s) can you please let me know how to stop this and target each Post properly?

PHP session data from form not being passed and/or not printing to screen

I am playing around with using my Raspberry Pi 3 as a web server.
I would like to learn more about processing user input through forms.
I have two files in /var/www/html, viz. form.html and form.php:
form.html:
<form action="form.php" method="post">
<input type="text" name="varname"/>
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['varname'] = $_POST['varname'];
}
?>
form.php:
<?php
session_start();
$var_value = htmlspecialchars($_SESSION['varname']);
echo $var_value;
?>
When I click Submit! on form.html the browser takes me to form.php which displays a blank page.
Naturally, I would like it to print $var_value to the screen.
Is there problem in my code, or could it be some other server-side issue?
Change your form.html extension to form.php,
And you may use the below code to achieve your work.
Form.php // Single page
<?php
session_start(); // Should be in first Line
if (isset($_POST['Submit'])) {
$_SESSION['varname'] = $_POST['varname'];
$var_value = htmlspecialchars($_SESSION['varname']);
echo $var_value;
}
?>
<form method="post">
<input type="text" name="varname"/>
<input type="submit" name="Submit" value="Submit!" />
</form>
?>
Otherwise: // Multiple Page
form.php
<form action="some_form.php" method="post">
<input type="text" name="varname"/>
<input type="submit" name="Submit" value="Submit!" />
</form>
some_form.php
<?php
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['varname'] = $_POST['varname'];
$var_value = htmlspecialchars($_SESSION['varname']);
echo $var_value;
}
?>

save input data in php to notepad.txt

<form id="form" name="form" method="post" action="">
<div class="jquery-script-clear"></div>
<h1>BARCODE GENERATOR</h1>
<div id="generator"> Please fill in the code :
<input type="text" name="barcodeValue" id="barcodeValue" value="1234"><br> <br>
</div>
<div id="submit">
<input type="button" onclick="generateBarcode();" value=" Generate "> <input type="button" onclick="printDiv('print')" value="Print" />
</div>
</form>
<?php
$barcodeValue = $_POST["barcodeValue"];
$save = file_get_contents("save.txt");
$save = "$barcodeValue" . $save;
file_put_contents("save.txt", $save);
echo $save;
?>
sample picture
How to save the input data in save.txt file. When i clicked generate button the text file not showing in same folder.
The problem with your code is you have no submit button so your form was not actually posting when you pressed the button. if you look at my edits you can see I changed the button from input type="button" to type="submit". That allows for the form to submit back to the same php script.
Your script also was causing errors because you accessed $_POST["barcodeValue"] without checking if it existed. You also have to check if the save.txt exists before reading from it. If analyze my edits you can see how checking if the variables are available will help quite a bit.
<form id="form" name="form" method="post" action="">
<div class="jquery-script-clear"></div>
<h1>BARCODE GENERATOR</h1>
<div id="generator"> Please fill in the code :
<input type="text" name="barcodeValue" id="barcodeValue" value="1234"><br> <br>
</div>
<div id="submit">
<input type="submit" value=" Generate ">
</div>
</form>
<?php
if(isset($_POST["barcodeValue"]))
{
$barcodeValue = $_POST["barcodeValue"];
if(file_exists("save.txt"))
$save = file_get_contents("save.txt");
else
$save = "";
$save = $barcodeValue . $save;
file_put_contents("save.txt", $save);
echo $save;
}
?>
Let me know if you need more help

PHP: 2 Forms, 1 Being File Upload

I have a single profile page that I want to upload a photo on as a separate action. I have the first form submitting to the page successfully, it is when I submit the photo form that the page returns blank with an empty message.
HTML
<form method="post" action="profile.php" id="main">
<input name="txtFirstName" type="text" value="<?php echo $sFirstName; ?>">
<input type="submit" name="btnSubmit" value="Submit" />
</form>
<form method="post" action="profile.php" id="photo_upload" enctype="multipart/form-data">
<img src="<?php echo($sPath); ?>" height='100' width='100' id="imgProfile" />
<br>
<input type="file" name="fUpload" id="fUpload">
<br>
<input type="submit" name="btnUploadPhoto" value="Upload" class="cancel"/>
</form>
PHP
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($_POST['btnSubmit']) {
$sFirstName = $_POST['txtFirstName'];
}
else if ($_POST['btnUploadPhoto']) {
// DO MY MOVE LOGIC
}
}
When the "btnSubmit" is called the page loads and the textbox gets the name that was entered. When the "btnUploadPhoto" is called I get this on the screen and nothing else:
{error: '', msg: '' }
How do I get the page to reload with the original form?
As you are uploading a file, you will want to check if a file has been submitted in your PHP code. Here is the code to do this:
isset($_FILES['fUpload'])

Categories