【问题标题】:initiate POST method in jQuery without involving any input variables在不涉及任何输入变量的情况下在 jQuery 中启动 POST 方法
【发布时间】:2018-03-15 21:54:23
【问题描述】:

我是 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>';
 }
 ?> 

【问题讨论】:

    标签: javascript php jquery sql ajax


    【解决方案1】:

    使用 $.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>';
        }
    ?> 
    

    【讨论】:

    • 谢谢凯文 HR!你的代码对我来说效果很好。
    猜你喜欢
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    • 2018-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    • 1970-01-01
    相关资源
    最近更新 更多