language change in php - php

i am trying to change the all page language but session is not working . Session is taking previous value and language change after two click so please solve this problem.
$_SESSION['lanuage'] = $_POST['hindi_hidden'];
if (!empty($_SESSION['lanuage'])){
if ($_SESSION['lanuage'] =='hi')
{
$_SESSION['lanuage'] = $_POST['hindi_hidden'];
}
else if ($_SESSION['lanuage'] =='en')
{
$_SESSION['lanuage'] = $_POST['hindi_hidden'];
}
else {}
}
else
{}
<input type="button" name="hi" id="hi" value="Hindi" onclick="get_hindi(this.id);" class="submit_sytle"/>
<input type="button" name="en" id="en" value="Eng" onclick="get_hindi(this.id);" class="submit_sytle"/>

Can you try this
<?php
session_start();
if(isset($_POST['hindi_hidden'])){
$_SESSION['lanuage'] = $_POST['hindi_hidden'];
} else {
$_SESSION['lanuage']='hindi';
}
echo $_SESSION['lanuage'];
?>
<style>
.submit_sytle{
text-transform:capitalize;
}
</style>
<form method="post">
<input type="submit" name="hindi_hidden" id="hi" value="hindi" onclick="get_hindi(this.id);" class="submit_sytle"/>
<input type="submit" name="hindi_hidden" id="en" value="english" onclick="get_hindi(this.id);" class="submit_sytle"/>
</form>

Code after your comment:
You can try this, it works for me:
<?php
session_start();
if (isset($_POST['hindi_hidden'])) {
$_SESSION['lanuage'] = $_POST['hindi_hidden'];
}
if (isset($_SESSION['lanuage']) && ($_SESSION['lanuage'] =='hi' || $_SESSION['lanuage'] =='en')) {
$_SESSION['lanuage'] = $_POST['hindi_hidden'];
}
echo $_SESSION['lanuage'];
?>
<style>
.submit_sytle{
text-transform:capitalize;
}
</style>
<form method="post">
<input type="submit" name="hindi_hidden" id="hi" value="hindi" onclick="get_hindi(this.id);" class="submit_sytle"/>
<input type="submit" name="hindi_hidden" id="en" value="english" onclick="get_hindi(this.id);" class="submit_sytle"/>
</form>
<script>
function get_hindi(lang) {
//alert (lang);
document.getElementById('hindi_hidden').value=lang;
document.form_login.submit();
}
</script>
Output:
english when I click on English
hindi when I click on Hindi

Related

php input form can only be numbers not words

i made a form where you can put a number and show which one is the highest.
i want to make sure you can put any words in this form.
that there show a notification you cant put words in the form
<?php
function maxGetal($getal1, $getal2)
{
if ($getal1 > $getal2) {
return ($getal1);
} elseif ($getal2 > $getal1) {
return ($getal2);
} else {
return ("gelijk");
}
}
?>
<form action="" method="post">
<input type="text" name="eerstegetal" placeholder="Eerste getal"><br>
<input type="text" name="tweedegetal" placeholder="Tweede getal"><br>
<input type="submit" name="submit" value="Bereken hoogste getal">
<p> -----------------------------------------------------------------------</p>
</form>
<?php
if (isset($_POST["submit"])) {
$maxgetal = maxGetal($_POST["eerstegetal"], $_POST["tweedegetal"]);
echo $maxgetal;
}
$input1 = doubleval($_POST["eerstegetal"]);
$input2 = doubleval($_POST["tweedegetal"]);
?>
The following code shows a notification when a letter is typed.
<?php
function maxGetal($getal1, $getal2)
{
if ($getal1 > $getal2) {
return ($getal1);
} elseif ($getal2 > $getal1) {
return ($getal2);
} else {
return ("gelijk");
}
}
?>
<form action="" method="post">
<input type="text" id="eerstegetal" name="eerstegetal" oninput="Eerste_check()" placeholder="Eerste getal"><br>
<input type="text" id="tweedegetal" name="tweedegetal" oninput="Tweede_check()" placeholder="Tweede getal"><br>
<input type="submit" name="submit" value="Bereken hoogste getal">
<p> -----------------------------------------------------------------------</p>
</form>
<?php
if (isset($_POST["submit"])) {
$maxgetal = maxGetal($_POST["eerstegetal"], $_POST["tweedegetal"]);
echo $maxgetal;
}
$input1 = doubleval($_POST["eerstegetal"]);
$input2 = doubleval($_POST["tweedegetal"]);
?>
<script src="https://code.jquery.com/jquery-3.6.1.min.js"></script>
<script>
function Eerste_check(){
var eerstegetal = $('#eerstegetal').val()
var pattern = /^\d+\.?\d*$/;
if(!pattern.test(eerstegetal)){
alert('Eerstegetal should contain only numbers.')
}
}
function Tweede_check(){
var tweedegetal = $('#eerstegetal').val()
var pattern = /^\d+\.?\d*$/;
if(!pattern.test(tweedegetal)){
alert('Tweedegetal should contain only numbers.')
}
}
</script>
Try the following:
<input type="number" name="eerstegetal" placeholder="Eerste getal"><br>
<input type="number" name="tweedegetal" placeholder="Tweede getal"><br>
I hope it helps

