1

我有一个onclick在地图区域的直角坐标中使用的 php 页面。

当用户在此区域单击时,我需要另一个 php 页面上的函数来更新该 user_id 的 MySQL

page_1.php - 用户以 user_id 身份登录

<map id="map_id_01" name="map_name_01">
<area shape="poly" id="area_id_01" class="" title="area_title_01" 
onclick="page_2.php myfunction" 
coords="360,236,696,236,696,448,360,448,360,237"/></map>

page_2.php

<?php myfunction($user_id); ?>

什么 JavaScript 方法可以在page_1.php上触发 page_2.php 上的函数

4

2 回答 2

2

您不能直接从页面调用 PHP 函数。你看,onclick属性和其他类似的,比如onblur调用一个JavaScript函数!为了从另一个 PHP 页面调用函数,您需要使用 AJAX。

在这里,这个问题对你来说应该足够了(而且它似乎也是重复的):using jquery $.ajax to call a PHP function

但是,请确保您为此使用 jQuery,但如果不是,您可以将其替换为普通的旧 AJAX,如下所示:

xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", 'page_2.php', true);
xmlhttp.send('test');
//xmlhttp.responseText will return the response as a text, and xmlhttp.responseXML will return it as xml
于 2012-12-20T01:11:41.470 回答
1

假设 jquery 和一个非常基本的例子

function addMap(userid)
{
    $.ajax({
       type: "POST",
       url: "http://domain.com/page_2.php",
       data: "userid="+userid,
       success: function(msg){
         alert( "Data Saved: " + msg ); //Anything you want
       }
     });
}

page_2.php

if(!empty($_POST['userid'])){
myfunction($_POST['userid']);
}
于 2012-12-20T01:14:27.660 回答