Jump to content

How to use ajax call in a module(to check whether the mobile phone number already exists)


avi_24

Recommended Posts

Hai friends can you help me out to knowing ajax call within a module. The purpose is i have to check whether the mobile phone number is already registered. I want to the function to return just True or false value....The problem is that i am not much into ajax part...so please help me out on how to add an ajax function and call it form a button click through jquery..and show the result in same form without refreshing

Link to comment
Share on other sites

the question is: when you want to check the number?

 

anyway,

you have to create file: for example: ajax-call.php, create it in root dir of your module.

create there code: 

include_once('../../config/config.inc.php');
include_once('../../init.php');

public function to_check_phone_number_here(){
// code to check phone
if (phone)
return 1;
else
return 0;
} 
if ($_GET['ajax']){
echo function to_check_phone_number_here();
}

then in front office create ajax call like:

<script type="text/javascript">
function QuickLook() {
        $.ajax({
                                        url: '{$base_dir}modules/YOUR_MODULE/ajax-call.php',
                                        type: 'get',
                                        data: 'ajax=true',
                                        success: function(data) {
                                                console.log('success');
                                                // OTHER SUCCESS COMMAND - CHECK THE RETURN VALUE
                                        }
                                });
    return false;
    }
</script>
  • Like 4
Link to comment
Share on other sites

Hai Vekia as you told i tried the ajax call but nothing works.... I have done the following (please help me)

 

1) created file ajax-call.php file in root folder of my module with following code

<?php
include_once('../../config/config.inc.php');
include_once('../../init.php');

public function checkReferral(){
// code to check phone
$num=1;

$sql = new DbQuery();
			$sql->select('firstname,lastname');
			$sql->from('customer');
			$sql->where('`id_customer`=\''.pSQL($num).'\'');  //instead of variable $num i want to
			$result = Db::getInstance()->getRow($sql);  	  //an arguement passed as checkReferrals parameter

if (isset($result))
{	
	$name=$result['firstname'].$result['lastname'];
	return $name;
}
else
return 0;
} 
if ($_GET['ajax']){
echo function checkReferral();
}

?>

2) modified the authentication.tpl file inside the themes/default theme directory , by adding following code


	
	<fieldset class="account_creation">
	<h3>{l s='Referral Information'}</h3>
	<p class="text">
		<label for="referralcode">{l s='Enter Your Referral code'}<sup> *</sup></label>
		<input type="text" size="52" maxlength="128" id="referralcode" name="referralcode"  value="{if isset($smarty.post.referralcode)}{$smarty.post.referralcode|escape:'htmlall':'UTF-8'}{/if}" />
		<p class="preference_description">{l s='Referral Code is neccessary for account creation'}</p>
		<p id="demo"></p>
<input type="button" name="check" id="check" onClick="QuickLook()" value="{l s='Check'}" class="exclusive" />
	</p>
</fieldset>
 

	
	<script type="text/javascript">
	{literal}
function QuickLook() {
        $.ajax({
                                        url: '{$base_dir}modules/test/ajax-call.php',
                                        type: 'get',
                                        data: 'ajax=true',
                                        success: function() {
                                                console.log('success');
                                                // OTHER SUCCESS COMMAND - CHECK THE RETURN VALUE
											   document.getElementById("demo").innerHTML="Referral name returned from function checkReferral";
											
                                        }
                                });
    return false;
    }
	
	{/literal}
</script>
	
Link to comment
Share on other sites

  • 2 years later...

Hello, I have the same issue and I found this topic which helped me start with the code. I didn't finish though and I'm stuck to some problems.

 

I don't want to make a module. I just want to check if mobile phone already exists in database during registration. I use PS 1.5.4

I think that the red lines are the false ones.

 

I have ajax-call.php in root folder with code

<?php
include_once('/config/config.inc.php');
include_once('/init.php');

public function checkReferral(){
	// Code to get phone from id phone_mobile
	$num=document.getElementById("phone_mobile");
	// Get phone end

	$sql = new DbQuery();
				$sql->select('`firstname`,`lastname`');
				$sql->from('ps_address');
				$sql->where('`phone_mobile` LIKE \'%'.$num.'%\'');  
				$result = Db::getInstance()->getRow($sql);

	if (isset($result))	{
		$name=$result['firstname'].$result['lastname'];
		return $name;
	}
	else
		return 0;
}
if ($_GET['ajax']){
	echo function checkReferral();
}
?>

Then I have added this code in themes/mytheme/authentication.tpl

<input onClick="QuickLook()" type="text" class="text" name="phone_mobile" id="phone_mobile" value="{if isset($smarty.post.phone_mobile)}{$smarty.post.phone_mobile}{/if}" />

and

<script type="text/javascript">
	{literal}
	function QuickLook() {
		$.ajax({
			url: '{$base_dir}ajax-call.php',
			type: 'get',
			data: 'ajax=true',
			success: function() {
					console.log('success');
					document.getElementById("phone_mobile").innerHTML="This mobile is already used by $name. Please contact our support";
			}
		});
		return false;  //return false and continue with registration
    }
	{/literal}
</script>

Please help me fill in the blanks

Thanks in advance 

Link to comment
Share on other sites

On chrome code inspection I can see that I trigger the on event function using jquery, but I cannot send and receive the data with ajax-call.php. I always get feedback as $name...

the code is

/theme/authentication.tpl

<script type="text/javascript">
{literal}
	jQuery('#phone_mobile').on('input propertychange paste', function() {
		$.ajax({
			url: '/ajax-call.php',
			type: 'get',
			data: 'ajax=true',
			success: function() {
					console.log('$name');
					document.getElementById("phone_mobile").innerHTML="This mobile is already used by $name. Please contact our support";
			}
		});
		return false;  //return false and continue with registration
	});
{/literal}
</script>

and ajax-call.php

<?php
include_once('/config/config.inc.php');
include_once('/init.php');

function checkReferral(){
	// Code to get phone from id phone_mobile
	$num=document.getElementById("phone_mobile");

	$sql = new DbQuery();
				$sql->select('`firstname`,`lastname`');
				$sql->from('ps_address');
				$sql->where('`phone_mobile` LIKE \'%'.$num.'%\'');  
				$result = Db::getInstance()->getRow($sql);  	  //an arguement passed as checkReferrals parameter

	if (isset($result))	{
		$name=$result['firstname'].$result['lastname'];
		return $name;
	}
	else
	{ return; }
}
if ($_GET['ajax']){
	echo function checkReferral();
}

?>

I also get an error in php_errorlog and network->response

[30-Nov-2015 10:51:12 CST6CDT] PHP Fatal error:  Call to undefined function getElementById() in /home/site/public_html/ajax-call.php on line 7
Can anyone help?
Edited by joss54 (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...