开发者

setting session or php variable in javascript?

开发者 https://www.devze.com 2023-04-06 11:11 出处:网络
i am doing a simple html form for two languages(English & Hindi) so i show the link for both language for ex <a href=\"#\"> English </a> / <a href=\"#\">Hindi after that i need t

i am doing a simple html form for two languages(English & Hindi) so i show the link for both language for ex <a href="#"> English </a> / <a href="#">Hindi after that i need to display the form with corresponding language what user clicked one of the hyperlink. so here how do i find out which language link is clicked?

 <a href="#"> English </a> / <a href="#">Hindi

  <?  if($_SESSION['language']=='English'){ ?>
   <form开发者_运维知识库 action="" method="post" name="englishform">
     ......
    </form>
 <? } else { ?>  
 <form action="" method="post" name="hindiform">
     ......
    </form>
 <? } ?>


You can not change PHP code with java script. As PHP code is run on server. Sent to client and then a java script can be run on the result. So you have two options:

Either send both forms. Use css to hide both. An onclick show the corresponding form.

Or the AJAX soltion. Then the user clicks on the link. Use java script to fetch the form from the server (an other URL) and show in in the page.


You could add a hidden field to both forms, containing the selected language.

<form action="" method="post" name="englishform">
  <input type="hidden" name="language" value="en" />
  ...
</form>

<form action="" method="post" name="hindiform">
  <input type="hidden" name="language" value="hi" />
  ...
</form>


<html>
<head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.js"></script> 
    <script type="text/javascript">
    $(function() {
        $("#showEnglishForm").click(function()
        {
            $("#hindiFormDiv").hide();
            $("#englishFormDiv").show();
        });
        $("#showHindiForm").click(function()
        {
            $("#hindiFormDiv").show();
            $("#englishFormDiv").hide();
        });
    });
    </script>
</head>

<body>
    <a href="#" id="showEnglishForm">show English form</a>
    <a href="#" id="showHindiForm">show Hindi form</a>
    <div id="englishFormDiv" style="display:none;">
        <h3>English form</h3>
       <form action="" method="post" name="englishForm">
         ...
       </form>
    </div>
    <div id="hindiFormDiv" style="display:none;">
        <h3>Hindi form</h3>
        <form action="" method="post" name="hindiForm">
         ...
        </form>
    </div>
</body>
</html>


Well, assuming you just want to hide or show a different form based on the hyperlink:

<a class="switcher" rel="englishform" href="#"> English </a> / <a class="switcher" rel="hindiform" href="#">Hindi</a>

<form action="" method="post" id="englishform" style="display:none">
......
</form>

<form action="" method="post" id="hindiform" style="display:none">
......
</form>


<script>

    $("a.switcher").click(function(e){
        var name = $(this).attr("rel");

        $("#" + name).show();

        e.preventDefault();
    });

</script>


I don't know javascript well enough, but if you get jQuery you can do the following:

<a href="en">English</a> / <a href="hi">Hindi</a>

<form action="" method="post" data-lang="en">
    ...
</form>

<form action="" method="post" data-lang="hi">
    ...
</form>

jQuery: (needs to be specified more on your actual page or it will target all links and forms)

$('a').click(function(e){

    e.preventDefault();

    $('form').hide();
    $('form[data-lang="'+$(this).attr('href')+'"]').show();

});

Or without jQuery:

<? if(!$_GET['lang'] || ($_GET['lang'] != "en" && $_GET['lang'] != "hi")){ ?>
<a href="form.php?lang=en">English</a> / <a href="form.php?lang=hi">Hindi</a>
<? }elseif($_GET['lang'] == "en"){ ?>
<form action="" method="post">
    ...
</form>
<? }elseif($_GET['lang'] == "hi"){ ?>
<form action="" method="post" data-lang="hi">
    ...
</form>
<?
}
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消