Php(sessions)...how to put textbox value in session and pass it to another page?i m getting error

<?php
session_start();
?>
<form action="HardDisk.php" method="post">//form
<input type="text" name="user" />
<?php
if(isset($_POST['user']))
{
$_SESSION['userGet']=$_POST['user'];
}
?>
<input type="submit" value="submit" />
</form>
2nd page
<?php
session_start();
?>
<?php
if (isset($_SESSION['userGet'])) {
$form_value = $_SESSION['userGet'];
}
echo $form_value ;
?>
..........................................................................................................
You have used the text box name with that check the submit button name too.
<form action="HardDisk.php" method="post">//form
<input type="text" name="user" />
<input type="submit" value="submit" name="submit" />
</form>
<?php
session_start();
if(isset($_POST['submit'])&&isset($_POST['user'])) {
$_SESSION['userGet']=$_POST['user'];
}
?>
And in your second page
<?php
session_start();
if (isset($_SESSION['userGet'])) {
echo $form_value = $_SESSION['userGet'];
}
Give the submit button a name of submit then use the following PHP code:
if(isset($_POST['submit'])){
if(isset($_POST['user']))
{
$_SESSION['userGet']=$_POST['user'];
}
}

two HTML forms submitting same php script, but i want 2nd form to run the code following it not the entire script

i want to do somthing like:
1.php:
<html>
<form action=1.php method=POST enctype="multipart/form-data">
Choose a user name:<input type="text" name="username">
<input type="submit" value="Save and Proceed">
</html>
<?php
if(isset($_POST['username']))
{
$name=$_POST['username'];
echo $name;
if($name=='azra')
{
?>
<html>
<form method="POST" action="1.php" enctype="multipart/form-data"></br>
enter age:</font> <input type="text" name="age">
<input type="submit" value="done">
</form>
</html>
<?php
f( isset($_POST['age']))
{
$age=$_POST['age'];
echo $age;
if($age==25)
{
echo "your age is ". $age;
echo"you are eligible";
}
}
}
}
?>
After the second form is submitted i do not want the script 1.php to run from the start but i want it to run the code following the form which is echoing the age only.
i do not want to go putting the later code in second script and accessing the variable of first script through making them session variables.please help. thankyou in advance
Change your condition to this
if ((isset($_POST['username'])) && ($_POST['submit'] == 'Save and Proceed')) {
}
this is the code i changed:
<html>
<form method="POST" action="2.php" enctype="multipart/form-data"></br>
Choose a user name:</font> <input type="text" name="username">
<input type="submit" value="Save and Proceed">
</form>
</html>
<?php
if(isset($_POST['username']) && ($_POST['submit'] == 'Save and Proceed'))
{
$name=$_POST['username'];
echo $name;
if($name=='azra')
{
?>
<html>
<form method="POST" action="2.php" enctype="multipart/form-data"></br>
enter age:</font> <input type="text" name="age">
<input type="submit" value="done">
</form>
</html>
<?php
if( isset($_POST['age']) && ($_POST['submit'] == 'done'))
{
$age=$_POST['age'];
echo $age;
if($age==25)
{
echo "your name is" .$name;
echo "your age is ". $age;
echo"you are eligible";
}
}
}
}
?>

How to perform same event on two different buttons?

