0

I want to change a session value and a <p> text using submit type button. However, the session does changed, but the text not changed.
Here's the PHP codes inside the body.

<?php
  if (isset($_POST['lang']) == true && !empty($_POST['lang'])) {
    $_SESSION["language"]=$_POST['lang'];
  }
?>

<p>My Name is Khan</p>

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'])?>" method="post">
    <button type="submit" name="lang" value="jpn" id="jpn">日本語</button>
    <button type="submit" name="lang" value="eng" id="eng">English</button>
</form>

Here's the Javascript codes

$(document).ready(function(){
    $("#jpn").on('click', function(){
      $("p").text("私の名前はハーンです");
  });

  $("#eng").on('click', function(){
      $("p").text("My name is Khan");
    });
});

When I click the Japanese button, the text changed for a flash but changed back to the original.
How can I fix this? thank you..

5 Answers 5

1

Problem is that you also submitting form, which reloads page if you don't want to reload page add preventDefault() - it will not submit form.

$(document).ready(function(){
    $("#jpn").on('click', function(e){
        e.preventDefault();
      $("p").text("私の名前はハーンです");
  });

  $("#eng").on('click', function(e){
        e.preventDefault();
      $("p").text("My name is Khan");
    });
});

But if you need to reload page to set your session, then you need to change text in paragraph with php:

<?php
  if (isset($_POST['lang']) == true && !empty($_POST['lang'])) {
    $_SESSION["language"]=$_POST['lang'];
    if($_POST['lang'] == 'jpn'){
      echo '<p>私の名前はハーンです</p>';
    }else{
      echo '<p>My Name is Khan</p>';        
    }
  }
?>



<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'])?>" method="post">
    <button type="submit" name="lang" value="jpn" id="jpn">日本語</button>
    <button type="submit" name="lang" value="eng" id="eng">English</button>
</form>
Sign up to request clarification or add additional context in comments.

Comments

1

1.) php's isset returns an boolean, no need to check == true.

if( isset($_POST['lang']) && !empty($_POST['lang']) )
    $_SESSION["language"] = $_POST['lang'];

2.) If you want to post a form to the same document, you can leave the action of your form just empty as well.

<form action="" method="post">
</form>

3.) You should write the initial state to the document by php, to prevent the wrong display when the user hit's reload. Otherwise the user will submit the form, the variable will be set, but after reload alsways the text My name is Khan will be shown.

You can do it like this, for example in a single line:

echo "<p>" . ($_SESSION["lang"] == "jpn" ? "私の名前はハーンです" : "My name is Khan") . "</p>";

Or as complete if:

if( $_SESSION["lang"] == "jpn" )
    echo "<p>私の名前はハーンです</p>";
else
    echo "<p>My name is Khan</p>";

Comments

0

$(document).ready(function(){
    $("#jpn").on('click', function(){
      $("p").text("私の名前はハーンです");
  });

  $("#eng").on('click', function(){
      $("p").text("My name is Khan");
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>My Name is Khan</p>

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'])?>" method="post">
    <button type="submit" name="lang" value="jpn" id="jpn">日本語</button>
    <button type="submit" name="lang" value="eng" id="eng">English</button>
</form>

Comments

0
<p><?php echo (isset($_SESSION["language"]) && $_SESSION["language"] == 'jpn') ? '私の名前はハーンです':'My name is Khan';?></p>

just put this condition in

tag

Comments

0

This is because form action is going with refresh .You need to prevent by js event

$(document).ready(function(){
    $("form").on('click','#jpn', function(e){
        e.preventDefault();
      $("p").text("私の名前はハーンです");
    });

  $("form").on('click','#eng', function(e){
        e.preventDefault();
      $("p").text("My name is Khan");
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.