2015-01-09 4 views
1

이것은 내 첫 번째 게시물이므로 도움을 미리 감사드립니다.HTML 버튼을 실행 된 PHP로 바꿉니다

공항 METAR을 가져 와서 표시하기위한 코드 PHP가 있습니다.

<?php 

$location = "EGLL"; 

get_metar($location); 

function get_metar($location) { 
$fileName = "http://weather.noaa.gov/pub/data/observations/metar/stations/$location.TXT"; 
    $metar = ''; 
    $fileData = @file($fileName) or die('METAR not available'); 
    if ($fileData != false) { 
      list($i, $date) = each($fileData); 

      $utc = strtotime(trim($date)); 
      $time = date("D, F jS Y g:i A",$utc); 

      while (list($i, $line) = each($fileData)) { 
        $metar .= ' ' . trim($line); 
        } 
      $metar = trim(str_replace(' ', ' ', $metar)); 
      } 

    echo "<div style=\"color: white;\">METAR FOR $location (Issued: $time UTC):<br>$metar</div>"; 
    } 
?> 

는 현재 버튼을 클릭하면 website.com/metar.php로 리디렉션 내 웹 사이트의 주요 뉴스에 버튼이 있습니다 : 다음과 같이 코드 'metar.php이'입니다. 사람이 어떻게 버튼을 metar.php의 출력으로 대체 될 수 있도록이 코드를 변경하는 말해 줄 수 있다면

<li><button type="submit" style="height: 40px;" class="btn btn-primary" onclick="window.location.href = '/heathrow.php'"/>METAR At London Heathrow</button></li> 

나는 그것을 감사하겠습니다이로 리디렉션하는 것보다 오히려 클릭하면 다음과 같이 코드는 버튼을 클릭하면 website.com/metar.php가 표시됩니다.

내가 만든 그 의미가 있기를 바랍니다.
다시 한번 감사드립니다!

+0

에 오신 것을 환영합니다 SO에! [How to ask] (http://stackoverflow.com/help/how-to-ask) 페이지를 읽어보십시오. 당신은 당신이 지금 시도하지 않은 것을 우리에게 보여줘야한다는 것을 알게 될 것입니다. 때로는 어디서부터 시작해야할지 알기가 어렵 기 때문에 여기에 몇 가지 팁이 있습니다. 자바 스크립트를 사용해야합니다. 보다 구체적으로, 페이지를 요청하려면 AJAX를 사용해야합니다. 즉, metar.php 페이지에 손이있는 것처럼 보이기 때문에이 데이터를 서버 쪽에서 가져 와서 HTML의 어딘가에 배치하고 숨기고 버튼 클릭시 표시 할 수 있습니다. –

+0

죄송합니다. 내가 말했듯이 그것은 사이트에서 내 첫 질문입니다. 단추를 클릭 할 때 어떻게 숨기고 표시 할 수 있는지 보여주는 답변을 게시 하시겠습니까? 다시 한번 감사드립니다. –

+1

이를 달성하기 위해 AJAX 요청을 수행 할 수 있습니다. 어쨌든 PHP는 서버 측 언어이기 때문에 문자 그대로 PHP를 사용하여 페이지를 새로 고치지 않고도 HTML을 편집 할 수있는 방법이 없습니다. 그러나 자바 스크립트를 사용하여 이러한 작업을 수행 할 수 있으며 훨씬 간단한 사용을 위해 jQuery를 사용하여 전체 코드를 매우 단순하게 만들 수 있습니다. http://api.jquery.com/jquery.ajax/의 예를보고 현재 인덱스가 metar.php 스크립트에 AJAX 요청을 수행하도록 구성하십시오.이 스크립트는이 시점에서 값을 반환한다고 가정합니다. – briosheje

답변

3

이것은 AJAX를 사용하여 수행 할 수 있습니다. 아래 코드는 jQuery를 사용하여 어떻게 수행 할 수 있는지 보여줍니다. 여러 버튼의


<div id="dectinationDivId" style="color: white;"></div> 

<button id="buttonId" style="height: 40px;" class="btn btn-primary">METAR At London Heathrow</button> 

<script src="http://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script> 
<script type="text/javascript"> 
    $('#buttonId').click(function(){ 
     $.ajax({ 
      url: "/heathrow.php", 
      success: function(data) { 
       $('#dectinationDivId').html(data); 
       $('#buttonId').hide(); 
      }, 
      error: function(jqXHR) { 
       alert(jqXHR.responseText); 
      } 
     }); 
    }); 
</script> 

더 쉽게 코드를 (추가하고이 경우에는 버튼을 제거)을 유지 할 수 있습니다 더 나은, 더 일반적인, 방법이있다.

HTML/JS :

<script src="http://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script> 
<script type="text/javascript"> 
    $(document).ready(function() { 
     var metarButtons = $('.getMetarButtons'); 
     metarButtons.click(function(){ 
      var clickedButton = $(this); 
      $.ajax({ 
       type: "POST", 
       url: "/metarData.php", 
       data: { location: clickedButton.attr('data-location') }, 
       dataType: 'html', 
       success: function(data) { 

        $('#outputDiv').hide('slow', function() { 
         $(this).remove(); 
        }); 

        metarButtons.show('slow'); 

        var outputElement = $('<div id="outputDiv" style="color: white;">' + data + '</div>'); 
        outputElement.hide(); 
        outputElement.insertAfter(clickedButton); 

        clickedButton.hide('slow', function() { 
         outputElement.show('slow'); 
        }); 
       }, 
       error: function(jqXHR) { 
        alert(jqXHR.responseText); 
       } 
      }); 
     }); 
    }); 
</script> 

<button style="height: 40px;" class="btn btn-primary getMetarButtons" data-location = "LLBG">METAR At Tel Aviv Ben Gurion</button> 
<button style="height: 40px;" class="btn btn-primary getMetarButtons" data-location = "EGLL">METAR At London Heathrow</button> 
<button style="height: 40px;" class="btn btn-primary getMetarButtons" data-location = "EGGW">METAR At London Luton</button> 
<button style="height: 40px;" class="btn btn-primary getMetarButtons" data-location = "KJFK">METAR At New York John F. Kennedy</button> 

PHP (metarData.php) :

<?php 
    $location = $_POST["location"]; 

    get_metar($location); 

    function get_metar($location) { 
    $fileName = "http://weather.noaa.gov/pub/data/observations/metar/stations/$location.TXT"; 
     $metar = ''; 
     $fileData = @file($fileName) or die('METAR not available'); 
     if ($fileData != false) { 
      list($i, $date) = each($fileData); 

       $utc = strtotime(trim($date)); 
       $time = date("D, F jS Y g:i A",$utc); 

       while (list($i, $line) = each($fileData)) { 
        $metar .= ' ' . trim($line); 
       } 
      $metar = trim(str_replace(' ', ' ', $metar)); 
     } 

     echo "METAR FOR $location (Issued: $time UTC):<br>$metar"; 
    } 
?> 
+0

편집 : 작동합니다 !!!!! 대단히 감사합니다 @ MažvydasTadaravičius! –

+0

@ MažvydasTadaravičius 음, 반 작품. [웹 사이트] (http://theobearman.com)를보고 버튼을 클릭하십시오. 맨 위의 작품 만 작동하고 클릭하면 4 개의 정의 된 방송국 중 하나를 무작위로로드합니다. –

+0

@TheoBearman - _id_로 요소를 선택할 때 동일한 _id_을 가진 요소가 여러 개 있으면 첫 번째 요소 만 선택됩니다. 귀하의 경우 4 개의 버튼이 있으므로 4 개의 고유 한 _ids_를 사용해야합니다. 또는 PHP, JS 및 HTML을 일부 수정하여 동일한 클래스와 _location_ 속성을 가진 4 개의 버튼을 PHP에 전달할 수 있습니다. 이렇게하면 _AJAX_ 함수 하나만 선언해야합니다. –

관련 문제