J
J
Jackmoon2016-03-21 13:52:41
API
Jackmoon, 2016-03-21 13:52:41

Yandex API online translator. The request sends the translation, in the lower form, but along with the original language. How to fix it?

Ajax sends, but does not work correctly, I think it's a Get request, I can't figure out how to fix the error...please help!
actual form-html:

<!DOCTYPE HTML>
<html>
<head>
<title>переводчик</title>
<style>
* { font-family:Calibri }
fieldset { margin-bottom: 10px; padding: 10px; border:1px #b7410e solid;}
legend { padding: 5px 3px; font-family:GOST_Common ;color:#b7410e; font-size:20px; font-weight: bold; font-variant: normal;}
   select { width: 254px; }
input[type=submit] { width: 170px; padding: 10px;}
</style>
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
    <script src="http://malsup.github.com/jquery.form.js"></script> 
     <script> 
        // wait for the DOM to be loaded 
        $(document).ready(function() { 
            // bind 'myForm' and provide a simple callback function 
            $('#comment').ajaxForm(function() { 
                alert("Thank you for your comment!"); 
            }); 
        }); 
    </script> 
    <script type="text/javascript">
      	function send(){
            $.ajax({
                url:    'Translate.php?action=sample1',
                type:     "GET", //Тип запроса
                dataType: "html", //Тип данных
                data: $("#forma").serialize(), 
                success: function(data) {
                    $('#field').html(data);
                },
                error: function(data) { //Если ошибка
                $('#field').html("Ошибка при отправке формы");
                }
             });
        }
  </script>
</head>

<body>
<form id="forma" action="Translate.php" method="get"> <div style="WIDTH: 150px">
  <fieldset>
    <legend>Ваш текст</legend>
  <select name="sport">      <!--первая форма перевода-->
        <option value="Русский">Русский</option>
        <option value="Английский">Английский</option>
        <option value="Китайский">Китайский</option>
        <option value="Болгарский">Болгарский</option>
        <option value="Айзербайджанский">Айзербайджанский</option>
    </select>
  <select name="sport">
        <option value="Русский">Русский</option>
        <option value="Английский">Английский</option>
        <option value="Китайский">Китайский</option>
        <option value="Болгарский">Болгарский</option>
        <option value="Айзербайджанский">Айзербайджанский</option>
      </select>
  <p><textarea name="comment" id="comment" cols="85" rows="7"> </textarea>
        </fieldset>
         <fieldset>
    <legend>Перевод</legend>
    
<p><textarea name="field" id="field" cols="85" rows="7"></textarea>
        </fieldset>
    
  <a onclick="send()">Отправить</a>
  </p>
</form>


</body>
</html>

Translate.php (loads the API using the GET method)
<?php
error_reporting(-1);
header('Content-Type: text/html; charset=utf-8');
$comment=$_GET["comment"];
print $comment;
 $xml = simplexml_load_file("https://translate.yandex.net/api/v1.5/tr/translate?key=trnsl.1.1.20160122T121122Z.b28c9e1fadbb4e43.f5f40d8f7eb1f4ba1da5198c7db7ee5676a37be4&text=$comment&lang=eng-ru");
 
    print($xml->text);
?>

translate.php (displays the translation on the screen)
<?php
include 'Translate.php';

$movies = new SimpleXMLElement($xmlstr);

echo $movies->movie->print_r($xml);
?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vlad Ermolov, 2019-08-26
@Ermolov

Correct Translate.php to look like this

error_reporting(-1);
header('Content-Type: text/html; charset=utf-8');
$comment=$_GET["comment"];

 $xml = simplexml_load_file("https://translate.yandex.net/api/v1.5/tr/translate?lang=ru-en&key=trnsl.1.1.20160122T121122Z.b28c9e1fadbb4e43.f5f40d8f7eb1f4ba1da5198c7db7ee5676a37be4&text=$comment");
 
    print($xml->text);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question