简体   繁体   English

使用ajax在同一文件中将javascript变量传递给php函数,而无需刷新页面

[英]Pass javascript variable to php function using ajax in same file without page refresh

I am using PHP function to display geofences. 我正在使用PHP函数显示地理围栏。 I want to pass javascript variable to php function in same file without page refresh. 我想将JavaScript变量传递给同一文件中的php函数,而无需刷新页面。

function load(id,type){
if(type===2){
  window.location.href="basic.php?idd=" + id;  // i want to change code here

<?php
   $cir =  get_fence_by_type($_GET['idd']);
   if($cir) {
   foreach($cir as $row){
     $fence_id  = $row['geo_id'];
   }
 }
?>

PHP function is: PHP函数是:

function get_fence_by_type($id){
    $query = "Select * from geofence where geo_id=".$id;
    $result = pg_exec($query);
    $d  = array();
    while($myrow = pg_fetch_assoc($result)) { 
        $d[] = $myrow;
    }   
    return  $d; //returns result in array
}

javascript window.location.href passes javascript value to php function but it reloads page also. javascript window.location.href将javascript值传递给php函数,但也会重新加载页面。

If you're using jQuery, you can use $.ajax() . 如果使用的是jQuery,则可以使用$.ajax() It allows you to send data to your server and do something with the response. 它使您可以将数据发送到服务器,并对响应进行处理。

Eg: 例如:

$.ajax({
    type: 'POST',
    data: myvar
    success: function(Response) { console.log(Response); }
});

will send myvar to your server (with a bit of tweaking of parameters of course) and log whatever the server sends back to your browser console. 会将myvar发送到您的服务器(当然需要进行一些参数调整),并记录服务器发送回浏览器控制台的所有内容。

Have a read of what you can do with jQuery. 阅读有关使用jQuery的知识。

You can do this using jQuery. 您可以使用jQuery完成此操作。 Basically you don't refresh the page, you just do an async query to the server and you get the response. 基本上,您无需刷新页面,只需对服务器执行异步查询即可获得响应。 In the following example the response is showed in alert box. 在以下示例中,响应显示在警报框中。

Your "index" file: 您的“索引”文件:

function load(id,type)
{
    $.post("basic.php", { idd: idd }, function(data){ 
          alert(data); 
    });
}

and basic.php 和basic.php

<?php

function get_fence_by_type($id){
$query = "Select * from geofence where geo_id=".$id;
$result = pg_exec($query);
$d  = array();
while($myrow = pg_fetch_assoc($rs)) { 
    $d[] = $myrow;
}   
return  $d;
}
 $cir =  get_fence_by_type($_GET['idd']);
if($cir) {
 foreach($cir as $row){
 $fence_id  = $row['geo_id'];
}
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用ajax调用将php变量传递给javascript函数,并在同一页面上获取响应文本 - pass php variable to javascript function using ajax calls and getting response text on same page 在datepicker onchange函数之后,使用javascript / ajax调用将值发送到同一php页面,而无需刷新 - After datepicker onchange function, send value to same php page with javascript/ajax call without refresh 自动刷新 PHP Function 无需重新加载页面 Javascript / Z3EB7106C3477A60E6BBF5DBFDE1DA59 - Auto Refresh PHP Function without reloading page Javascript / Ajax 在同一页面上使用ajax将js变量传递给php - pass js variable to php using ajax on the same page 将变量传递给wordpress函数并使用ajax刷新它 - Pass variable to wordpress function and refresh it using ajax 将变量从Javascript函数传递到同一php文件中的php - Pass variable from Javascript function to php in the same php file 使用 AJAX 在同一页面上将 Javascript var 传递给 PHP - Using AJAX to pass Javascript var to PHP on same page 将同一页面中的javascript变量传递给php函数问题 - Pass a javascript variable in same page to a php function issue 在同一页面上使用ajax将javascript varibale传递给php变量 - Passing javascript varibale to php variable using ajax on the same page 在同一页面上使用javascript或AJAX更改PHP变量 - Change PHP variable using javascript or AJAX on the same page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM