Hotfix: weatherRequest getCityName -POST serialize

This commit is contained in:
Divlo 2019-12-29 01:45:04 +01:00
parent 10553b62e7
commit 964d5fcf35
4 changed files with 23 additions and 19 deletions

View File

@ -14,7 +14,7 @@ $_SESSION['LAST_CALL'] = date("Y-m-d h:i:s");
include 'keyVariable.php'; include 'keyVariable.php';
ini_set("allow_url_fopen", 1); ini_set("allow_url_fopen", 1);
$cityName = $_COOKIE['city']; $cityName = htmlspecialchars($_POST['city']);
$url = 'https://api.openweathermap.org/data/2.5/weather?q='.$cityName.'&lang=fr&units=metric&appid='.$apiWeather; $url = 'https://api.openweathermap.org/data/2.5/weather?q='.$cityName.'&lang=fr&units=metric&appid='.$apiWeather;
$json = file_get_contents($url); $json = file_get_contents($url);
// $obj = json_decode($json); // $obj = json_decode($json);

View File

@ -8,15 +8,16 @@ $(function () {
} }
}); });
$("#submitWeatherRequest").click(() => { $("#submitWeatherRequest").click((event) => {
event.preventDefault();
const city = $('#cityName').val(); const city = $('#cityName').val();
const cityName = city.split(' ').join('+'); const cityName = city.split(' ').join('+');
const data = `city=${cityName}`;
if(isEmptyValue(cityName)) { if(isEmptyValue(cityName)) {
$('.results').html(emptyMessageError); $('.results').html(emptyMessageError);
} }
else { else {
createSessionCookie("city", cityName); weatherRequest(data);
weatherRequest();
} }
}); });

View File

@ -12,10 +12,11 @@
* @example * @example
* Ajoute au DOM de la page dans la div .results, la météo de la ville demandée. * Ajoute au DOM de la page dans la div .results, la météo de la ville demandée.
*/ */
function weatherRequest() { function weatherRequest(cityData) {
$.ajax({ $.ajax({
type: 'POST',
url: '/php/getWeatherJson.php', url: '/php/getWeatherJson.php',
type: "POST", data: cityData,
success: function(data) { success: function(data) {
try { try {
const json = jQuery.parseJSON(data); const json = jQuery.parseJSON(data);

View File

@ -16,19 +16,21 @@
<div class="text-center"> <div class="text-center">
<img class="function-image" src="/img/function-image/weatherRequest.png" alt="Météo"> <img class="function-image" src="/img/function-image/weatherRequest.png" alt="Météo">
</div> </div>
<div class="form-group"> <form id="weatherForm" action="#" method="POST">
<label for="cityName">Entrez le nom d'une ville :</label> <div class="form-group">
<input name="cityName" type="text" id="cityName" placeholder="(e.g : Paris, FR)" class="form-control"> <label for="cityName">Entrez le nom d'une ville :</label>
<br> <input name="city" type="text" id="cityName" placeholder="(e.g : Paris, FR)" class="form-control">
<div class="form-row text-center"> <br>
<div class="col-12"> <div class="form-row text-center">
<button type="submit" id="submitWeatherRequest" class="btn btn-dark text-center">Envoyer</button> <div class="col-12">
</div> <button type="submit" id="submitWeatherRequest" class="btn btn-dark text-center">Envoyer</button>
</div> </div>
<br> <br> </div>
<p class="results text-center"></p> <br> <br>
<p class="results text-center"></p>
</div>
</div> </div>
</div> </form>
</main> </main>
<!-- Footer --> <!-- Footer -->