I want to make button that only appears when you make a post but after you reload page or click the button that button disappear. The button should redirect you to page with your posts. I tried to make it with SESSION but it doesn't seem to work. Can you guys post how would you do that.
EDIT: I fixed typing mistake, still nothing shows up.
<?php
session_start();
include ('init.php');
if(isset($_POST['error'])){
echo($_SESSION['error']);
unset($_SESSION['error']);
}
if(isset($_POST['success'])){
header('location: index.php');
}
if(isset($_POST['submit'])){
$title = $Users->checkInput($_POST['title']);
$text = $Users->checkInput($_POST['text']);
if($Post->postMessage($title,$text)){
$_SESSION['error'] = '<input type="submit" name="success">'; /* Session with input i want to show only once */
header('location: POST.php');
return;
}
}
?>
<html>
<head><title></title></head>
<body>
<form method='POST'>
Title: <input type="text" name="title">
Text: <input type="text" name="text">
<input type="submit" name="submit">
</form>
Go back
</body>
</html>
This is my class file:
class Post extends Users{
public function postMessage($title,$text){
$sql = "INSERT INTO forum(title,text,date,forum_id) values(:title,:text,:date,:forum_id)";
$stmt = $this->pdo->prepare($sql);
$stmt->execute(array(
':title' => $title,
':text' => $text,
':date' => date('Y-m-d H:i:s'),
':forum_id' => $_SESSION['id']
));
return true;
}
}
I believe that instead of if(isset($_POST['error'])), you should do an isset of $_SESSION, i.e:
if(isset($_SESSION['error'])){
echo($_SESSION['error']);
unset($_SESSION['error']);
}
Related
Session not working:
On-Page 1, I am getting Client ID using the form and redirecting the user to auth.marketingcloudapis.com for validation which then redirects to "https%3A%2F%2Fpharmtechsonly.com%2Fwebform%2Fcollect" which is my page 2.
Page 1 :
<html>
<body>
<?php
session_start();
if(isset($_POST['webform']))
{
$Client_ID = $_POST['Client_ID'];
$YOUR_SUBDOMAIN = $_POST['YOUR_SUBDOMAIN'];
$_SESSION["Client_ID"] = $_POST['YOUR_SUBDOMAIN'];
$_SESSION["YOUR_SUBDOMAIN"] = $_POST['YOUR_SUBDOMAIN'];
$redirect = "https://".$YOUR_SUBDOMAIN.".auth.marketingcloudapis.com/v2/authorize?response_type=code&client_id=".$Client_ID."&redirect_uri=https%3A%2F%2Fpharmtechsonly.com%2Fwebform%2Fcollect%2Dcode&scope=email_read%20email_write%20email_send&state=mystate";
header("Location: $redirect");
session_write_close();
exit();
}
?>
<form action="" method="post">
Client ID:<br>
<input type="text" name="Client_ID">
<br>
YOUR SUBDOMAIN:<br>
<input type="text" name="YOUR_SUBDOMAIN">
<br><br>
<input type="submit" name="webform" value="Authorize">
</form>
</body>
</html>
Page 2 :
<?php
session_start();
$Client_ID = $_SESSION["Client_ID"];
echo $Client_ID;
?>
I am simply echoing $_SESSION["Client_ID"] which does not work immediatly when I reach page 2, however works when I hit refresh.
You're assigning $_POST['YOUR_SUBDOMAIN']
to $_SESSION['Client_ID']
instead of $Client_ID
i feel dumb (like always)
So i have website, and finally trying to move what i build on localhost to my website but the hell that's not working somehow. Finally after research what i get is my session is not working.
So i try make simple php session on my website and it's somehow the session variable not setting like i want.
This is the example i try :
http://dofaiyah.com/shop/login/ << access this, when clcik submit, i make $_SESSION["token"] = "green" and then redirect to index (http://dofaiyah.com/shop/index.php) and echo that session.
But that's not working.
My code is very simple
On Login
<?php
session_start();
if ( isset( $_POST['submit'] ) ) {
$_SESSION["token"] = "green";
header("Location: ../index.php");
//echo $_SESSION["token"];
}
?>
<html>
<body>
<form action="" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
On index
<?php
session_start();
if(!isset($_SESSION['token'])){
//header("Location: login");
}
else {
$_SESSION['views'] = 1;}
echo "views = ". $_SESSION['token'];
?>
Like what i'm missing? i really don't know.
//////
Edit : yes i'm already try to excecute it at my localhost and it's working perfectly.
On Login You are redirecting before to show the session value .
I'm new to PHP. I want to display a message that the database is updated after each time I redirect it after entering the data.
$sql = "INSERT INTO incoming (recipt, userid, username, money)
VALUES ('$recipt', '$userid', '$username', '$money')";
if ($conn->query($sql) === TRUE) {
echo "<script>window.open('incoming2.php','_self')</script>";
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
2 methods
1-you can redirect to any page adding message in get variable and check at that page if that variable is set then display it as message
//redirect to index.php with msg as
header('location:index.php?msg=2 records updated');
//at index page where you want to display message
if(isset($_GET['msg']) && !empty($_GET['msg'])){
echo '<p class="myMsg">'.$_GET['msg'].'</p>'
}
2- second method is to save the message to session variable and access it at page but you will have to unset that variable as below
//sending message assuming session_start() is written at to of all pages
$_SESSION['msg']="2 records updated or what ever your message is";
//where you want to display message
if(isset($_SESSION['msg']) && !empty($_SESSION['msg'])){
echo '<p class="myMsg">'.$_SESSION['msg'].'</p>'
unset($_SESSION['msg']);
}
Pass the message to your url:
echo "<script>window.open('incoming2.php?message=New+record+created+successfully','_self')</script>";
Then you can get the message in incoming2.php:
echo urldecode($_GET['message']);
Be careful: sanatize your input!
Use header("Location: incoming2.php"); instead of echoing JS.
Also, check your SQL statement for SQL injection vulnerabilities.
If you are posting the data on the same page you could do the following:
<?php
if(isset($_REQUEST["submit"])){
// mySQL code here
// return either success or failed
$confirmation="success";
}
?>
<html>
<head>
<title>Feedback</title>
</head>
<body>
<div>
<?php
if(isset($confirmation)){
echo $confirmation;
}
?>
</div>
<form method="post" action="">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" name="submit" Value="Submit">
</form>
</body>
</html>
If you are sending the data to a separate page:
On the receiving page:
<?php
// mySQL code here
// return either success or failed
//redirect to index.php with confirmation as true or false
header('location:index.php?confirmation=success');
?>
and on the page that you Sent the data from:
<html>
<head>
<title>Feedback</title>
</head>
<body>
<div>
<?php
//at index page where you want to display message
if(isset($_GET['confirmation']) && !empty($_GET['confirmation'])){
echo $_GET['confirmation'];
}
?>
</div>
<form method="post" action="uploaddata.php">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" name="submit" Value="Submit">
</form>
</body>
</html>
You can then use CSS3 animations to fade the message in and out for a better user experience :-)
<?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.
Username and password not appear on Page 2.PHP although I post it to Page2.PHP
Page1.PHP
<form name="form1" method="post" action="Page2.php">
<input type="text" name="txtLogin">
<input type="password" name="txtPWD">
<input type="submit" name="btnSub" value="go">
</form>
Page2.PHP
<?php
if(isset($_REQUEST['txtLogin']))
{
session_start();
$_SESSION['login']=$login;
}
if(isset($_SESSION['login']))
header('Location: detail.php');
else
header('Location: index.html');
?>
put this on page2.php
if(isset($_POST['txtLogin']) && isset($_POST['txtPWD']))
{
//get values & do other scripts like saving values on sessions
$user = $_POST['txtLogin'];
$pass = $_POST['txtPWD'];
echo $user.'<br>'.$pass;
}
else
{
//event here
}
The problem is here:
$_SESSION['login']=$login;
You are using the $login variable, but it isn't actually being set anywhere.
A few lines further up, we see that the login name is actually in $_REQUEST['txtLogin'], not $login. So you should be using that.
$_SESSION['login']=$_REQUEST['txtLogin'];
Hope that helps.
Check settings: enable_post_data_reading, request_order, variables_order, gpc_order on http://www.php.net/manual/en/ini.core.php