Page 1 of 1
Calling a function through a link.
Posted: Sat Jan 22, 2011 12:07 pm
by phazorRise
I want to check availablity of username for registering new user. When user clicks on Check Availablility link it should call the function check_avail(). The function check_avail() is in same page. I don't want to redirect on same page or any other page. Cause in both cases user'll lost the values filled.
Is any way to achieve this through a link or something else? Thank You!
Re: Calling a function through a link.
Posted: Sat Jan 22, 2011 12:52 pm
by cpetercarter
A neat way to do this would be an AJAX call (ie to use Javascript on the browser to send the proposed name to the server; and then display "Name available" or "Name not available" depending on the server response.) The jquery Javascript library contains useful functions for doing this.
The only real alternative is for the user to submit the whole form as POST data to the server. You do not need to lose all the user's data if the server finds that the user name is not available - you can resend the page with the (valid) data already entered in the input boxes etc, and leave only the user name field blank.
Re: Calling a function through a link.
Posted: Sun Jan 23, 2011 8:17 am
by phazorRise
but Javascript can be disabled by users.
Re: Calling a function through a link.
Posted: Sun Jan 23, 2011 8:24 am
by John Cartwright
phazorRise wrote:but Javascript can be disabled by users.
cpetercarter already outlined your best option (javascript). Sure it can be disabled, but 99% I know work off the assumption javascript is enabled, and will provide a little notice if javascript is disabled they will not be able to have the full site functionality. Regardless, you should still not rely on JS, and as a fall back have your form submit normally with all your data to repopulate and display the user availability.
Re: Calling a function through a link.
Posted: Sun Jan 23, 2011 10:14 am
by phazorRise
John Cartwright wrote:phazorRise wrote:but Javascript can be disabled by users.
cpetercarter already outlined your best option (javascript). Sure it can be disabled, but 99% I know work off the assumption javascript is enabled, and will provide a little notice if javascript is disabled they will not be able to have the full site functionality. Regardless, you should still not rely on JS, and as a fall back have your form submit normally with all your data to repopulate and display the user availability.
thank you. but how can i check availability of username by accessing MySQL db through Javascript? and is it eefficient! also if javascript is disabled then script might accept duplicate username or again i have to write separate script for checking same.
Re: Calling a function through a link.
Posted: Mon Jan 24, 2011 9:59 am
by sockpuppet
phazorRise wrote:John Cartwright wrote:phazorRise wrote:but Javascript can be disabled by users.
cpetercarter already outlined your best option (javascript). Sure it can be disabled, but 99% I know work off the assumption javascript is enabled, and will provide a little notice if javascript is disabled they will not be able to have the full site functionality. Regardless, you should still not rely on JS, and as a fall back have your form submit normally with all your data to repopulate and display the user availability.
thank you. but how can i check availability of username by accessing MySQL db through Javascript? and is it eefficient! also if javascript is disabled then script might accept duplicate username or again i have to write separate script for checking same.
Have the JS call a page. e.g. check_name.php?name=dave this then displays if the name is available. The php page checks the mysql, not the javascript. That just loads the page in the back ground.
When you post the form you'll also need to recheck the name to make sure that it's free, if not error back to the form.
Re: Calling a function through a link.
Posted: Mon Jan 24, 2011 4:56 pm
by adc123
its a little dirty but you can do this
in the value of you text fields add <? echo"$username"; ?> then at the top of the page $username=$_REQUEST['username'];
now your form will refresh with submitted values leaving you free to direct the link to another page aviliable.php
do the sql count call or whatever function you want in the separate page.
and end it with
header redirect
header('Location:
http://www.example.com/?username=$usern ... ble=$count'); //you can add more variables as a normal get.
this is a dirty solution and puts data into the url but if you dont want to use ajax or js its a solution
Re: Calling a function through a link.
Posted: Tue Jan 25, 2011 6:09 am
by phazorRise
@socketpuppet :
so you're telling me paxx the querystring on check_name.php, right?
But in that case also page will be redirected. So other fields filled by user will be lost as i have to again redirect the control to previous page and it'll be refreshed. I don't want to refresh entire form.
Instead i used Sajax to call a php function that'll check availability by accessimg DB. Call will be made through javascript by passing the text field value in anchor tag with clcik event showing link as 'Check Availability'. i'm done with writing the code ad it works well.
Re: Calling a function through a link.
Posted: Tue Jan 25, 2011 6:16 am
by phazorRise
adc123 wrote:its a little dirty but you can do this
in the value of you text fields add <? echo"$username"; ?> then at the top of the page $username=$_REQUEST['username'];
now your form will refresh with submitted values leaving you free to direct the link to another page aviliable.php
do the sql count call or whatever function you want in the separate page.
and end it with
header redirect
header('Location:
http://www.example.com/?username=$usern ... ble=$count'); //you can add more variables as a normal get.
this is a dirty solution and puts data into the url but if you dont want to use ajax or js its a solution
@socketpuppet and @adc123: thnx for ur cocerns. I'm done with code. See above post reply and let me know.
Re: Calling a function through a link.
Posted: Tue Jan 25, 2011 6:44 am
by raj23
Hi,
You can achieve by external ajax statement the only thing you have to do just link the file
cut and copy this code
Step1:
*
* ajax_click.js
* chriscook.me
*/
function loadurl(dest) {
try {
// Moz supports XMLHttpRequest. IE uses ActiveX.
// browser detction is bad. object detection works for any browser
xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
// browser doesn't support ajax. handle however you want
}
// the xmlhttp object triggers an event everytime the status changes
// triggered() function handles the events
xmlhttp.onreadystatechange = triggered;
// open takes in the HTTP method and url.
xmlhttp.open("GET", dest);
// send the request. if this is a POST request we would have
// sent post variables: send("name=aleem gender=male)
// Moz is fine with just send(); but
// IE expects a value here, hence we do send(null);
xmlhttp.send("null");
}
function triggered() {
if ((xmlhttp.readyState == 4) (xmlhttp.status == 200)) {
document.getElementById("ajaxlink").innerHTML = xmlhttp.responseText;
}
}
Step 2:
Add this code in your html file
<script src="ajax_link.js" type="text/javascript"></script>
Step 3:
place this code in php file
<div id="ajaxlink" onclick="loadurl('ajax_function.php')">Check Availability</div>
BY
Drupal web development
Re: Calling a function through a link.
Posted: Tue Jan 25, 2011 6:49 am
by sockpuppet
$_REQUEST[] shouldn't be used. $_GET or $_POST. Also you shouldn't touch any $_ variable without cleaning it first so you can be sure its clean.
adc123 wrote:its a little dirty but you can do this
in the value of you text fields add <? echo"$username"; ?> then at the top of the page $username=$_REQUEST['username'];
now your form will refresh with submitted values leaving you free to direct the link to another page aviliable.php
do the sql count call or whatever function you want in the separate page.
and end it with
header redirect
header('Location:
http://www.example.com/?username=$usern ... ble=$count'); //you can add more variables as a normal get.
this is a dirty solution and puts data into the url but if you dont want to use ajax or js its a solution
Re: Calling a function through a link.
Posted: Tue Jan 25, 2011 7:40 am
by phazorRise
raj23 wrote:Hi,
You can achieve by external ajax statement the only thing you have to do just link the file
cut and copy this code
Step1:
*
* ajax_click.js
* chriscook.me
*/
function loadurl(dest) {
try {
// Moz supports XMLHttpRequest. IE uses ActiveX.
// browser detction is bad. object detection works for any browser
xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
// browser doesn't support ajax. handle however you want
}
// the xmlhttp object triggers an event everytime the status changes
// triggered() function handles the events
xmlhttp.onreadystatechange = triggered;
// open takes in the HTTP method and url.
xmlhttp.open("GET", dest);
// send the request. if this is a POST request we would have
// sent post variables: send("name=aleem gender=male)
// Moz is fine with just send(); but
// IE expects a value here, hence we do send(null);
xmlhttp.send("null");
}
function triggered() {
if ((xmlhttp.readyState == 4) (xmlhttp.status == 200)) {
document.getElementById("ajaxlink").innerHTML = xmlhttp.responseText;
}
}
Step 2:
Add this code in your html file
<script src="ajax_link.js" type="text/javascript"></script>
Step 3:
place this code in php file
<div id="ajaxlink" onclick="loadurl('ajax_function.php')">Check Availability</div>
BY
Drupal web development
I already got the solution. I solved it through Sajax. Anyways, thank you for concern. And hey, have you tried Sajax before? Tell me if you've found any bug or drawback of using it.