userzero
userzero

Reputation: 119

Post and Get value by jquery ajax

How can I add my values owdid and visited id after clicking below link by ajax?

<a href="index.php" onclick="insertvisit(<?php echo $interestid;?>)">member1</a>

Below is my insertvisit function. I have defined owdid and interestid

function insertvisit(visitedid) {
  $.ajax({
       type: "POST",
       url: 'insertvisit.php',
       data:{'field1' : owdid, 'field2' : visitedid},
       success:function(html) {
       }
  });
}

and below is insertvisit.php

global $pdo;
$ownid = $_GET['field1'];
$interestid =$_GET['field2'];

$query = $pdo->prepare("UPDATE tablem SET field1= ? WHERE field2= ?");
$query -> bindValue(1, $ownid);
$query -> bindValue(2, $interestid);
$query -> execute();

Please help thanks.

Upvotes: 1

Views: 56

Answers (1)

lalitpatadiya
lalitpatadiya

Reputation: 720

You need to pass both value in function with , separated and also you need to change your function call like bellow

<a href="index.php" onclick="insertvisit(<?php echo $interestid.','.$owdid;?>)">member1</a>

And your function :

function insertvisit(visitedid,owdid) {
   $.ajax({
           type: "POST",
           url: 'insertvisit.php',
           data:{'field1' : owdid, 'field2' : visitedid},
           success:function(html) {
           }
   });
}

and also you need to change your method $_GET to $_POST like below

$ownid = $_POST['field1'];
$interestid =$_POST['field2'];

$query = $pdo->prepare("UPDATE tablem SET field1= ? WHERE field2= ?");
$query -> bindValue(1, $ownid);
$query -> bindValue(2, $interestid);
$query -> execute();

i hope this will help you.

Upvotes: 1

Related Questions