Quiero enviar mi formulario usando ajax y procesarlo usando PHP. Lo que he hecho hasta ahora es tomar la ubicación del usuario (código postal/código postal) usando ajax y procesarlo usando PHP para mostrar a los usuarios datos relacionados con su ubicación. Lo que quiero hacer ahora es agregar un formulario y mostrar los datos relevantes cuando se envía el formulario. Soy completamente nuevo en Ajax, JSON. ¿Es posible lograr esto? Gracias por adelantado.
<script> function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); } else { x.innerHTML = "Geolocation is not supported by this browser."; } } function showPosition(position) { const KEY = "hidden"; const LAT = position.coords.latitude; const LNG = position.coords.longitude; let url = `https://maps.googleapis.com/maps/api/geocode/json?latlng=${LAT},${LNG}&key=${KEY}`; //ajax request fetch(url) .then(response => response.json()) .then(data => { console.log(data); let parts = data.results[0].address_components; parts.forEach(part => { if (part.types.includes("postal_code")) { const dbParam = JSON.stringify(part.long_name); xmlhttp = new XMLHttpRequest(); xmlhttp.onload = function() { document.getElementById("demooo").innerHTML = this.responseText; } xmlhttp.open("GET","readJson.php?x=" + dbParam); xmlhttp.send(); } }); }) } </script>El siguiente es mi código HTML.
<form action="readJson.php" method="get"> <input id="datee" type="date" name="datee"> <input id="submit" type="submit" name="search"> </form> <div id="demooo"></div>Lo que quiero lograr es algo como esto. El siguiente es mi código php
$obj = json_decode($_GET["x"], false); if(isset($_GET['search'])){ $res=mysqli_query($conn, "SELECT * FROM trin WHERE tr_departuredt LIKE '%$_GET[datee]%' && WHERE tr_postalcode=$obj"); //if no match found if(mysqli_num_rows($res)==0){ echo '<script>alert("Sorry Please try again ")</script>'; echo "<script type='text/javascript'> document.location ='find.php'; </script>"; }else{ while($row= mysqli_fetch_assoc($res)) { //the data i want here } } } //if the search button is not clicked else{ $res=mysqli_query($conn, "SELECT * FROM trin WHERE tr_postalcode=$obj"); while($row= mysqli_fetch_assoc($res)) { //the data i want here }