How to save php form data for use it later - php

I have a simple html form along with some php codes in a index.php file,
and my question is: how can I save that form data and use them in a diffrent page?
Php code looks like this:
if(isset($_POST['submit'])){
//collect form data
$name = $_POST['name'];
$email = $_POST['email'];
}
HTML code looks like this:
<form action='' method='post'>
<p><label>Name</label><br><input type='text' name='name' value=''></p>
<p><label>Email</label><br><input type='text' name='email' value=''></p>
<p><input type='submit' name='submit' value='Submit'></p>
</form>

Simplest solution is save this data in session but this have a limitation to browser close time and it's only assigned to current browser session:
<?php
session_start();
if(isset($_POST['submit'])){
//collect form data
$_SESSION['name'] = $_POST['name'];
$_SESSION['email'] = $_POST['email'];
}
echo $_SESSION['name'];
echo $_SESSION['email'];
?>
For better solution read this solutions and their limitations:
Session - http://php.net/manual/en/reserved.variables.session.php
File - http://php.net/manual/en/ref.filesystem.php
Database - https://www.w3schools.com/php/php_mysql_intro.asp

you can save in session like below code
<?php
session start();
$_SESSION['postdata'] = $_POST;
?>

I donĀ“t know if this is what you want, since you dont specify, but you could save it via cookies with javascript, and then capture it with php.
JAVASCRIPT
//Call this in the html
function Example(element){
var name = $(elemt).find('name').text();
document.cookie = escape('variable') + '=' + escape(name) + '' + '; path=/';
PHP
$name= $_COOKIE['variable']))

Related

Saving form information with sessions

I'm trying to use sessions to save form input, but it works only on the last form submitted.
I have an index.php that goes like this:
<?php
session_start();
$_SESSION['number-of-sessions'] = 0; ?>
...
then, there's a link to form.php:
...
<form action='display.php' method='post'>
<input type='text' name='name'/>
<input type='text' name='id'/>
<input type='submit' value="Submit"/>
...
And here's display.php:
<?php
session_start();
$_SESSION['number-of-sessions']++;
$_SESSION[$_SESSION['number-of-sessions']] = $_POST;
?>
...
<?php
for($i = 1; $i <= $_SESSION['number-of-sessions']; $i++) {
print_r($_SESSION[$i]));
}
?>
// another link that goes back to form.php
So basically, it always prints the last form submitted and the others are just blank space.
Is there any other way of doing this or am i doing it right?
p.s: i can't use databases.
Not surprisingly, you have a new $_SESSION for each session, so stocking any information that way won't work.
If you want to persist data across sessions, you have to persist it somewhere. Consider writing it to a file if you can't use databases, for example.

Storing value used in POST form in a PHP variable

I'm using a POST form that transfers data from one PHP file to another.. no issues there. However, I'm trying to store session data into a PHP variable.
<?php
if(!isset($_SESSION['login_user']))
{
session_start();
echo "<form action='../account.php' method='post'>";
echo "<p><b>Username: </b><input id='uname' type='username' name='uname' align='middle'></p>";
echo "<p><b>Password: </b><input id='pword' type='password' name='pword' align='middle'></p>";
echo "<p><input type='Submit' style='width:15%'></p>";
echo "</form>";
}
?>
I need the "uname" value to be stored as $_SESSION['login_user'] upon clicking submit, but this shouldn't prevent POSTing to the next page. How can I handle this?
session_start() should be initialized before anything you do in php
in your account.php initialize the session :
<?php
session_start();
$_SESSION['login_user'] = $_POST['uname'];
//rest of the stuff
?>
In account.php, do following assignment:
$_SESSION['login_user'] = $_POST['uname'];

I'm trying to use a textbox value as a session variable