I have this piece of code below, supposing it will display no request when no data in database and it will display the request with an accept and reject button when they is data in database. How should i write my code in order to make the accept and reject button perform some action?
<?php
$travelRequest = $user->userTravelRequest($userid);
if(!$travelRequest){
echo '<div class="requestbox">
<p> You have no request from others at the moment. </p>
</div>';
}
else {
foreach($travelRequest as $request){
echo '<div class= "requestbox">
<p>'. $request->trip_name.'</p>
<p>Organised by '.$request->username.'</p>';
?>
<form method="POST">
<div class = "AR-btn">
<input type="button" name="accept" value="Accept"/>
<input type="button" name="reject" value="Reject"/>
<p></p>
</div>
</form>
</div>
<?php
}
}
?>
Use type="submit" instead of type="button", and give them the same name. Then they'll submit the form, and the script can test which button was used from the value of that parameter.
<input type="submit" name="action" value="Accept"/>
<input type="submit" name="action" value="Reject"/>
You then test this with:
if (isset($_POST['action'])) {
if ($_POST['action'] == 'Accept') {
// Add to database
} elseif ($_POST['action'] == 'Reject') {
// Delete from database
}
}
Try this code :-
<?php
if (isset($_POST['accept']) && $_POST['accept'] == 'Accept' ) {
// do what you want
die("Accept is clicked");
} else if (isset($_POST['reject']) && $_POST['reject'] == 'Reject' ) {
// do what you want
die("Reject is clicked");
}
?>
<?php
$travelRequest = $user->userTravelRequest($userid);
if(!$travelRequest){
echo '<div class="requestbox">
<p> You have no request from others at the moment. </p>
</div>';
} else {
foreach($travelRequest as $request){
echo '<div class= "requestbox">
<p>'. $request->trip_name.'</p>
<p>Organised by '.$request->username.'</p>';
?>
<form method="POST">
<div class = "AR-btn">
<input type="submit" name="accept" value="Accept"/>
<input type="submit" name="reject" value="Reject"/>
<p></p>
</div>
</form>
</div>
<?php
}
}
?>
Just try this logic..may help you
$query = "select data from database";
.
.
.
$data = $row['data'];
if($data){ // reject button when they is data in database
echo '<input type="submit" name="action" value="Reject"/>';
}
else{ // viceversa
echo '<input type="submit" name="action" value="Accept"/>';
}

PHP - Processing after input is entered twice in a row

I have a problem with my form.
I have an HTML Form as follows:
<form action="index.php" method="post">
<input type="text" name="text" id="text">
<input type="submit" value="Send" name="send">
</form>
Now my problem is:
Every time I process this form for the first time, it should output an error. The error has to be shown every time, if this is the first try.
If I enter the same input value twice, it should succeed.
I thought about setting a session variable $_SESSION['first_visit] if I'm processing the form for the first time.
I also thought about saving the $_POST['text'] - Value into Session but it's being overwritten every time.
Thank you for your answers.
<?php
extract($_POST);
if( isset($send) )
{
if( $fired <= 0 )
{
$fired++;
echo "Nope, error. send again to succeed";
}
else
{
echo "Yay success";
}
}
?>
<form action="" method="post">
<input type="hidden" name="fired" value="<?php echo isset($fired) ? $fired : 0 ?>">
<input type="text" name="text" id="text">
<input type="submit" value="Send" name="send">
</form>
Here's some quick and dirty snippet. We set a hidden input field with an initial value of 0 and count it up everytime the form has been send. The only thing to do afterwards is to check if the value of the hidden field is bigger than 0
You can use cookies instead of session
<form action="index.php" method="post" id="exampleForm">
<input type="text" name="text" id="text">
<input type="button" value="Send" name="send" onClick="checkVisit()">
</form>
<script>
function checkVisit(){
var isFirstVisit = getCookie("first_visit");
if(isFirstVisit == "" || isFirstVisit == 1){
setCookie("first_visit",0,1);
}else if (isFirstVisit == 0){
setCookie("first_visit",1,1);
document.getElementById("myForm").submit();
}
}
</script>
Thanks for this fast Answer! Your Tipp helped me with my problem a lot.
Here is the working Script:
<?php
session_start();
if(isset($_POST['go'])) {
$text = $_POST['text'];
$fired = $_POST['fired'];
if($fired <= 0) {
$fired++;
echo "Nope";
$_SESSION['val'] = $_POST['text'];
}
else {
if($fired > 0 && $_SESSION['val'] == $_POST['text'] ) {
echo "Success";
}
else {
echo "Failed";
$_SESSION['val'] = $_POST['text'];
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="test2.php" method="post">
<input type="text" name="text" id="text">
<input type="hidden" name="fired" value="<?php echo isset($fired) ? $fired : 0 ?>">
<input type="hidden" name="val" id="val" value="">
<input type="submit" value="Senden" name="go">
</form>
</body>
</html>

Categories