-1

http://www.w3schools.com/webservices/tempconvert.asmx/FahrenheitToCelsius Web サーバーから応答を取得するには、ajax を使用して php コードを記述する必要があります。以下は、リクエストを投稿して応答を取得するコードです。投稿リクエストなので、サイトが更新されます。リフレッシュせずに応答が必要です.Ajaxでそれを行う方法がわかりません。私を助けてください。どうもありがとう。

    <?php
    if (isset($_POST['Fahrenheit'])&&$_POST['Fahrenheit']!=null) {
    $out = print_name($_POST['Fahrenheit']);
    }
    else {
     print_form();
    }

    function print_name($name) {
        $ch = curl_init();
        $far = 'Fahrenheit='.$name;
        curl_setopt($ch, CURLOPT_URL,"http://www.w3schools.com/webservices/tempconvert.asmx     /FahrenheitToCelsius");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$far);

        // http_build_query(array('postvar1' => 'value1')));

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $server_output = curl_exec ($ch);
        curl_close ($ch);
        echo 'It is: '.$server_output;
        return $server_output;
    }


    function print_form() {
      echo '
        <form method="post">
    <table>
      <tr>
        <td>Fahrenheit to Celsius:</td>
        <td>
        <input class="frmInput" type="text" size="30" name="Fahrenheit">
        </td>
      </tr>
      <tr>
        <td></td>
        <td align="right">
         <input name="cel" type="submit" value="Submit" class="button">
         </td>
      </tr>
    </form>
      ';
    }

私を助けてください。

4

1 に答える 1

0

あなたの質問は「自分で宿題をしてください」のようなものなので、私の答えは、自分の課題を行うことができる良いサイトを指しています: http://ajaxpatterns.org/XMLHttpRequest_Call

やりたいことは、javascript で xmlHTTPRequest() オブジェクトを使用して、データを Web サーバーに投稿し、応答を読み取ることです。

上記のリンク先のページには、デモへのリンクが壊れています。正しいリンクは次のとおりです: http://ajaxify.com/run/xmlHtttpRequestCall/

ページのソースは非常に興味深いものです。たとえば、次のスニペットを検討してください。

function createXMLHttpRequest() {
  try { return new XMLHttpRequest(); } catch(e) {}
  alert("XMLHttpRequest not supported");
  return null;
}
var xhReq = createXMLHttpRequest();
    xhReq.open("GET", "sumGet.php?figure1=5&figure2=1", false); //here method of sending data and server page where to send to
    xhReq.send(null);
    var serverResponse = xhReq.responseText;
    $("response").innerHTML = serverResponse;
于 2013-08-09T20:41:50.957 に答える