I'm trying to use a textbox value as a session variable when the button is clicked, so far I have it working but it's hardcoded in, how do I get it to use the textbox value instead?
Here's the session code:
<?php
session_start();
$_SESSION['url'] = "url";
?>
Here's the textbox:
echo "<input type='text' id='starurl' value=''/>";
echo "<br><button onclick='save_a9({$row99['starID']})'>Approve</button><button onclick='save_d9({$row99['starID']})'>Disapprove</button><br>";
Here's the save_a9:
function save_a9(id) {
$.post('response6.php', {starID:id},
function(result) {
alert(result);
window.location.reload();
});
}
Is this what you mean? the value doesn't need a form, it just needs to go to another page to be used there
<?php
session_start();
if (isset($_POST['url']) {
$_SESSION['url'] = $_GET['url'];
}
?>
echo "<input type='text' id='starurl' value='" . htmlspecialchars($_SESSION['url'])"
When you are assigning the _SESSION url, you will need to use the posted variable to assign the string.
$_SESSION['url'] = $_GET['url'];
To do the opposite, and have the textbox show the value of the session, you would:
echo "<input type='text' id='starurl' value='" . htmlspecialchars($_SESSION['url']) . "'/>";
It is important to note that if you want the text box to actually do something, you will need to have the input box wrapped around a form tag.
The <form> tag tells the browser where the form starts and ends. You can add all kinds of HTML tags between the <form> and </form> tags. (thanks echoecho!)
If you are working RESTfully, GET should be used for requests where you are only getting data, and POST should be used for requests where you are making something happen.
Some examples:
GET the page showing a particular SO question
POST a comment
Click the "Add to cart" button and send a POST request.
(Thanks Skilldrick!)
The form & PHP file would look like this:
<?php
session_start();
if (isset($_POST['url']) {
$_SESSION['url'] = $_POST['url'];
}
echo '<form action="POST" method="?">';
echo "<input type='text' id='starurl' value='" . htmlspecialchars($_SESSION['url']) . "'/>";
echo '</form>';
You will notice when the form is updated, the session updates too. When you close your browser and open it again, you will see you old contents on the input box. (Press "enter" to save the input box without a submit button).
Simply use this one
//page1.php
<form action="page2.php" method="post">
<input type="text" name="session_value">
<input type="submit" name="set_session" value="Set Session">
</from>
//page2.php
<?php
if(isset($_POST['set_session'])){
session_start();
$_SESSION['url'] = $_POST['session_value'];
}
?>
For getting the value from your textbox, first modify your HTML as :
<input type='text' id='starurl' name='url' value=''/>
The textbox needs a name attribute which is used by the $_POST, $_GET or $_REQUEST superglobals.
Then depending upon your form submission method (GET or POST) :
$_SESSION['url'] = $_GET['url'] // Default method for form submission
$_SESSION['url'] = $_POST['url'] // For <form method="post">
$_SESSION['url'] = $_REQUEST['url'] // Works for both
EDIT :
Since you are using a button onclick event to set the session variable, you can use this (assuming you are using jQuery) :
Javascript:
$('button').click(function(){
var url = $('#starurl').val();
$('#sessionURLDiv').load('save_session_url.php?url='+url);
});
PHP: save_session_url.php :
<?php
session_start();
if ( isset ( $_GET['url'] ) ) {
$_SESSION['url'] = $_GET['url'];
}
?>
HTML :
Just add this div anywhere in the page.
<div id = "sessionURLDiv"></div>
I hope this helps.

php, form using the same page after submittion

I'm wondering what's the easiest way to make let's say a form with user/pass and submit button with php but after you submit it goes back to the same page instead of going to another php page.
I'm thinking of if/else statement but a bit confused how to set it after lots tries but still not getting the result wanted
weird I did all those you guys said before I posted..but...>.<"
let's say just something simple like this...
but I also want to set if nothing is entered then sumbit is clicked there'll be an error....should be something easy but I don't know why I can't seem to figure it out
<?php
function userPass()
{
echo "<form method='post' action=" . $_SERVER['PHP_SELF'] . ">";
echo "<input type='text' name='user' /><br/>";
echo "<input type='text' name='pass' /><br/>";
echo "<input type='submit' value='Login' />";
}
if(empty($_POST["user"]))
{
userPass();
}
if(!(empty($_POST["user"])))
{
if($_POST["user"] == "comp")
{
echo "Welcome comp";
}
else
{
echo "Wrong user";
}
}
?>
The other answers are right; you only need to send the user back around to your current page in the "action" property. You can test to see if they did so using "isset".
Something that I'm surprised hasn't been mentioned yet is that your input is not being sanitized, and you're setting yourself up for disaster. Huge injection vulnerability in your action attribute:
$_SERVER['PHP_SELF']
If you don't sanitize this, then someone can just modify the URL that they see and your poor PHP script won't know any better than to process that as your SELF constant.
In other words, you absolutely must use an htmlspecialchars() function to html-encode that parameter. With that, your action should look more like htmlspecialchars($_SERVER['PHP_SELF']).
if current page is index.php, use index.php in form tag as value of action.
like this:
<form action="index.php" method="post">
</form>
u can check for submitted form by putting:
if(isset($_POST)){
...
}
at top of page
Just use below syntax
<form method="post" action="">
You can check whether post is set using isset() method.
<?php function userPass() {
echo "<form method='post' action=" . $_SERVER['PHP_SELF'] . ">";
echo "<input type='text' name='user' /><br/>";
echo "<input type='text' name='pass' /><br/>";
echo "<input type='submit' value='Login' />"; }
if(empty($_POST["user"]))
{
userPass();
}
*if(!(empty($_POST["user"])))*
{
if($_POST["user"] == "comp")
{
echo "Welcome comp";
}
else
{
echo "Wrong user";
}
}
?>
This part of your code is wrong, you should type : if(!empty($_POST["user"]))
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
This is exactly how you work with your form to reload the page when click the submit button inside this form.
ADDITIONAL:
Add required to all input you wanted to be required or check if it's empty.
This is a code that I created to control learning the required input fields satisfy the requirements. when this is so, the data will be sent to the database. if it does not meet the requirements, there will be a message may be shown at the top of the page
<?php
if (!isset($_POST['submitform'])) {
}
else
{
$_SESSION['firstname'] = $_POST['firstname'];
$_SESSION['lastname'] = $_POST['lastname'];
$_SESSION['email'] = $_POST['email'];
$_SESSION['mobile'] = $_POST['mobile'];
$_SESSION['telephone'] = $_POST['telephone'];
$_SESSION['place'] = $_POST['place'];
$_SESSION['street'] = $_POST['street'];
$_SESSION['housenumber'] = $_POST['housenumber'];
$_SESSION['gender'] = $_POST['gender'];
if (empty($_POST['firstname']) or empty($_POST['lastname']) or empty($_POST['email']) or empty($_POST['mobile'])or empty($_POST['telephone']) or empty($_POST['place'])
or empty($_POST['street']) or empty($_POST['housenumber']) or !isset($_POST['gender']))
{
echo "Sending denied";
}
else
{
require 'database.php';
header('Location: succes.php');
} ?>
I hope this is helpful information for you
this code will help you
<form action="<?= $_SERVER['REQUEST_URI'] ?>" method="post">
or you could just do this:
<form action="" method="post">
Thank you ....
You can define in the form submission:
<form method=get action=index.php >
You can also leave action out altogether, and it will automatically post/get to that same file.
I think that all have missed the actual question, i.e. if there is a way to stay in the same page after submitting a form. The answer is NO. Once you submit a form -- whether you use the POST or GET method, and of course assuming that you are using $_SERVER['PHP_SELF'] or empty action, etc. -- a new page is opened automatically. This holds at least for PHP. (I have tried a lot of different ways using XAMPP.) I don't now about other scripting languages.

Edit form with hidden inputs

I have this simple form.
a.php
<html>
<head>
</head>
<body>
<?
echo "
<form action = 'a2.php' method = 'post'>
<input type = 'text' name = 'name'>
<input type = 'submit' value = 'SEND' name = 'send'>
</form>
";
?>
</body>
</html>
a2.php
<?
$name = $_REQUEST ['name'];
echo $name;
echo "
<form action = 'a2.php' method = 'post'>
<input type = 'submit' value = 'EDIT' name = 'edit'>
</form>
";
?>
How can I keep the value introduced when I click EDIT and i'm back to the first form?
Thanks.
EDIT 2: using hidden inputs
on a2.php just put another <input type="hidden" name="hidden_name" value="{$_POST['name']}" /> after you hit submit on a2.php (BTW for it to go back to a.php, you need to change the form action="a.php" on a2.php), a.php will have a $_POST['hidden_name'], that will contain the value from the first iteration.
EDIT: before you start handling $_SESSION variables, first initiate the session before any html output with a session_start() function.
Use a superglobal like $_SESSION so in your case you would need to fetch the incoming in a2.php $_SESSION['name'] = $_POST['name'] and refer to the $_SESSION['name'] in your a.php. Remember that $_SESSION['name'] will retain the last assigned value until the the session is terminated, I.e. browser window is closed.
You can read more in http://www.php.net/manual/en/reserved.variables.session.php
Also about session_start: http://www.php.net/manual/en/function.session-start.php
1.creating the hidden inputs in forms by using type="hidden"

Categories