使用PHP和jQuery搜索结果的动态链接

时间:2015-10-09 17:19:13

标签: php jquery mysqli joomla

我正在Joomla网站上工作。我目前正在尝试在搜索结果上显示数据库。数据将根据搜索ID结果用户点击从MySQL数据库中提取,并将显示在div上,其中包含id = id,name,email,company_name等。 我能够获得搜索结果,但是当我点击每个项目时,我收到错误Uncaught TypeError: this.format is not a function并且没有显示任何数据。 您可以在此处查看http://onlinepcdoc.com/index.php/members-list搜索magasco或只输入7

我的表格

<script src="http://code.jquery.com/jquery-1.10.2.js"   type="text/javascript"></script>
<script src="./action/scripts/slink.js" type="text/javascript"></script> 


<title>Search</title>

<body>
 <form action="http://comiut.com/index.php/user-records" method="post">
   <input type="text" name="search" Placeholder="enter the search      criteria..." onkeydown="searchq();"/>

 </form>
//Serach result//
  <div id="output"> </div>

//Data to populate upon click on any search result//
    <div id="id"></div> 
    <div id="name"></div>
    <div id="email"></div>
    <div id="company_name"></div>

</body>

slink.js

function searchq() {
var searchTxt = $("input[name='search']").val();

 $.post("../action/subs/search.php/", {searchVal: searchTxt}, function(output) {
 $("#output").html(output);
 });
}


 jQuery('body').on('click', 'a.resultItem', function(e) {
  e.preventDefault();
  jQuery.ajax({
  url: "http://onlinepcdoc.com/action/subs/getItem.php",
  method: 'post',
  data : jQuery(this).data('id') // see the data attribute we used above     in the a tag we constructed
  }).done(function(data) {
  jQuery("#id").html(data.id);
  jQuery("#name").html(data.name);
  jQuery("#email").html(data.email);
  jQuery("#company_name").html(data.company);
  });
});

的search.php

<?php 

    include '../db/connect.php';

global $con; 
if(isset($_POST['searchVal'])) {
  $searchq = $_POST['searchVal'];
  $searchq = preg_replace ("#[^0-9a-z]#i","",$searchq);

  $query = mysqli_query($con,"SELECT * FROM oz2ts_users WHERE oz2ts_users.id LIKE '%$searchq%' OR oz2ts_users.name LIKE '%$searchq%' OR oz2ts_users.username LIKE '%$searchq%' ") or die("Could not search"); 
  $count = mysqli_num_rows($query);
  if($count == 0){
     $output = 'There is no result to show!'; 
       } else{ 
        while($row = mysqli_fetch_array ($query)) {
            $id = $row['id'];
            $name = $row['name'];
            $username = $row['username'];    

   $output .= '<div><a class="resultItem" data-id="' . $id . '">'   
         . $name . ' '.$id.'</a></div>'; 

        }           
    }

  }
  echo($output);
?>

getitem.php

 <?php

 include '../db/connect.php';
 global $con; 
 if(isset($_POST['id'])) {
 $id = intval($_POST['id']);
 $result = mysqli_query($con,"SELECT oz2ts_users.id, oz2ts_users.name,    oz2ts_users.username,  oz2ts_users.email FROM oz2ts_users WHERE oz2ts_users.id =    $id") or die("Could not search"); 
   $row = mysqli_fetch_assoc($result);
   header('Content-Type: application/json');
   echo json_encode($row);
 }

 ?>

Connect.php

<?php

$dbhost = 'localhost';
$dbuser = 'user1';
$dbpass = 'pw';
$dbname = 'db1';

$con=mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>

Error message on chrome console

0 个答案:

没有答案