在jQuery中启动POST方法而不涉及任何输入变量

时间:2018-03-15 21:54:23

标签: javascript php jquery sql ajax

我是jQuery和PHP的新手。这可能是一个微不足道的问题。 通常,jQuery处理表单输入,将其发布到PHP,然后让PHP将其传递给数据库。 就我而言,我拥有当前用户的地理位置,并将用户的地理位置与目标在JavaScript中的地理位置进行比较。如果这两个位置接近意味着用户到达目的地,则通过在数据库中归档的Place_Id下为当前用户插入目的地的标识符(我们只是说Id = 1以保持简单)来记录数据库。数据库中的表只有两列(userId和placeId)。 我想知道如何通过jQuery和PHP实现。

以下是地理位置比较的JavaScript代码。 我需要函数postIt()来帮助使用jQuery和关联PHP启动PHP。

  <script type="text/javascript" ,  
  src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
  </script>
  <script>
  var lat;
  var long;

  window.onload=function(){
  getLocation();
  }


  function getLocation() {
  if (navigator.geolocation) {
  watchId = navigator.geolocation.watchPosition(showPosition, locationError,
  {maximumAge:0, timeout:10000, enableHighAccuracy:true});
  } 
  else {
  alert("Browser doesn't support Geolocation. Visit http://caniuse.com to 
  discover browser support for the Geolocation API.");
     }
   }


  function locationError(error) {} // error function here


  function showPosition(position) {
  x.innerHTML = "Latitude: " + position.coords.latitude +
  "<br>Longitude: " + position.coords.longitude;
  lat = position.coords.latitude;
  long = position.coords.longitude;

  comparePosition();
  }


  function comparePosition()
  {
       var userCurrentLat = lat;
       var userCurrentLong = long;
       var Dest1_Lat = 38.00;     //this is just for demo
       var Dest1_Long = -72.00;   //this is just for demo
  if (userCurrentLat == Dest1_Lat 
  &&  userCurrentLong == Dest1_Long)//just a simplified way of comparison 
  {
    postIt();

  }}

  function postIt()
  { $.post ('insertDest1.php', {current_user_id, //pseudo jQuery code here
  destinationId(1)}, callback function() )      //where I need help
    }
 </script>

PHP(insertDest1.php)

 <?php
 include ('mysqli_connect.php');
 $query = "INSERT INTO user (userId,placeId) VALUES 
    ('current_user_id' , '1')";     
 $result = @mysqli_query ($dbc, $query); // Run the query.
 if ($result) { // If it ran OK.

 // Print a message.
 echo '<h1 id="mainhead">Success!</h1>';
 }

 else { // If it did not run OK.
 echo '<h1 id="mainhead">Error</h1>';
 }
 ?> 

1 个答案:

答案 0 :(得分:0)

使用$ .ajax获取更多配置选项:

function postIt()
{ 
    $.ajax({
        url: 'insertDest1.php',
        type: 'POST',
        data:{
            userId: 'current_user_id', // replace with actual user id
            placeId: 'the_place_id' // replace with actual place id 
        },
        success: function(serverResponse) {
            // handle output from server here ('Success!' or 'Error' from PHP script)
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            // handle any network/server errors here
            console.log("Status: " + textStatus); 
            console.log("Error: " + errorThrown); 
        }
    });
}

设置PHP文件以处理来自AJAX的POST数据

<?php
    include ('mysqli_connect.php');

    # Always sanitize input from $_POST variable to prevent SQL injection
    $userId = $dbc->escape_string($_POST['userId']); // current_user_id
    $placeId = $dbc->escape_string($_POST['placeId']); // the_place_id

    $query = "INSERT INTO user (userId, placeId) VALUES ('".$userId."' , '".$placeId."')";     
    $result = @mysqli_query ($dbc, $query); // Run the query.

    if ($result) { // If it ran OK.
        // Print a message.
        echo '<h1 id="mainhead">Success!</h1>';
    }
    else { // If it did not run OK.
        // Print error.
        echo '<h1 id="mainhead">Error</h1>';
    }
?> 
相关问题