Loading new page after php code has run? - php

I have a form, this form is on a page called question1.php, and I want it to load question2.php when the submit button is pressed.
<form action="question2.php" method="post">
<input type="radio" name="ans" value="cuboid">
<input type="radio" name="ans" value="cone">
<input type="radio" name="ans" value="cylinder">
<input type="radio" name="ans" value="sphere">
<input type="submit" value="submit" name="submit">
</form>
But I also have this php code
<?php
if(isset($_POST['submit'])) {
if(isset( $_POST['ans'])) {
$selected_answer = $_POST['ans'];
if($selected_answer == "cuboid") {
$_SESSION["cuboid"] = ((int)$_SESSION["cuboid"]) + 1;
}
}
}
?>
EDIT: I have made a simpler demo to try and explain myself better, i have got three pages.
page1.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<form action="page2.php">
<input type="submit" value="submit" name="submit">
</form>
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
?>
</body>
</html>
page2.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<form action="page3.php" method="post">
<input type="radio" name="ans" value="color">
<input type="submit" value="submit" name="submit">
</form>
<?php
// Echo session variables that were set on previous page
if(isset($_POST['submit'])) {
if(isset($_POST['ans'])) {
$selected_answer = $_POST['ans'];
if($selected_answer == "color") {
$_SESSION["favcolor"] = "red";
}
}
}
?>
</body>
</html>
And page3.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
echo "Favorite color is " . $_SESSION["favcolor"] . ".";
?>
</body>
</html>
So on the first page I declare the session variable "favcolor", then on the second page if the user selects the radio button I want to update the color to red, however it just won't chane for me, on the third page it is still printing green

You had issue on page2.php because you are submitting form on page3.php but you are handling submission request on page2.php. So i corrected below for you.
page1.php
<?php session_start(); ?>
<!DOCTYPE html>
<html>
<body>
<form action="page2.php" method="post">
<input type="submit" value="submit" name="submit">
</form>
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
?>
</body>
</html>
page2.php
<?php session_start(); ?>
<!DOCTYPE html>
<html>
<body>
<form action="page3.php" method="post">
<input type="radio" name="ans" value="color">
<input type="submit" value="submit" name="submit">
</form>
</body>
</html>
page3.php
<?php session_start(); ?>
<!DOCTYPE html>
<html>
<body>
<?php
// Echo session variables that were set on previous page
if (isset($_POST['submit'])) {
if (isset($_POST['ans'])) {
$selected_answer = $_POST['ans'];
if ($selected_answer == "color") {
$_SESSION["favcolor"] = "red";
}
}
}
echo "Favorite color is " . $_SESSION["favcolor"] . ".";
?>
</body>
</html>

I hope this should work.
$_SESSION["cuboid"] = isset($_SESSION["cuboid"]) ? $_SESSION["cuboid"] : 0;
$_SESSION["cuboid"] = ((int)$_SESSION["cuboid"]) + 1;

From what I can tell, you are attempting to redirect to another webpage.
Try sending HTTP headers using the header() function.
header('Location: http://example.com/page3.php');
exit();
If the HTTP header doesn't work at first, turn on the output buffer using ob_start().

You are checking for $_POST['ans'] in the wrong page.
Currently you are checking for 'ans' when user enters page2.php, which is before the user submits the form that contains that input element.
When you submit the form on page2.php, the action sends the request over to page3.php, so you actually want to move your if statement that sets the color to green to the top (below session start) of page3.php

Related

How to pass a Variable through a session

I know there are other posts about this, but I still can't seem to see where the code is incorrect.
I'm trying to pass a variable from one page to another via session: Below are the two pages; excuse some of the variable names as I was just plotting them in quickly.
main.php
<?php
session_start();
session_unset();
if(isset($_POST['submit']))
{
$_SESSION['itemId'] = $_POST['firstName'];
echo "name = " . $_SESSION['itemId'];
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="result.php" method="POST">
First name:
<input type="text" name="firstName" placeholder="First Name">
<br>
<input type="submit" name="submit">
</form>
</body>
</html>
result.php
<?php
session_start();
echo $_SESSION['itemId'];
session_destroy();
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>
Previously I didn't have the unset in there. However, it hasn't made a difference.
The value is getting stored in the session, its just not passing it through to the other page.
Main page should looks like this:
<?php
session_start();
//remove session_unset();
if(isset($_POST['submit']))
{
$_SESSION['itemId'] = $_POST['firstName'];
echo "name = " . $_SESSION['itemId'];
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="result.php" method="POST">
First name:
<input type="text" name="firstName" placeholder="First Name">
<br>
<input type="submit" name="submit">
</form>
remove session_unset(); line after start_session()
and why r u destroying session in result page.
if you want to destroy then
store session value in variable
$id = $_SESSION['itemId'];
echo $id;
this code will help u

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 Forms Verification After Submit

can anyone fix my code, i have two problem :
1/ if i use info.php directly i can see button submit (need to fix this by condition, so each time i use info.php without submit i must be redirected to another page or same page with link"click here befor submite" )
2/ when i refresh page i can always see bouton submit after clicking on link "click here befor submite" (i want destroy session, each time the page is reloaded i want see link not button submit) thank you very much
Index.php
<html>
<body>
<?php
session_start();
?>
<?php
if (isset($_GET['submit']) && ($_POST['submit'] == 'submit')) {
header('location: info.php');
}
?>
<?php if (isset($_SESSION['submit'])==1) {
?>
<form method="get" action="index-1.php">
<br>
<?php
}
else {
?>
<form method="get" action="info.php">
<?php
}
?>
<br>
<font color ="red">
<a target="_blank" href="http://www.google.com"><font color ="black">*</font> text</a></br><br></h2>
<hr /></font>
<h2><font color ="red">1.</font>Enter Your Personal Facebook profil URL : <input name="id" placeholder="https://www.facebook.com/YourName"/ size="45"/></h2><br>
</br>
<?php if (isset($_SESSION['submit'])==1) {
?>
<input type="submit" name="submit" value="==>submit<==" id="submit" />
<?php
}
else {
?>
<center><strong>Click Here Before Submit </center></strong>
<?php
}
?>
</form>
</body>
</html>
info.php
<?php
session_start();
$_SESSION['submit'] =1;
header('location: index.php');
?>

PHP Checkbox from db, unchecked after submit

I have this code. I need that all the checkbox (taken by database) that I choose remain checked even after submitting the page.How can I do that?
?>
<?php
function connetti(){
$conn=mysql_connect("localhost","user","pass");
mysql_select_db('colours');
return $conn;
}
?>
<html>
<head>
<title>Scelta colori</title>
<meta charset="utf-8">
</head>
<body>
<h1>Scelta colori</h1>
<h1>Benvenuto <?php echo $_SESSION['user']; ?></h1><br>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<?php
$conn=connetti();
$sql="SELECT tonalita FROM colori";
$risultato=mysql_query($sql);
while ($row=mysql_fetch_array($risultato)){
$valore=$row['tonalita'];
echo('<input type="checkbox" value="'.$valore.'" name="colori[]">'.$valore.'</input><br>');
}
mysql_free_result($risultato);
mysql_close($conn);
?>
<input type="submit" value="Invia">
<input type="reset" value="Annulla">
</form>
</body>
</html>
u can use, after submitting the form, the $_POST vars and check it with $valore
<?php
echo('<input type="checkbox" value="'.$valore.'" name="colori['.$valore.']"'.((isset($_POST["colori"][$valore])&&$_POST["colori"][$valore]==$valore)?' checked="checked"':"").'>'.$valore.'</input><br>');
?>
untestet but should work
edit: got the error on the name-attr and fixed issue on not settet index. testet on php5

PHP Form echoing Post variable not working

<?php
ob_start();
// First we execute our common code to connection to the database and start the session
define('MyConst', TRUE);
include('../database.class.php');
include('../table.class.php');
include('../user.class.php');
include('../loginattempts.class.php');
include('../timer.class.php');
include('../characters.class.php');
include('../weapontype.class.php');
include('../objects/weapons/weaponobject.class.php');
include('../objects/weapons/bowieknife.class.php');
include('../npc/enemy.class.php');
include('../npc/skinhead.class.php');
include('../npc.class.php');
include('../npctype.class.php');
include('../functions.php');
include('../loginf.php');
include('locationf.php');
$dbo = database::getInstance();
$dbo -> connect("***************", "********", "********", "***************", array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
secSessionStart();
// At the top of the page we check to see whether the user is logged in or not
if(empty($_SESSION['user']))
{
// If they are not, we redirect them to the login page.
header("Location: login.php");
// Remember that this die statement is absolutely critical. Without it,
// people can view your members-only content without logging in.
die("Redirecting to login.php");
}
$_SESSION['currentlocation'] = "combat.php";
?>
<?php
if($_POST['formSubmit'] == "Submit")
{
$varMovie = $_POST['formMovie'];
echo $varMovie;
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="index.php" method="post">
Which is your favorite movie?
<input type="text" name="formMovie" maxlength="50">
<input type="submit" name="formSubmit" value="Submit">
</form>
</body>
</html>
Ok...so its supposed to echo out some text. Instead it just reloads the form! I'm not sure what else to write and it won't allow me to post so i'm just going to repeat what i've wrote until i reach the limit.
Add an ELSE part in the HTML, that will either show the form OR the answer, but keeps the header etc intact.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
if($_POST['formSubmit'] == "Submit")
{
$varMovie = $_POST['formMovie'];
echo $varMovie;
}
else {
?>
<form action="index.php" method="post">
Which is your favorite movie?
<input type="text" name="formMovie" maxlength="50">
<input type="submit" name="formSubmit" value="Submit">
</form>
<?php } ?>
</body>
</html>
I would tend to use:
<?php
if (array_key_exists("formSubmit",$_POST) && !strcmp($_POST["formSubmit"],'Submit'))
{
$varMovie = $_POST['formMovie'];
echo "Movie=(${varMovie})<br>\n";
}
:
:
Also comment out all the includes etc. above this, check it's giving you the contents of formMovie then add the other stuff back in gradually until it fails (or not).
Cheers.

Categories