Victor York
Victor York

Reputation: 1681

How to send an ID through AJAX to another file and use the ID in thus file

I have an HTML file which contains various fields as follows(I will only show one example since the rest are the same):

<map name="parcerlas">
    <area shape="poly" coords="618,4,618,74,681,76,683,56,684,39,684,27,683,14,682,4" href="#" alt="Parcela 9" title="Parcela 9" onclick="getdata('9');">
</map>

Using the functiong getdata() I retrieve the ID inside and I need to pass this ID using AJAX to another PHP file and use it in the new file. How can I achieve this?

Here is my getdata() function:

<script type="text/javascript">
    function getdata(id){
    alert(id);  
        $.ajax({
           type: "POST",
           url: "activations.php",
           data: 
           {
              id;
           }
    }
</script> 

Upvotes: 0

Views: 1934

Answers (3)

D&#225;vid Szab&#243;
D&#225;vid Szab&#243;

Reputation: 2247

You need to specify the key of the data:

data: { 'id': id }

Also there are syntax errors in your snippet:

function getdata(id){
    alert(id);  
    $.ajax({
        type: "POST",
        url: "activations.php",
        data: {
            'id': id // < no need for semi-colon, properties are key->value based, 'key': value
        }
    }); // << no closing bracket and semi-colon
} // << close your function

So you can access it in your activations.php:

$_POST["id"]

Always check if it is isset or not:

if (isset($_POST["id"])) $id = $_POST["id"]; else exit;

Answering OP's particular questions:

You need to open a PDO connection to your SQL server (example with MySQL)

if (isset($_POST["id"])) $id = $_POST["id"]; else exit;

$db = new PDO('mysql:host=localhost;dbname=yourdb;charset=utf8', 
              'username', 
              'password'); // Open a connection to your database
$q = $db->prepare("SELECT * FROM achievements WHERE id=?"); // Get all achievements where id=$id
$q->execute(array($id)); // Run the query

$rows = $q->fetchAll(); // Fetch the query, associated array

echo json_encode($rows); // Convert the $rows to json, so javascript can read it

You will have to set the datatype of your ajax request to JSON or parse the data on your own with $.parseJSON(data);

You can use the returned data with adding a success function to your ajax:

function getdata(id){
    alert(id);  
    $.ajax({
        type: "POST",
        url: "activations.php",
        data: {
            'id': id // < no need for semi-colon, properties are key->value based, 'key': value
        },

        success: function(data) {
            var achievements = $.parseJSON(data);

            // use achievements variable
        }
    }); // << no closing bracket and semi-colon
} // << close your function

Upvotes: 2

GoalDone
GoalDone

Reputation: 343

Change id; to 'id':id and no semicolon(;)

Upvotes: 0

iCollect.it Ltd
iCollect.it Ltd

Reputation: 93601

You have a few typos (no closing brackets on Ajax call, unwanted semi-colon etc), but mainly need to provide the data as a key value pair. Note: the quotes are only required in a key name if it is not a single word (no spaces or hyphen/- etc):

<script type="text/javascript">
    function getdata(id){
    alert(id);  
        $.ajax({
           type: "POST",
           url: "activations.php",
           data: 
           {
              id: id
           }
        });
    }
</script> 

Upvotes: 0

Related Questions