Archive for category Javascript

Parse mysql result(multiple records) info with delemiter and show with ajax.

In javascript, call ajax with proper info

var DELIM_1 = '###';
var DELIM_2 = '^^^';
function getEmployerInfo(emp_id)
{
showLayer(LOADING_MSG, 'loadLayer');
var url = '';
var pars = 'cmd=showEmployer&id=' + emp_id;
var myAjax = new Ajax.Request(
url,
{
method: 'get',
parameters: pars,
onComplete: showResponse
});
}

now in php file select multiple records and return as a string…

function getEmployerInfo($employerID)
{
$info['table'] = EMPLOYER_TBL;
$info['where'] = ‘id = ‘ . $employerID;
$result = select($info);
if(empty($result))
{
return null;
}
foreach( $result[0] as $key => $value)
{
$data[] = $key . DELIM_1 . stripslashes($value);
}
return implode(DELIM_2, $data);
}

Finally in js response you should do …

function showResponse(retVal)
{
var response = retVal.responseText.split(DELIM_2);
var frm = document.frmEmployer;
with(frm)
{
for(i = 0; i < response.length; i++)
{
var values = response[i].split(DELIM_1);
if(values[0] == ‘billing_type’)
{
for(j = 0; j < eval(values[0]).length ; j++)
{
if(eval(values[0])[j].value == values[1])
{
eval(values[0])[j].checked = true;
}
else
{
eval(values[0])[j].checked = false;
}
}
continue;
}
if($(values[0]))
{
$(values[0]).value = values[1];
}
}
}
hideLayer(’loadLayer’);
$(’addNew’).style.display = ‘inline’;
}

No Comments

how to load iframe with javascript.

Its simple. you can load iframe using javascript by the following way. for example if i want to load an url with several parameters what you need to do is ..

var iframe = document.getElementById('frame_search');
var url_string = script_url+'?cmd=userList&LastName='+LastName.value+'&FirstName='+FirstName.value+'&start_date='+start_date.value+'&end_date='+end_date.value+'&SocSecNum='+SocSecNum.value;
iframe.src = url_string;

,

No Comments

Explode in javascript

I like to use explode function a lot in php. Here is a way to use it in javascript
function explodeArray(item,delimiter)
{
tempArray=new Array(1);
var Count=0;
var tempString=new String(item);
while (tempString.indexOf(delimiter)>0)
{
tempArray[Count]=tempString.substr(0,tempString.indexOf(delimiter));
tempString=tempString.substr(tempString.indexOf(delimiter)+1,tempString.length-tempString.indexOf(delimiter)+1);
Count=Count+1
}
tempArray[Count]=tempString;
return tempArray;
}

Here is an example:
if that string is included with #### (4 hash) then do the following..

splitstring = response.split("####");
var myarray = explodeArray(splitstring,',');

,

No Comments

how to call a php function after a time interval with javascript and ajax ;)

In the template file call that javascript function which you want to call each time when page loads.

<script>
setpage();
</script>

In javascript do the following . (i’ve used prototype.js for ajax response)

function pageincrement()
{
page = page+1;
if(page>5)
{
page=1
}

setpage();
}

I’ve used prototype.js for sending ajax response

function setpage()
{
var url = ”;
setTimeout(”pageincrement()”, 5000);
if(mouseOnDiv ==1) return;
var pars =’cmd=pageload&htmlpage=’+page;
var myAjax = new Ajax.Request(url, {method: ‘get’, parameters: pars, onComplete: showResponse});
}

Now in php, grab cmd value and return which page (location of page) to load in show response. (do it urself)

function showResponse(serverResponse)
{
var response = serverResponse.responseText;

$(’quiz_list’).innerHTML = response;
}

and finally in php file echo the response according to the page number.

, ,

No Comments

Several tricks in javascript

1. Move to php file with parameters from javascript you can do it in following way

sURL = ‘test.php?cmd=remove_item&id=’ + productID+’&itemType=’+itemType;
window.location.replace(sURL);

now in php file, find the cmd in request and do whatever action you want.

2. From a dropdown box of an html page quite often i suffered to find option value in javascript (because of my coding style ofcourse :D ). Instead of giving dropdown option values its generally provides option index. Here is how to get option value in the javascript.

var ind = document.getElementById(’toptag’).selectedIndex;
var val = document.getElementById(’toptag’).options[ind].innerHTML;

3. In an application i wanted to refresh my main window from a pop up window of that main site.this can be done using

window.opener.reload();

you can also go to new location of that main window using

window.opener.location.href=’your new location’;

4. javascript innerHTML not working properly in IE ?

If its a textarea or textbox then try using innerText instead of innerHTML

5. setattribute colspan problem in Internet Explorer?

i faced a strange problem  with setattribute colspan in Internet Explorer. whenever i tried to add field dynamically using js. It worked fine in firefox but became a mess in internet explorer.

Just replace the word colspan with colSpan (capital S)     ;-)

No Comments

javascript innerHTML not working properly in IE ?

If its a textarea or textbox then try using innerText instead of innerHTML

,

2 Comments

How to deal with ajax (prototype.js)

Using prototype.js its very easy to implement Ajax (Asynchronous javascript and xml). Just call the specific function from html and then parse the xml that is returned from php.

Here is an example. in this example, we are calling a function in the onChange event of a dropdown box.


<tr>

<td>
<select name=”product_name” id=”product_name” style=”width:165px” onChange=”showprice();”>
<option value=”">Select One</option>
<?php
$searchcategory = “select product_name, product_id from product”;
$searchquery = mysql_query($searchcategory);
while($row=mysql_fetch_row($searchquery))
{
echo ‘<option>’.$row[0].’</option>’;
}
?>
</select>
</td>
<td align=right>
<label class=”label” id=lbl_product_price name=lbl_product_price> Price </label>
</td>
<td>
<input readonly type=”text” class=”text” id=”product_price” name=”product_price” value=”">
</td>

</tr>

in html head tag include this line

<script language=”JavaScript” src=”prototype.js” mce_src=”prototype.js”></script>

Now in the javacript part, i’ll use prototype.js , and here is how to use it. you can download prototype.js from internet.

function showprice()
{
checkavailability();
var url = ’sales1.php’;
var proname = $F(’product_name’);
var pars = ‘cmd=loadPrice&name=’ + proname;
//alert(pars);
var myAjax = new Ajax.Request(url, {method: ‘get’, parameters: pars, onComplete: showResponse});
}

Now in php file (sales1.php) , just return product price …

if(cmd==loadPrice) //// [note: we have passed cmd = loadPrice in javascript part ]

{

$product_name = $_REQUEST['product_name'];

$statement =”select product_price from product where product_name=$product_name”;

$result = mysql_query($statement);

$row = mysql_fetch_object($result);

return $row->product_price;

}

Now parse the return value in javascript showResponse function

function showResponse(serverResponse){

if(serverResponse.responseText){
$(’product_price’).value = parseInt(serverResponse.responseText);

// if you want to show the price in a div or in a td then you should use $(’product_price’).innerHTML in place of $(’product_price’).value //
}
}

That’s all. Enjoy :)

, ,

1 Comment

How to fix problem with dropdown appearing infront of dhtml menu in Internet explorer

Just include attachIframe function with your onClick or onmouseover function

<a style=”cursor:pointer” onClick=”searchJob(); attachIframe(’testhide’);”>Quick Job Search</a>


[note:] testhide is the div id which you need to replace by your div id.

And the function is given below just paste this in your html or js page… and enjoy.


<script type=”text/JavaScript”>
{literal}
function attachIframe(divId)
{if(navigator.appName.indexOf(’Microsoft’) != -1)
{
var div = document.getElementById(divId);
var iframe = document.createElement(’IFRAME’);

iframe.id = ‘fake-iframe’;
iframe.style.position = ‘absolute’;
iframe.style.left = getOffsetLeft(div);
iframe.style.top = getOffsetTop(div);
iframe.style.width = (div.offsetWidth) + ‘px’;
iframe.style.height = (div.offsetHeight - 10)+’px’;
iframe.style['filter'] = ‘alpha(opacity:100%)’;
iframe.style.zIndex = 4;
document.body.appendChild(iframe);
}
}
{/literal}
</script>

1 Comment