1

I have a php page that uses onclick in the rectangular coordinates of the area of a map.

When the user clicks in this area I need a function on another php page to update MySQL for that user_id

page_1.php - user is logged in as 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); ?>

What JavaScript method would work on page_1.php to trigger the function on page_2.php

4
  • sounds like a job for AJAX Commented Dec 20, 2012 at 1:09
  • What would the AJAX code look like Commented Dec 20, 2012 at 1:09
  • A click and 35 keystrokes to ask that. You could have saved 30 by typing ajax into the address bar of a modern web browser and hitting return. Good devs are lazy. Never forget that. Commented Dec 20, 2012 at 1:32
  • Thanks for your help... 35-30 though = 5 Commented Dec 20, 2012 at 1:47

2 Answers 2

2

You can't just call a PHP function directly from a page. You see, the onclick property and other similar ones such as onblur call a JavaScript function! In order to call a function from another PHP page, what you need is to use AJAX.

Here, this question should be enough for you (and it seems to be a duplicate aswell): using jquery $.ajax to call a PHP function

However, make sure you're using jQuery for this one, but if you aren't, you can replace it with plain old AJAX like this:

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
Sign up to request clarification or add additional context in comments.

Comments

1

assuming jquery, and a very basic example

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']);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.