开发者

I have a var that I want to translate (its name is 'ftext') - I am using the GET method

开发者 https://www.devze.com 2022-12-22 08:56 出处:网络
I have this code: <html xmlns=\"http://www.w3.org/1999/xhtml\"> <head> <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"/>

I have this code:

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
        <title></title>
        <script type="text/javascript" src="http://www.google.com/jsapi"></script>
        <script type="text/javascript">

            google.load("language", "1");

            function initialize() {
                $sents =$_GET["ftext"];
                google.language.translate($sents, "en", "he", function(result) {
                    if (!result.error) {
                      var container = document.getElementById("translation");
                      container.innerHTML = result.translation;
                    }
                });
            }
            google.setOnLoadCallback(initialize);
        </script>
    </head>
    <body>
        <div id="translation"></div>
    </body>
</html>

I 开发者_如何学Gohave a variable that I want to translate (its name is in ftext). I am using the GET method to catch it from other web page, the problem is that i dont know how to catch it and use it in the initialize function. What I am doing wrong?

This is the web page I getting the text from:

<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
        <title>MY-Site</title>
    </head>

    <body>
        <form method=get action=new1.html accept-charset=utf8>
            <textarea name=ftext cols=12 rows=3 dir=ltr>
            </textarea><br>
            <input type=submit value=send>
        </form>
    </body>
</html>


I'm not sure what you're doing exactly, but you seem to be mixing up PHP and JavaScript. You can't do that - PHP works on the server side, at the time the HTML is produced; JavaScript works on the client side, when the HTML is received and rendered.

You can influence the behaviour of the JavaScript in PHP by "injecting" PHP output into the Javascript code.

You may want to do something like this:

...
sents = "<?php echo $_GET["ftext"]; ?>";
google.language.translate(sents, "en", "he", function(result) {
....

that way, you give sents the value of the GET parameter in PHP. The browser then makes the translate request using that value.

0

精彩评论

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

关注公众号