Saturday, November 13, 2010

Image Slider

<div id="myIMG" style="display: none;"></div>

<script>
$(function(){

    total_img = 3;

    img = Array();
    img[0] = 'http://ip.skyzone.mn/mb_images/header_computer.jpg';
        img[1] = 'http://ip.skyzone.mn/mb_images/header_mobile.jpg';
        img[2] = 'http://ip.skyzone.mn/mb_images/header_voip.jpg';

    lnk = Array();
    lnk[0] = 'link1';
        lnk[1] = 'link2';
        lnk[2] = 'link3';

    showIMG(0,'#myIMG');

});

    function showIMG(idx, target_div){
        if(idx == total_img){
            idx = 0;
        }
        $(target_div).css('display','none');
        $(target_div).html('<a href="'+lnk[idx]+'" target="_blank"><img src="'+img[idx]+'" border="0" /></a>');
        $(target_div).fadeIn(3000);
        idx++;
        //alert(idx);
        setTimeout("showIMG("+idx+",'"+target_div+"');",5000);
    }
</script>

Multiple Checkbox Validation

How to validate the checkbox using Jquery?

In the HTML form have multiple chekcbox in same using html array element.
That time we will use three types of checkbox validation for checked or not.
Checkbox validation has three types.
  • Checkbox validation using Id
  • Checkbox validation using name
  • Checkbox validation using div tag
Example

Checkbox validation using Id

<form>
    <div id="checkboxId">
    <input type="checkbox" name="chkBox[]" id="chkBox[]">
    <input type="checkbox" name="chkBox[]" id="chkBox[]">
    <input type="checkbox" name="chkBox[]" id="chkBox[]">
    </div>
    <input type="button" name="Btnaction" value="submit" 
onclick="return validateId();">
</form>
</div>
<script language="javascript" src="jquery.js" type="text/javascript">
</script>
<script language="javascript">
function validateId()
{
 var selector_checked = $("input[@id=chkBox]:checked").length;
    alert(selector_checked);
    if (selector_checked == 0)
    {
        return false;
    }
    else if (selector_checked == 0)
    {
        return false;
    }
    else
    {
        return true;
    }
}
</script>

Checkbox validation using Name

function validateName()
{
    var selector_checked = $("input[@name=chkBox]:checked").length;
    alert(selector_checked);
    if (selector_checked == 0)
    {
        return false;
    }
    else if (selector_checked == 0)
    {
        return false;
    }
    else
    {
        return true;
    }
}

Checkbox validation using Div tag

function validateDiv()
{
    if($("#checkboxId input[type='checkbox']:checked").length > 0)
    {
     return true;
    }
}

What is Jquery?

What is Jquery?
Jquery is one of the javascript library file. We can easy to integrate the website. Jquery is client side scripting language.
How to install the Jquery in web page?
Step 1:
First download the Jquery library file.
See the current release
Download Latest Jquery Version
Step 2:
Put the downloaded file in where you need in your website

For Example :

<html>
    <head>
         <title>Example page for Jquery</title>
         <script language="javascript" type="text/javascript" src="jquery-1.3.2.min.js"></script>
    </head>
</html>

How to select all and unselect all checkbox using Jquery

<form>
    <div id="checkboxId">\n
    <input type="checkbox" name="chkBox[]" id="chkBox[]">
    <input type="checkbox" name="chkBox[]" id="chkBox[]">
    <input type="checkbox" name="chkBox[]" id="chkBox[]">
    </div>\n
    <input type="checkbox" name="selectAll" id="selectAll" 
onclick="selectAllCheckbox('checkboxId', this);">
</form>
</div>
<script language="javascript" src="jquery.js" 
type="text/javascript">
</script>
<script language="javascript">
function selectAllCheckbox(divname, obj)
{  

 if (obj.checked)
 {\n
  $"#" + divname + " input[type='checkbox']").attr('checked', true);
 }
else
 {
 $("#" + divname + " input[type='checkbox']").attr('checked', false);
}
}
</script>

Thursday, November 4, 2010

Retrieving an RSS Feed in PHP

RSS Feeds are widely used throughout the internet today and on all kinds of websites. From websites like Digg to most blogs. If you want to display an RSS feed on your website its probably easier than you think.
For this example I'm going to show you how to display the RSS feed for The Tutorial Blog using PHP.

The Code

The first two lines of code that we write are going to be what retrieve the RSS feed.

