New to sessions in PHP - php

I'm trying to get to grips with sessions as it's a part of PHP I'm not very good with. Could you help me in explaining what is happening here on the two pages that I have? It is giving an undefined index and I've no idea why.
Thanks
File 1
<strong>Test Form</strong>
<form action="test2.php" method"post">
<input type="text" name="picturenum"/>
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php
// starting the session
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['picturenum'] = $_POST['picturenum'];
}
?>
File 2
<?php
session_start();
echo $_SESSION['picturenum'];
?>

session_start() must go at the top of the page:
<?php
session_start();
// Opening <html>, etc goes below
?>
<strong>Test Form</strong>
<form action="test2.php" method"post">
<input type="text" name="picturenum"/>
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php
if (isset($_POST['Submit'])) {
$_SESSION['picturenum'] = $_POST['picturenum'];
}
?>

This works:
Form (teste1.php)
<?php
session_start();
// Opening <html>, etc goes below
?>
<strong>Test Form</strong>
<form action="test2.php" method"post">
<input type="text" name="picturenum"/> <!-- make sure you type something here -->
<input type="submit" name="Submit" value="Submit!" />
</form>
File 2 (test2.php)
<?php
if (isset($_POST['picturenum'])) {
$_SESSION['picturenum'] = $_POST['picturenum'];
echo $_SESSION['picturenum'];
}else{
echo "something wrong with the POST";
}
?>

As far as I can see, you're starting session after the form in the first file. The rule is: you should start the session before any echo or any HTML output, even before a space. So, basically, session_start() should be your first line after <?php.
Then, how do you get to the second page? If you close the browser and then re-open it, the session of course won't persist and you'll get your undefined index.
Please comment on this if you need any further explanations.

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

PHP sessions problems

I'm using sessions to save what ever the user types in the form and what ever they do type will be displayed on the other pages.
It was working perfectly fine but after all the server uploads and such my code has completely done one on me and i'm lost.
Can somebody see if they can spot a mistake? I need fresh eyes.
HTML.
<div id="form"><!--Form Start-->
<form action="home.php" method="post">
<p>Enter Name <input type="text" id="full_name" name="fullName" class="name_input"/>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
</div><!--Form end-->
PHP.
<?php
session_start(); // declaring the variable
if(isset($_POST['fullName'])){ //setting the variable
if(empty($_POST['fullName'])){ //if empty dont to nothing but my wep page will reload
}else{ //if they have do this
$_SESSION['fullName'] = $_POST['fullName']; //get the session for the name (From the from)
header("Location: home.php"); //then will direct the user to the home page (will also display name on each page)
}}
?>
Session on other pages
<div id="echo"> <!-- div ECHO start -->
<?php
echo $_SESSION['fullName']
?>
</div> <!--div ECHO end -->
$_SESSION['fullName'] = $_POST['fullName'];
session_register(fullName);
replace with this code try it
You'll need to add session_start() on whatever page you are redirecting to that is supposed to display the data.
Also, (I'm assuming you realize) what you posted doesn't have anything that would output the data, like:
<input type="text" name="fullName" value="<?php echo $_SESSION['fullName']; ?>"/>
You need to start session on other page as well and stop the script from setting that session. After header location you need to use exit here.
<?php session_start();?>
<div id="echo"> <!-- div ECHO start -->
<?php
echo $_SESSION['fullName'];
?>
you need use exit after header location :-
header('location: home.php');
exit;
Just change the div id form to other because it has a default and remove the empty function because you add isset functon.
Use this.
<div id="myform"><!--Form Start-->
<form action="home.php" method="post">
<p>Enter Name <input type="text" id="full_name" name="fullName" class="name_input"/>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
</div><!--Form end-->
PHP.
<?php
session_start();
if(isset($_POST['fullName']))
{
$_SESSION['fullName'] = $_POST['fullName']; //get the session for the name (From the from)
header("Location: home.php");
exit();
}
?>
Session on other pages.
<div id="echo"> <!-- div ECHO start -->
<?php
session_start();
print_r($_SESSION);
echo $_SESSION['fullName'];
?>
</div> <!--div ECHO end -->
May be it helpful to you.If any problem then let me know.
You are "posting" the values to home.php, doing that you can't set $_SESSION['fullName'] = $_POST['fullName'] in the origin.
Change
<form action="home.php" method="post">
to
<form action="name_of_the_first_script.php" method="post">
$_POST['fullName'] does not exist before the redirect.
Here is how everything should look like (lest call the page index.php):
<div id="form"><!--Form Start-->
<form action="index.php" method="post">
<p>Enter Name <input type="text" id="full_name" name="fullName" class="name_input"/>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
</div><!--Form end-->
now after you hit submit the index.php will be reactioned and at this time with the $_POST request meaning that that the condition
if(isset($_POST['fullName'])){
will be true and the PHP code can be executed, setting the $_SESSION variable and redirecting you to home.php where you ca now read the $_SESSION previously set in index.php
Hope this can me more clear now! :)

How to redirect with a $_SESSION variable from a form

I'm a bit inexperienced so go easy on me.
I need to save the value from a form textarea before the form is submitted (I need it even after the page is reloaded).
After the reload, I need to redirect to a predefined page on the site that includes the textarea value on the very end on the URL.
I have something like this so far:
<php?
session_start();
$_SESSION['textarea_value'] = $_POST['textarea_name'];
?>
// below is called directly after a popup form submission
location.reload();
if ($_SESSION['textarea_value'] != null) {
header("Location: http://www.xxxxxxxx.com/?s=$_SESSION['textarea_value']");
unset($_SESSION['textarea_value']);
}
Okay, so here's what I would do:
Method #1
index.php
<?php
if($_POST)
{
session_start();
$_SESSION["someVar"] = $_POST["someVar"];
header("Location:otherPage.php");
}
?>
<form action="" method="post">
<textarea name="someVar"></textarea><br/>
<input type="submit" value="submit">
</form>
otherPage.php
<?php
session_start();
$someVar = $_SESSION["someVar"];
?>
Method #2: If you want to do it with a get request, you don't even need to use sessions:
index.php
<?php
if($_POST)
{
header("Location:otherPage.php?someVar=".$_POST["someVar"]);
}
?>
<form action="" method="post">
<textarea name="someVar"></textarea><br/>
<input type="submit" value="submit">
</form>
otherPage.php
<?php
$someVar = $_GET["someVar"];
?>
Method #3: You could also even take out the redirection entirely:
index.php
<form action="otherPage.php" method="post">
<textarea name="someVar"></textarea><br/>
<input type="submit" value="submit">
</form>
otherPage.php
<?php
$someVar = $_POST["someVar"];
?>

Categories