Posts Tagged ‘jquery’

jquery ajax read xml text and attribute

Below is example about using Jquery ajax to read text & attribute for each node of an internal xml file.

1. Example of the XML file included is below

<dates>
	<date value="2009-11-24">Tuesday, November 24</date>
	<date value="2009-11-25">Wednesday, November 25</date>
	<date value="2009-11-26">Thursday, November 26</date>
	<date value="2009-11-27">Friday, November 27</date>
	<date value="2009-11-28">Saturday, November 28</date>
	<date value="2009-11-29">Sunday, November 29</date>
	<date value="2009-11-30">Monday, November 30</date>
</dates>

2. Javascript jquery ajax call use to read text & attribute of the xml file above

function update_date_list()
{
    var dates = document.getElementById("ddl_date");
    var i = 0;
    jQuery.ajax({
		type: "GET",
		url: "date.xml",
		dataType: "xml",
		success: function(xml)
		{
			jQuery(xml).find('date').each(function(){
                dates.options[i] = new Option(jQuery(this).text(), jQuery(this).attr('value'));
				i++;
			});
		}
	});
}

3. For each xml node, we will use to create drop down list items

<select id="ddl_date" name="ddl_date">
	<option value="2009-11-24">Tuesday, November 24</option>
	<option value="2009-11-25">Wednesday, November 25</option>
	<option value="2009-11-26">Thursday, November 26</option>
	<option value="2009-11-27">Friday, November 27</option>
	<option value="2009-11-28">Saturday, November 28</option>
	<option value="2009-11-29">Sunday, November 29</option>
	<option value="2009-11-30">Monday, November 30</option>
</select>

To see the real resutl, let’s check it out and view page source.

Bookmark and Share

php jquery ajax form with captcha and image loading

Javascript:

<script>
function change_port(port)
{
	jQuery("#port").val(port);
}
jQuery(document).ready(function(){

	if(jQuery("#port").val() == "")
		jQuery("#port").val("80");
	if(jQuery("#ip").val() == "")
		jQuery("#ip").val("<?php echo $_SERVER['REMOTE_ADDR']?>");

	jQuery("#check_port_go").click(function(){
		var dataString = "ip=" + jQuery("#ip").val() + "&&port=" + jQuery("#port").val() + "&&code=" + jQuery("#code").val();
		jQuery.ajax({
			type: "POST",
			beforeSend: function(){
				jQuery('#check_port_loading').css({ visibility: 'visible' });
			},
			url: "<?php echo get_option('siteurl');?>/check-port.php",
			data: dataString,
			dataType: "xml",
			success: function(xml)
			{
				jQuery(xml).find('CheckPort').each(function(){
					 var Port = "";
					 Port = jQuery(this).find('Port').text();
					 var Ip = "";
					 Ip = jQuery(this).find('Ip').text();
					 var Value = "";
					 Value = jQuery(this).find('Value').text();

					 switch (Value)
					 {
						case "1":
							jQuery("#check_port_result").text("Port " + Port + " is open on " + Ip);
							break;
						case "0":
							jQuery("#check_port_result").text("Port " + Port + " is closed on " + Ip);
							break;
						case "-1":
							jQuery("#check_port_result").text("Please enter a valid IP (ex: xxx.xxx.xxx.xxx , 0 <= x <= 255)");
							break;
						case "-2":
							jQuery("#check_port_result").text("Please enter a valid Port Number (ex: 80,21,25,...)");
						case "-3":
							jQuery("#check_port_result").text("The CAPTCHA text is not correct.");
							break;
					 }
				 }); //close each(
				 jQuery('#check_port_loading').css({ visibility: 'hidden' });
				 jQuery("#image").attr('src', 'securimage/securimage_show.php?sid=<?php echo md5(uniqid(time())); ?>');
				 jQuery("#code").val("");
			}
		});
		return false;
	});
});
</script>

PHP code:

<?php
	include("securimage/securimage.php");
	$img = new Securimage();
	$valid = $img->check($_POST['code']);

	function validate_ip_address($ip_addr)
	{
		if(preg_match("/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/",$ip_addr))
		{
			$parts=explode(".",$ip_addr);
			foreach($parts as $ip_parts)
			{
				if(intval($ip_parts)>255 || intval($ip_parts)<0)
					return false; //if number is not within range of 0-255
			}
			return true;
		}
		else
			return false; //if format of ip address doesn't matches
	}

	$status = 0;

	if($valid == true)
	{
		if(isset($_POST["ip"]) && $_POST["ip"] != "" && validate_ip_address($_POST["ip"]) )
		{
			$ip = $_POST["ip"];

			if(isset($_POST["port"]) && $_POST["port"] != "" && is_numeric($_POST["port"]))
			{
				$port = $_POST["port"];
			}
			else
			{
				$status = -2; // port invalid
			}
		}
		else
		{
			$status = -1; // IP invalid
		}
	}
	else
		$status = -3; // wrong code

	$timeout = "2";

	if($status == 0)
	{
		$verbinding =  @fsockopen("$ip", $port, $errno, $errstr, $timeout);

		$str = "";

		if($verbinding)
		{
			$status = 1;
		}
	}

	header("Content-type:text/xml");
	$str = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
	$str .= "<CheckPort>\r\n";
	$str .= "<Ip>" . $ip . "</Ip>\r\n";
	$str .= "<Port>" . $port . "</Port>\r\n";
	$str .= "<Value>" . $status . "</Value>\r\n";
	$str .= "</CheckPort>";

	echo $str;
?>

php outout xml format:

<?xml version="1.0" encoding="utf-8"?>
<CheckPort>
<Ip>127.0.0.1</Ip>
<Port>80</Port>
<Value>-3</Value>
</CheckPort>
Bookmark and Share