How to control the execution of PHP code [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
with this example I recap my problem:
The first time I execute this php page I want to echo "I have two buttons".When the user click buttons "next" or "next1" I want to echo "I already said I have two buttons" and not "I have to buttons" again.I proved also with hidden input text but it doesn't work. thk for help
<?php
if ($_SESSION['already_said'] == false ){
echo "I have two buttons". date('h:i:s')."<br>";
}
$_SESSION['already_said']=true;
if( isset($_GET["next"]) || isset($_GET["next1"])) {
echo "I already said I have two buttons". date('h:i:s')."<br>";
}
?>
<html>
<body>
<form name="form" method="GET" action="" >
<button type="submit" name="next">
<span class="label">Next</span>
</button>
<button type="submit" name="next1">
<span class="label">Next1</span>
</button>
</form>
</body>
</html>

Of course the important thing is to remember to put the session_start() at the top of any script that uses the session. In fact it is a good idea to put it in all your scripts even if a few dont use the session just to keep the session alive.
<?php
session_start();
$msg = "I have two buttons ". date('h:i:s')."<br>";
if (! isset($_SESSION['already_said']) ){
$_SESSION['already_said'] = true;
}
if( (isset($_GET["next"]) || isset($_GET["next1"]) ) && isset($_SESSION['already_said'])) {
$msg = "I already said " . $msg;
}
?>
<html>
<body>
<div> <?php echo $msg; ?></div>
<form name="form" method="GET" action="" >
<button type="submit" name="next">
<span class="label">Next</span>
</button>
<button type="submit" name="next1">
<span class="label">Next1</span>
</button>
</form>
</body>
</html>

Related

PHP POST HTML From [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 days ago.
Improve this question
I am creating a small game app with a small "login" system . The user on index.php needs to input a username in order to proceed .
<form action="game.php" method="POST" class="box">
<h5 class="text-center">Provide a info</h5>
Enter username :
<input type="text" name="username" class="form-control form-control-sm text-secondary m-0">
<input type="submit" value="ENTER" class="btn btn-md btn-danger text-light mt-1">
</form>
After user has inputed his username on other page game.php I check are values entered if not die() message will appear . Piece of code for that is below .
if(isset($_POST['username'])){
// get username value from post
$username = $_POST['username'];
// checker() function checks do username already exists in DB
$found = checker();
var_dump($username);
if(!$found) {
// call a function to create a user
// and generate a unique token
$token = randomString();
createUser();
}
} else die("need to fill a input field");
Below This I have system for upgrade .
For example I have woodStock and woodLevel variables.
In game.php in HTML code below I created a from with action on PHP_SELF and method=POST .
Code below :
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" action="POST">
// submit button with name="wood" specified for a wood Upgrade
<input type="submit" value="Upgrade" class="btn btn-sm btn-light text-dark" name="wood">
</form>
In PHP script I had specified a if statement with isset function to determine is Button clicked or not .
Code :
if(isset($_POST['wood'])) {
var_dump($username);
// calls a upgradeWood() function and arguments are passed
upgradeWood($woodLevel,$woodStock);
}
The problem in all this case is that whenever I click a Upgrade button script throws a error from
die() function .
output : need to fill a input field
I had already tried everything . Even setting up a session but it is not working .
Have no idea what to do .
Please help me .
Tryed with session_start() and $SESSION superglobal . But it is still confusing to me .
I am still very new to PHP and Backend.

How to add multiple passwords to this PHP code? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I don't know PHP and have downloaded this code that works perfectly fine.
I add this code on top of any *.php file and it makes that page, password protected that only opens up if you type the password which in this case is 1234.
Now, I want to add 3 passwords instead of 1. I just don't know where to edit this code.
<?php
session_start();
if(isset($_POST['submit_pass']) && $_POST['pass'])
{
$pass=$_POST['pass'];
if($pass=="1234")
{
$_SESSION['password']=$pass;
}
else
{
$error="Incorrect Pssword";
}
}
if(isset($_POST['page_logout']))
{
unset($_SESSION['password']);
}
?>
<?php
if($_SESSION['password']=="1234")
{
?>
<form method="post" action="" id="logout_form">
<input type="submit" name="page_logout" value="Logout">
</form>
<?php
}
else
{
?>
<form method="post" action="">
<div>Protected Content</div>
<br />
<input type="password" name="pass" placeholder="Type your password">
<input type="submit" name="submit_pass" value="Login">
<br /><br />
<div><?php echo $error;?></div>
</form>
<?php
}
?>
Where should I, add what, to be able to have 3 possible passwords?
For this, you may edit password checking line like this:
<?php
session_start();
if(isset($_POST['submit_pass']) && $_POST['pass'])
{
$pass=$_POST['pass'];
$available_passwords = ['12345','pass123','myOtherPassword'];
if(in_array($pass,$ava_passwords))
{
$_SESSION['password']=$pass;
}
else
{
$error="Incorrect Pssword";
}
}
if(isset($_POST['page_logout']))
{
unset($_SESSION['password']);
}
?>

PHP session not showing a value [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
<?php
session_start();
?>
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="pageContainer">
<form action="second.php" class="formLayout" method="post">
<div class="formGroup">
<label>Name:</label>
<input type="text" name="first_name"><br>
<input type="hidden" name="postback" value="true"><br>
</div>
<div class="formGroup">
<label> Car model:</label>
<div class="formElements">
<input type="radio" name="model" value="Mustang">Ford Mustang<br>
<input type="radio" name="model" value="Subaru">Subaru WRX STI<br>
<input type="radio" name="model" value="Corvette">Corvette<br>
</div>
<input type="submit" name="submit">
</form>
<?php
if (isset($_POST['submit'])) {
$_SESSION["first_name"] = $_POST["first_name"];
$_SESSION["model"] = $_POST["model"];
}
?>
</div>
</body>
</html>
I set up my code to set the value of the "first_name" and "model" names into my session variables.
When I try to access the values of the stored variables in the submission page of the form:
<?php
session_start();
?>
<html>
<body>
<?php
echo $_SESSION["first_name"];
echo $_SESSION["model"];
?>
</body>
</html>
I only receive the out of the model value, not the first_name value. I don't understand what I'm doing wrong here.
Let's assume your first file is called first.php. As shown in your <form>, the second one is second.php.
It seems that you are writing the data into the session in the first.php file, but this code will never run because you are submitting your form to second.php and not first.php!
So, move the code that writes the session variables into second.php instead. To test that it really worked, you can create a third.php to display them.
(I'm not sure why see the model set, but I guess it's still there from a previous test you did.)

How to send data from button click to php in the same file without using ajax [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
<html>
<body>
<?php
echo "Hello Wolrd!";
if(isset($_GET['submit'])) {
echo "hello";
}
else {
}
?>
<h2> Sending data onclick </h2>
<button name = "submit" id="submit" onclick="<?php echo $_SERVER['PHP_SELF']; ?>" > order </button>
</body>
</html>
This is the code how I am trying to work. On clicking the button the php script should be executed but nothing is happening.
You need to create a form with action blank or same page name and use GET or POST or REQUEST in form method. This will help you to send the data on the same page and you can use some PHP code to display the data in whatever format you want.
Try this:
<html>
<body>
<?php
echo "Hello World!"; // will print when we run the program
if(isset($_GET['submit']))
{
echo "Hello World"; // will print this after form submission.
}
?>
<h2> Sending data onclick </h2>
<form action="" method="GET">
<input type="submit" value="Order" name="submit">
</form>
</body>
</html>
<form action="" method="post">
<input type="text" name="inputField">
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit'])) {
echo "$_POST[inputField]";
}
else {
// Code here
}
?>
PHP is a server side language and the button on click event is on the client side. In your code the php code given inside the on click gets executed at the server side when the user requests the page and the output will be placed there.
The best way for doing what you need. Like submitting data to the same PHP page when the user clicks the button is to create a in html.
<?php
if(isset($_POST['submit']))
{
echo "Data";
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="submit" name="submit" value="SomeValue" >
</form>
Or the other way is use PHP Session variables
<?php
// BEST WAY TO PASS DATA BETWEEN PHP PAGES
session_start();
if(isset($_SESSION['YOUR_SESSION']))
{
echo "Session Data";
}
?>

$_POST Giving Empty values [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have an HTML Form and I am trying to get the values from the form in my php file.
<div id="qForm">
<form action="postComment.php" method="post" name="comment">
Name: <input type="text" name="askerName"><br>
Title: <input type="text" name="qTitle"><br>
<textarea maxlength="255" name="theQ"></textarea>
<input type="submit">
</form>
</div>
My PHP file looks like this but It echos as Welcome
<?php
$name = $_POST["askerName"];
echo $name; //this works
$textArea = $_POST["theQ"];
echo $textArea; //this does not work
?>
<html>
<head>
</head>
<body>
welcome <?php $_POST["askerName"]; ?>
</body>
</html>
Use
<body>
welcome <?php echo $_POST["askerName"]; ?>
</body>
instead.
you have to use print/echo to display your php output these two basic ways to get output: echo and print
<body>
welcome <?php print $_POST["askerName"]; ?>
</body>
else
<body>
welcome <?php echo $_POST["askerName"]; ?>
</body>
echo is marginally faster compared to print as echo does not return any value

Categories