简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM