views:

19

answers:

1

i have this code:

  <html xmlns="http://www.w3.org/1999/xhtml"&gt;
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title></title>
    <script type="text/javascript" src="http://www.google.com/jsapi"&gt;&lt;/script&gt;
    <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 have a var that i want to translate (is name is 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>
+1  A: 

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.

Pekka
it does not work,why?
nisnis84
Are you asking why you can't mix PHP and JavaScript or does your code still not working with the suggested changes?Pekka is right though. The main problem is that you're mixing PHP into your JavaScript.
Anne Schuessler
@nisnis I had an error in the code, corrected. Try again. If that does not work, please provide debugging info like the output of Firefox's Javascript error console.
Pekka
yes i understand what my mistake was.but the code after the change is still not working like i wanted to
nisnis84
@nisnis as far as I can see, it should work fine if you call your script like so: `your_file_name.php?ftext=Hello World` if it doesn't, you need to describe what doesn't work, and how you want it to work.
Pekka
it still dont work it does not show anything in the screeni just want it to show the translation
nisnis84
Is PHP supported where you host your site? Might be a stupid question, although it's not, because some domain hosting services don't support PHP especially for basic hosting packages. If you're working locally, you also need to have PHP support, e.g. using XAMPP or such.
Anne Schuessler
@nisnis please start showing at least a bit of effort and giving more useful information, and/or post a link so we can see online what you're trying.
Pekka
i am using WAMPserver
nisnis84
in the web page where i get the text i have a textarea and using the get method i deliver it to the file i wrote above
nisnis84
Then please post the generated HTML code you are getting, and please check your browser's Javascript error console as I asked six comments ago. I can't believe there is nothing in there if it doesn't work.
Pekka
why it does not show all the code i wrote?
nisnis84
@nisnis you need to select the code and then click the "code" button in the markdown editor (the one with the "010010101" symbol). And I don't mean the originating page, I mean the *target* page right as it appears in your browser (not in your editor).
Pekka
@nisnis I'm off to bed now, but I think @Anne hit the nail on the head. You are redirecting to a `.html` page that is most likely not being parsed by PHP. Try renaming it to `.php`.
Pekka
how do i get the target page ?it dont show anything on the screen
nisnis84