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.
Related
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";
}
?>
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>
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
So i basically have a form in html that submits to a preview page, where I access the variables to fill out the information on the page and display a preview of what the post will look like. I now want the user to be able to click a submit button and pass the same info to a new .php page that will handle submitting to database. I'm sure this is probably really easy, forgive me newbishness.
I have a form that submits to something like this and want is so when they click the "all good" button it sends the POST data on to a new .php page that can submit to the database and display a thank you message:
<?php
if (isset($_POST["preview"])) {
//get data
$firstvar = mysqli_real_escape_string($link, $_POST['name']);
$nextvar = mysqli_real_escape_string($link, $_POST['name']);
//etc
?>
<html>
//use vars in here
<p><a onclick="goBack()" class="btn btn-block btn-warning">Dont like what you see? Go back and fix it.</a></p>
<p>All good!</p>
</html>
JiteshNK provided one way of doing it using sessions.
Me, personally, I don't like to waste session variables on trivial things.
So another way you can do this is when you submit to your preview page (let's call preview.php), store the values in hidden input fields.
preview.php
<?php
if (isset($_POST["preview"])) {
// get data
$var1 = mysqli_real_escape_string($link, $_POST['var1']);
$var2 = mysqli_real_escape_string($link, $_POST['var2']);
// etc
?>
<!-- show vars -->
<form action="insert-page.php">
<!-- store vars into hidden input fields -->
<input type="hidden" name="var1" value="<?= $var1 ?>">
<input type="hidden" name="var2" value="<?= $var2 ?>">
<!-- etc -->
<p><a onclick="goBack()" class="btn btn-block btn-warning">Dont like what you see? Go back and fix it.</a></p>
<p><input type="submit" name="good" class="btn btn-block btn-success">All good!</a></p><!-- use submit button instead -->
</form>
Here is the below code.
<?php
include('connect.php');//connect.php is the connection file.
if(isset($_POST['submit']))//Here submit is the name of the form in HTML page.
{
$user_id=$_POST['user_id'];
$username=$_POST['username'];
$email=$_POST['email'];
$query = mysqli_query($con, "INSERT INTO sample (user_id, username, email)VALUES ('$user_id', '$username', '$email')");
header('Location: activation.php');
}
else
{
echo "<b>Error</b>";
}
mysqli_close($con);
?>
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 create session in header.php but when I change my page session get destroyed automatically .
session start this code i have place at very top of header.php
session_start();
php code for session
<?php $_SESSION['lang'] = $_GET['herbew'];
if(isset($_SESSION['lang'])){
echo $_SESSION['lang']; } ?>
HTML where I give option to user to change language
<form action="<?php echo $_SERVER['REQUEST_URI']?>" method="get">
<div class="lang">
<span class="english"><input type="hidden" name="english" value="english">
<input type="submit" name="submitsd" value="eng" ></span>
<span class="herbew"><input type="hidden" name="herbew" value="herbew">
<input type="submit" name="submit" value="her" ></span>
</div>
</form>
A session must be started inorder to access the session variables.
At the top of the page add:
session_start();
This will start a session. it's also required to have session_start(); on every page that uses the session array.
If you are looking to add data to the session array. You will need to do this after user presses the submit button. Ie,
Form > Redirects to AddSess.php > Adds required values to session > Redirects to the correct page
Ie:
<form action="AddSess.php" method="POST">
<input type="text" name="test">
<input type="submit" name="submit">
</form>
and on AddSess.php
session_start();
if (isset($_POST['submit'])){
$_SESSION[] = $_POST['test'];
header ("Location: index.php");
}
The above is a very basic example on how to append data to a session
Add variable in session variable in PHP
You are overwriting your session variable everytime the page is called. if $_GET['herbew'] is empty, so will be $_SESSION['lang']
Check $_GET before you assign it:
if (isset($_GET['herbew'])) {
$_SESSION['lang'] = $_GET['herbew'];
}
if(isset($_SESSION['lang'])) {
echo $_SESSION['lang'];
}
This question already has answers here:
Showing error without page refresh
(3 answers)
Closed 9 years ago.
I'm trying to create a comment section for my website and need to make it so that after the person submits a comment (and it contains errors), it will show the error (a new HTML element) without refreshing the page. I know almost nothing of AJAX/JQuery, so I'll need some help.
Here's what I have so far:
<?php
if(isset($_POST['reply_submit'])) {
$reply = $_POST['new_reply'];
$reply_message = "";
if(empty($reply)) {
$reply_message = "Your comment is too short.";
}
}
?>
<html lang="en">
<body>
<form class="no-margin" method="POST" action="">
<textarea name="new_reply" placeholder="Write a reply..."></textarea>
<button name="reply_submit" class="btn post-button" type="submit">Post' . (isset($reply_message) ? '<div class="comment-warning">' . $reply_message . '</div>' : '') . '</button>
</form>
</body>
</html>
So what I need it to do is, if the person's comment box doesn't meet the criteria (in this case, an empty field), I need it to show this error line without refreshing the page:
<button name="reply_submit" class="btn post-button" type="submit">Post' . (isset($reply_message) ? '<div class="comment-warning">' . $reply_message . '</div>' : '') . '</button>
Please help.
HTML mostly the same, add onsubmit action
<form class="no-margin" method="POST" action="" onsubmit=verify();>
<textarea name="new_reply" id="new_reply_textarea" placeholder="Write a reply..."></textarea>
...
</form>
<div class=comment-warning></div>
JS
function verify()
{
if ($('#new_reply_textarea').is(':empty'))
{
$('.comment-warning').html("Your comment is too short.");
return false;
}
else
{
return true;
}
}