Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

177
Vistas
How can I submit a form through Ajax and process it using php?

I want to submit my form using ajax and process it using PHP. What I have done so far is, have it take the user's location (zip code/postal code) using ajax and process it using PHP to show users data related to his/her location. What I want to do now is add a form and display the relevant data when the form is submitted. I'm entirely new to Ajax, JSON. Is it possible to achieve this? Thanks in advance.

<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>

The following is my HTML code.

 <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>

What I want to achieve is something like this. The following is my php code

    $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


    }
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda