how to Ajax-ify module what means your name

Here’s how you can make this cool module ajax like.

Download the original module

First install the component and the module normal way.

New what_means_your_name.php file

<?php
/**
* @version 1.1
* @package Joomla
* @copyright Copyright (C) 2010 ofertaweb.ro
* @license GNU/GPL http://www.gnu.org/copyleft/gpl.html
*/

// no direct access
defined(‘_JEXEC’) or die(‘Restricted access’);

$myabsoluteurl=JURI::base();
$url = $myabsoluteurl.’modules/mod_what_means_your_name/’;
$user =& JFactory::getUser();
$uname = $user->name;
?>
<div id=’name_form’ style=’text-align: center’>
<form name=’name_form’ method=’post’><label>Write your name here: </label>
<input name=’name2check’ id=’name2check’ type=’text’ maxchars=’20′ length=’6′ value=’<?php echo $uname; ?>’>
<input type=’submit’ id=’nc_submit’ value=’check name’>
</form>
</div>

<!–  you may want to remove the below line if jquery is already loaded –>
<script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js”></script>

<script type=”text/javascript”>
$(document).ready(function() {
$(‘#nc_submit’).click(function(){
$(“#results”).fadeOut(500);
var name = $(‘#name2check’).val();
$.ajax({
url: ‘<?php echo $url; ?>process.php’,
type: ‘POST’,
data: ‘name2check=’ + name,

success: function(result){

$(‘#results’).empty().remove();
$(‘#name_form’).append(‘<div id=”results”>’+result+’</div>’);
$(‘#results’).hide();
$(‘#results’).fadeIn(1000);
}
});

return false;
});

});
</script>

and now add a new php file process.php. Modify what is with RED

//process.php

<?php
/**
* @version 1.1
* @package Joomla
* @copyright Copyright (C) 2010 ofertaweb.ro
* @license GNU/GPL http://www.gnu.org/copyleft/gpl.html
*/

$link = mysql_connect(‘database_ip’,'user’,'password‘);
mysql_select_db(‘joomla_db‘);
if(!empty($_POST['name2check']))
{
$name = htmlspecialchars(stripslashes($_POST['name2check']));
$max_results = 5;
$show_gender = 1;
$show_origin = 1;
$query = “SELECT * FROM prefix_names WHERE name LIKE ‘%$name%’ LIMIT $max_results”;
$res=mysql_query($query);
while($row=mysql_fetch_assoc($res))
{
echo “<p>”;
echo “<strong>Meaning:</strong> “.$row['meaning'];
if($show_gender==1) echo “<br /><strong>Gender: </strong>”.$row['gender'];
if($show_origin==1) echo “<br /><strong>Origin: </strong>”.$row['origin'];
echo “</p>”;
}
}
?>

Comments are disabled for this post