I would like to delop a search facility allowing the user to search other user by name, matching users should be listed, each matching user should have a hyperlink labelled"Make Friends", could anybody help me write an ajax code for this, many thanks

Dani AI

Generated

Good starting points from the thread: is right to build a simple PHP endpoint first, then add Ajax; 's Zend_SearchLucene note is valid for advanced needs. Practical priorities for this project are: use modern DB access (not the old mysql functions shown by ), protect against SQL injection, escape output for HTML, limit results, and make the “Make Friends” action a protected POST (not a simple GET link).

A compact workflow and minimal examples (replace credentials and adapt column names):

PHP (search API returning JSON, use PDO and prepared statements):

<?php
header('Content-Type: application/json; charset=utf-8');
$q = trim($_GET['q'] ?? '');
if ($q === '') { echo json_encode([]); exit; }

$pdo = new PDO('mysql:host=localhost;dbname=PeterJones;charset=utf8mb4','dbuser','dbpass',[
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);

$stmt = $pdo->prepare('SELECT id, username, display_name FROM users WHERE username LIKE :q LIMIT 20');
$stmt->execute([':q' => "%$q%"]);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($rows);

Front-end (fetch + render; send friend requests with POST and a CSRF token from a meta tag):

fetch('/search-api.php?q='+encodeURIComponent(query))
  .then(r => r.json())
  .then(results => { /* build list, attach click handlers that POST to /friend-request.php with X-CSRF-Token */ });

Quick security & performance checklist

  • Use password_hash() for any passwords (do not store plaintext). See the PHP docs for password_hash.
  • Escape user data when inserting into HTML (htmlspecialchars) to prevent XSS.
  • Rate-limit and limit result count (e.g., 20 rows) to avoid abuse.
  • Index the username column or use FULLTEXT for larger datasets; for advanced fuzzy search consider Sphinx/Elasticsearch or libraries like Zend_Search_Lucene if you need ranking/stemming.
  • Implement friend-request as an authenticated POST endpoint that validates the requester, checks for duplicates, and returns JSON status.

Troubleshoot: call the PHP endpoint directly in the browser to check JSON output, verify CORS and correct Content-Type, and enable PDO exceptions during development to surface SQL errors. For docs: see PDO prepared statements and password_hash on php.net.

Recommended Answers

All 4 Replies

I would like to delop a search facility allowing the user to search other user by name, matching users should be listed, each matching user should have a hyperlink labelled"Make Friends", could anybody help me write an ajax code for this, many thanks

Are the users in a database? If so what database type is it?

It would be easier to develop a search page in just PHP and HTML, and then when that is working, you can add the "AJAX" part.

For searching, i would recommend you to look into the Zend Framework. It provides flexible classes for searching. Search for Zend_Search_Lucene

Are the users in a database? If so what database type is it?

It would be easier to develop a search page in just PHP and HTML, and then when that is working, you can add the "AJAX" part.

Hi

Thanks, i have got users in my database, i am using phpmyadmin database, could you advice me of the sample php code that i need to develop then will come back to you regarding the Ajax one, this is how my user database php looks like, any advanced stuff i can use?

<?php
$a = $_POST["username"];
$b = $_POST["password"];
$c = $_POST["email"];
$d = $_POST["telephone"];

$conn = mysql_connect("localhost", "PeterJones", "skssksks");
mysql_select_db("PeterJones");

$result=mysql_query ("INSERT INTO users (username,password,email,telephone) VALUES ('$a','$b','$c','$d')");
mysql_close($conn);

echo "$a Great You have successfully signed up!";

?>

I will really appreciate your help regarding this!

Hi

Thanks, i have got users in my database, i am using phpmyadmin database, could you advice me of the sample php code that i need to develop then will come back to you regarding the Ajax one, this is how my user database php looks like, any advanced stuff i can use?
<?php
$a = $_POST["username"];
$b = $_POST["password"];
$c = $_POST["email"];
$d = $_POST["telephone"];

$conn = mysql_connect("localhost", "PeterJones", "skssksks");
mysql_select_db("PeterJones");

$result=mysql_query ("INSERT INTO users (username,password,email,telephone) VALUES ('$a','$b','$c','$d')");
mysql_close($conn);

echo "$a Great You have successfully signed up!";

?>

I will really appreciate your help regarding this!

PHPMyAdmin is a MySQL administration interface written in PHP. So the database you'd be using is a MySQL database.

I'd recommend getting a book on MySQL (or even SQL in general). It will help a lot with creating the MySQL queries needed for your project(s).

To create the search functionality, first you have to create a search form. The form could be a simple once, with just one input box, for the username. eg:

<form action="search.php">

<label for="username">User Name</label>
<input type="text" name="username" />

<input type="submit" value="Search" />
</form>

The HTML form can be in a PHP page, or HTML page. The form when submitted, will send the form field values to "search.php", defined it the form's action attribute.

In your search.php page, you will receive the value entered by the user for the field "username".

eg:

$username = $_GET['username'];

You will want to take this $username and create an SQL query with it, that will search the database for that value, or similar values.

eg:

$username = $_GET['username'];

$conn = mysql_connect("localhost", "PeterJones", "skssksks");
mysql_select_db("PeterJones");

$result=mysql_query ("SELECT * FROM users WHERE username = '".mysql_real_escape_string($username)."'");
mysql_close($conn);

The above does a search for the exact username entered into the form. ie: "WHERE username = '{value}'".

If you want to search for usernames that have the entered value within them use:

WHERE username LIKE '%{value}%'

Take a look at the mysql_query() documentation for how to list the results from the query:
http://www.php.net/manual/en/function.mysql-query.php

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.