$file = file_get_contents("http://www.thetutorialblog.com/feed/");
$xml = new SimpleXMLElement($file);

Now the next thing we do is use the foreach function so for each item found do something with it
foreach($xml->channel->item as $feed){
echo $feed->title.'<br>';
}

What are code will do is simply echo the titles of all the items found within the feeds. If you also want to include a link to the feed you can do this by printing to screen $feed->link. All the code together should look something like this.

<?php $file = file_get_contents("http://www.thetutorialblog.com/feed/");
$xml = new SimpleXMLElement($file);
foreach($xml->channel->item as $feed){
echo $feed->title.'<br>';
}
?>
Thanks for taking the time to read this tutorial. I hope you have learned something.

PHP CONTACT FORM

Every website needs a way of contacting the people behind it and putting an email on a page is not such a good idea; because bots can easily pick up this email address and send spam to it. This is where a contact form comes in very useful because people can send you messages but do not get your email address.
For a contact form the first thing that we need to do is to create a form using HTML so that they can input their information and message that they are going to send.
<form action="send_message.php" method="POST">
 Name: <input type="text" name="name"> <br />
 Email: <input type="text" name="email"> <br />
 Message: <textarea name="message"></textarea> <br />
 <input type="submit" name="send_message" value="Send!">
</form>
When the press send it will then go to a page called send_message.php. This page will grab the information that they input and send it to an email address using the mail() function in PHP.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
 
$to = 'your_address@your_email.com';
$subject = "New message from $name";
 
mail($to, $subject, $message, "From: $email");
echo "Message sent";
?>
We need to check that the sender of the message is human so we will add a simple math question to the bottom of our form.
<?php
 
 $num_one = rand() % 10;
 $num_two = rand() & 10;
 $final_num = $num_one + $num_two;
 $_SESSION['answer'] = $final_num;
 echo $num_one . ' + ' . $num_two . ' = ';
 ?>
    <input type="text" name="answer" /> <br />
Don't forget to add
<?php
session_start();
?>
To the top of the page or we will get errors.
Now we check to see if they have entered information into every field and that the answer to the math question is correct, if it is the message will be sent otherwise an error will be returned.
All the code for the form is:
<?php
session_start();
?>
<form action="send_message.php" method="POST">
 Name: <input type="text" name="name"> <br />
 Email: <input type="text" name="email"> <br />
 Message: <textarea name="message"></textarea> <br />
    <?php
 
 $num_one = rand() % 10;
 $num_two = rand() & 10;
 $final_num = $num_one + $num_two;
 $_SESSION['answer'] = $final_num;
 echo $num_one . ' + ' . $num_two . ' = ';
 ?>
    <input type="text" name="answer" /> <br />
 <input type="submit" name="send_message" value="Send!">
</form>
All the code for error checking and sending the message is:
<?php
session_start();
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$user_answer = $_POST['answer'];
$real_answer = $_SESSION['answer'];
 
$to = 'your_address@your_email.com';
$subject = "New message from $name";
 
if(empty($name) OR empty($email) OR empty($message)) {
 echo "Fill in all fields.";
} elseif($user_answer != $real_answer) {
 echo "Math question was incorrect, please try again";
} else {
 mail($to, $subject, $message, "From: $email");
 echo "Message sent";
}
?>

5 PHP Frameworks you may not know about

Segull

Seagull 5 PHP Frameworks you may not know about
Supports:

* PHP4
* PHP5
* Multiple Database's
* ORM
* DB Objects
* Templates
* Caching
* Validation
* Ajax
* Auth Module
* Modules

PHP on TRAX

phpontrax 5 PHP Frameworks you may not know about
Supports:

* PHP5
* Multiple Database's
* ORM
* DB Objects
* Validation
* Ajax
* Modules

Yii

Yii 5 PHP Frameworks you may not know about
Supports:

* PHP5
* Multiple Database's
* ORM
* DB Objects
* Templates
* Caching
* Validation
* Ajax
* Auth Module
* Modules

Akelos

Akelos 5 PHP Frameworks you may not know about
Supports:

* PHP4
* PHP5
* Multiple Database's
* ORM
* DB Objects
* Templates
* Caching
* Validation
* Ajax
* Auth Module
* Modules

Prado

PRADO 5 PHP Frameworks you may not know about
Supports:

* PHP5
* Multiple Database's
* ORM
* DB Objects
* Templates
* Caching
* Validation
* Ajax
* Auth Module
* Modules