Search ( Min 3 characters )
| Tech | Title | Updated |
![]() ![]() ![]() | Using JQuery to get Multiple List items | 2012-04-04 13:51:04 |
this code will get the multiple selected list items from a listbox var list=""; var first=true; $('#tag :selected').each(function(i, selected) { if ( first==true) { first=false; } else { list+','; } list += $(selected).text(); }); If you want to get the values and not the displayed text you can use $(selected).val(); | ||
![]() | How to get Client IP Address | 2012-04-14 18:11:31 |
Using JQuery we can get the IP Address of the client using the following. This also requires a PHP file on the server that actually gets the IP address and returns the IP as JSON Data. - JavaScript - <script> $.get("getip.php", function(data) { alert("Your IP Address is : "+data); }); </script> The PHP File "getip.php" <?php header('Cache-Control: no-cache, must-revalidate'); header('Expires: Mon, 1 Jan 1990 00:00:00 GMT'); header('Content-type: application/json'); $ip="unknown"; if ( !empty($_SERVER['HTTP_CLIENT_IP'])) { $ip=$_SERVER['HTTP_CLIENT_IP']; } elseif ( !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip=$_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip=$_SERVER['REMOTE_ADDR']; } echo $ip; ?> | ||
![]() | Extracting Form Data | 2012-03-29 14:34:54 |
A nice way to get all the data from a form is to use JQuery to extract and wrap them up into a ready to use URL. What the following code does is.. 1) locate the form using its id 2) for every element in the form extract the name and value 3) builds a url with the form data as parameters 4) submits the form data to a php script var line=""; first=true; $.each($('#formid').serializeArray(), function(i, field) { if (first) { line=field.name+"="+field.value; } else { line+="&"+field.name+"="+field.value; } first=false; }); var addr = process.php?"+line ; $.get(addr, null, function(data) { // the form data has been submitted and processed. }); | ||
![]() | How to include JQuery in your site | 2012-03-29 14:35:43 |
The easiest way to add JQuery to your site is to include the latest version off the web. It may be faster depending on your server to download this and save it on your site too. The latest version that you can include on your web page is: <head> <script src="http://code.jquery.com/jquery-latest.js"></script> </head> You can now use JQuery on your site. | ||
![]() | Use JQuery to dynamically load a file off the same webserver | 2012-04-01 11:16:36 |
Loading a file off the server is very easy in JQuery. $.get("page.php", null,function(data) { $("#adiv").html(data); }); the above code will load the page.php off the server and then set the HTML in the adiv to the contents of the file. if the file is PHP it will be executed on the server as normal. The is a problem with this when developing.. When you test your page, your page will cache the results from page.php, so when you have made a change and want to retest, it is more than likely that the returned page is the cached version. If you dont want to modify you headers to force a reload, there is a very simple solution. $.get("page.php?sid="+Math.floor(Math.random()*1000000), null,function(data) { $("#adiv").html(data); }); what we have done here is to add a parameter to the end of the url, which is a random number. This means that every page you request will have to go to the server, and return the latest. | ||
![]() | JQuery onload | 2012-04-11 12:37:41 |
In JQuery, you should not use the <body onload=""> There is a new way to do this. $(document).ready(function() { //do init here }); | ||
![]() | Changing the Mouse Cursor with JQuery | 2012-05-10 07:05:19 |
to change the mouse cursor using JQuery $('#divid').css('cursor','move'); $('#divid').css('cursor','pointer'); | ||
![]() | Change or replace an image using JQuery | 2012-05-10 07:07:02 |
Make sure that your <img has an id , then use the following <img id="myimg" src="someimage.png"> <script> $('#myimg').attr("src", 'images/anotherimage.png'); </script> | ||
![]() | Posting data using JQuery | 2012-03-29 13:10:46 |
You can post JSON data to the server using the following. <script> var dat = JSON.stringify(dataToSend); $.post( "process.php", dat,function(data) { alert("Response: " + data); }); </script> However when you try to decode the above in php for example, you wont find the data as we have not given a post variable. We can slightly modify the above and include a post variable which is easy to read from PHP. var dat = JSON.stringify(dataToSend); $.post('process.php', { data: dat}, function(data){ alert(data)}, "text"); The above has been compressed into a few lines. Inside the PHP we can now get the POST variable named 'data'. You can also set the data like this. postdata={ "val" : value, "val2":value2, "val3":"a value"}; $.post('callme.php', postdata, function(data) { document.location.reload(true); }); | ||
![]() ![]() | How to post data in JQuery | 2012-04-28 17:47:26 |
To post data to the server using JQuery. For this we use the JQuery command $.post() var val1 = "value1"; var val2 = "value2"; postdata={ "val1" : value1, "val2":value2 }; $.post('senddata.php', postdata,function(data) { alert("Your data has been sent"); }); | ||


