Would like to know whether I can use session method for language translate? I tested the code and it's working fine.
This is the Form button
<form method="post">
<?php
if(isset($_POST['english'])){
echo "<button type='submit' name='Chinese'>Chinese</button>";
}else{
echo "<button type='submit' name='english'>English</button>";
}
?>
</form>
PHP Session to translate
<?php
if(isset($_POST['english'])){
$_SESSION['english'] = $_POST['english'];
}
if(isset($_POST['chinese'])){
unset($_SESSION['english']);
}
if(isset($_POST['english'])){
echo "English";
}else{
echo "Chinese";
}
?>
My code is working fine but i just want to confirm is that OK to use session for translate the language
Related
Hi I am having trouble accessing session on other pages. I have done everything but can't solve it either. I start each page with session_start(). I have a form that gets username name and password and sends it to another page that saves this information in session array and redirects to the previous page but it seems like the session is not changed. It would be hard to help and understand without the code so here it is:
In my sigin.php,
<?php
session_start();
$_SESSION['errorMessage']='0';
include("header.html");
if($_SESSION['loggedin']){
print("<div class='pageContent'>
<h2>Sign in</h2>
<form method='POST' action='sign.php'>
<input type='text' maxlength='20' name='username' placeholder='Username'/ required><br/>
<input type='text' maxlength='30' name='password' placeholder='Password'/ required><br/>
<input type='submit' name='submit' value='Sign in' style='padding:0.5em;margin-left:45%;font-family:cursive,helvetica,Arial; color:darkred;cursor:pointer;'/>
</form>");
if($_SESSION['errorMessage']){
print("<div style='font-family:inherit' color:red' margin:1em;'>"
. $_SESSION['errorMessage'] . "</div>");
}
print("<div style='font-family:inherit;padding-
bottom:1.5em;margin:1em;'>
Don't have an account? <a href='' style='text-
decoration:none;'>Click here</a>
to sign up.
</div>
</div>");
}
else{
print("<div class='pageContent'>
<h2>You are logged in.</h2>
</div>
");
}
include("footer.html");
?>
and the page that handles the form input:
<?php
session_start();
if($_SESSION['username']==$_POST['username']&&$_SESSION['password']==$_POST['password'])
{
$_SESSION['loggedin']='true';
$_SESSION['errorMessage']='';
}
else{
$_SESSION['errprMessage']='The username and password combination is invalid!';
$_SESSION['loggedin']='false';
}
header("Location:signin.php");
exit();
?>
Please help me as I can't find any help on internet about this. Thanks...
I am new to programming and am experimenting with sessions. I believe the code I have written is correct but after spending time trying to wrap my head around the concept, I am not able to figure out why the program isn't working.
When I debugged the code in the session-page.php, control goes to if(isset...) but then instead of entering the code block or showing a submit button on the browser, simply moves to the next session variable. Someone please be kind enough to explain it to me why this thing ain't working.
Also, can I not use <form> and simply use isset($_GET[ ])?
session-page.php[CODE]
<?php
session_start();
if(isset($_POST['sub']))
{
$_SESSION['xyz']="Hello World";
}
$_SESSION['abc']="Hey Buddy!";
?>
<form method="post">
<input type='submit' name='sub' value='redirect'>
</form>
test-page.php[CODE]
<?php
session_start();
if($_SESSION['xyz']!="Hello World")
{
header("location:session-page.php");
}
echo $_SESSION['abc'];
?>
You want to have this type of workflow (based on what you currently have):
/session-page.php
<?php
session_start();
if(!empty($_POST['sub'])) {
$_SESSION['xyz'] = "Hello World";
header('Location: test-page.php');
exit;
}
if(empty($_SESSION['abc']))
$_SESSION['abc']="Hey Buddy!";
?>
<form method="post" action="#">
<input type='submit' name='sub' value='redirect'>
</form>
/test-page.php
<?php
session_start();
if($_SESSION['xyz'] != "Hello World") {
header("location:session-page.php");
exit;
}
echo $_SESSION['abc'];
?>
try this.
<form method="post" action="" >
<input type='submit' name='sub'value='redirect'>
</form>
I wrote the following login.php file.
<?php
session_start();
//Check everything and if everything is correct and the username and password is correct and available
echo "Successfully";
$_SESSION['login_user'] = $username;
// and etc
?>
Now if the username is session as the the result $_SESSION['login_user'] value is session also.
and then I create check-session.html file and it is as follows:
<html>
<body>
<form method = "POST" action = "check.php">
<input type = "submit" value = "check-session">
</form>
</body>
</html>
And then the check.php file is as follows:
<?php
if(isset($_SESSION['login_user'])) {
echo "session is available";}
else { echo "session is not available"; }
?>
But the problem is when the login operation is successfully and now I want to know that the session is created really or not, after clicking the check-session button in the check-session.html page, I see the result from server as the follows:
session is not available
Also for more information I use wamp server.
Put session_start(); in the start of every page that's using sessions or is related to them in any way.
In the start of your check.php file
<?php
session_start();
if(isset($_SESSION['login_user'])) {
echo "session is available";}
else { echo "session is not available"; }
?>
You can solve this problem by making a separate file for setting session and include that file on the starting of each logged in page.
this is c.php for checking session...
<?php
include 'b.php';
if(isset($_POST['check_session']))
{
if(isset($_SESSION['login_user']))
echo "session is available";
else
echo "session is not available";
}
?>
<form method = "POST" action = "c.php">
<input type = "submit" name="check_session" value = "check-session">
</form>
a.php for login
<?php
if(isset($_POST['login']))
{
header("Location: c.php");
}
?>
<html>
<body>
<form method = "POST" action = "a.php">
<input type="submit" name="login" value="Login">
</form>
</body>
</html>
and also make b.php simply for setting session using session_start()
Simple php code to post a value and echo it inside if(isset()), when click on submit button, but it's not working.
<?php
if(isset($_GET['q']))
{
$q=$_GET['q'];
//echo "thisss iss qqqq".$q; this is working
$_SESSION['q']=$q;
if($q=="1")
{
?>
<form action='#'>
<tr>
<input type='text' name='txt_code' >
<input type='submit' name='code_sub' value='Showcd'>
</tr>
</form>
<?php
}
//here I wrote code for to echo $bkCode when click on 'showcd' button but it's not working
<?php
if (isset($_POST["code_sub"]))
{
echo $bkCode=$_POST['txt_code']; // THIS IS NOT WORKING
}
}
?>
It doesn't even enter inside the if (isset($_POST["code_sub"])) part
Default action of a form is GET. You have to specify that u want to do a POST :
<form action="#" method="POST">
A first, you should set the var, and after that a simple output:
<?php
if (isset($_POST["code_sub"]))
{
$bkCode=$_POST['txt_code']; // THIS IS WORKING
echo $bkCode;
}
?>
Also you need to set the form method to "POST"!
<form action='the_link' method="POST">
For using the post method
<form action='the_link' method="POST">
This will help ..:)
I have this piece of code and it works fine while isolated into the 2 small scripts I have below. Basically I push a button which makes a call to the database to delete a row.
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
<script>
function removeNo(){
$.ajax({url: "removeno.php",
type: "post",
});
};
</script>
</head>
<body>
<input class='mybutton' type='submit' onclick='removeNo()' value='Remove' />
</body>
</html>
removeno.php
<?php
include 'config.php';
$sql = "DELETE FROM comments WHERE id='24453305255'";
$query = mysql_query($sql);
?>
Now when I take the HTML button part and put it into a larger PHP script it does not work. The entry that I would like to delete is still in the database. I'm still calling the same PHP file 'removeno.php'. The Javascript is in the header just the same as it is above. All files are in the same directory, and I've made sure to include the 'config.php' connection file in the PHP script.
I've not posted my full PHP scripts as it's several hundred lines, however the whole page evaluates to this piece, and evaluates through the "if" maze I have below. It even displays the button as it should with the value of "remove". It would display "submit" if it didnt evaluate correctly. Am I calling this function correctly within this PHP script?
echo "<td>"; //Main Button
if($isleadership == 2) {
if($reviewed == "Yes") {
echo "<input class='mybutton' type='submit' onclick='removeNo()' value='Remove' />";
} else if ($reviewed == "No") {
echo "<input class='mybutton' type='submit' name='".$a."' value='Submit' />";
}
}