Archive for the ‘javascript’ Category


<?php
require('GoogleMapAPI.class.php');
$map = new GoogleMapAPI('map1');
$map->setAPIKey('ABQIAAAAPOHd09dXhJtbKHeZpxlh6BTdM_BGb_HRDyhTa-dAuFdRUQ382BTsqAFfGu_x3jaT_hgEvzyEiREMIg');
$map->setMapType('map');
$map->enableInfoWindow();
$map->enableDirections();
$map->setInfoWindowTrigger('mouseover');
$map->setWidth('400px');
$map->setHeight('400px');
$map->setFT('13.786987220018625,100.60581922531128','13.780276823001238,100.60309410095215');

?>

    <html ">
    <head>
    <?php $map->printHeaderJS(); ?>
    <?php $map->printMapJS(); ?>
    <!-- necessary for google maps polyline drawing in IE -->
    <style type="text/css">
      v\:* {
        behavior:url(#default#VML);
      }
    </style>
    </head>
    <body onload="onLoad()">
    <table border=0>
    <tr><td>
    <?php $map->printMap(); ?>

    </td>
	<td>
    <?php $map->printSidebar(); ?>
	  <div id="route"></div>
    </td>
	</tr>
    </table>

    </body>
    </html>
  • Comments Off

Drag & Drop with PHP & jQuery

The ability to drag and drop content on a page and have it save the order can make for a great user interface and is actually relatively easy to execute with a few lines of jQuery. You’ll need to include the jQuery user interface library which you can find here: Jquery Google API. All the files needed to get this up and running are in the download at the bottom of this post.

In this tutorial we’re going to be looking at 2 main PHP pages. the index.php page which contains the contents and functionality to perform the drag and drop and the updateList.php file which is a simple piece of code to update the listOrder column in the database using PHP and MySQL. Additionally you will need to add your database details to the connect.php file in the download package.


$(document).ready(function(){
	  function slideout(){
  setTimeout(function(){
  $("#response").slideUp("slow", function () {
      });

}, 2000);}

    $("#response").hide();
	$(function() {
	$("#list ul").sortable({ opacity: 0.8, cursor: 'move', update: function() {

			var order = $(this).sortable("serialize") + '&update=update';
			$.post("updateList.php", order, function(theResponse){
				$("#response").html(theResponse);
				$("#response").slideDown('slow');
				slideout();
			});
		}
		});
	});

});

Here’s a summary of whats going on in the code above.

1. To start with we have a simple function that will slide up the ‘response’ div which contains the message once the data is saved in the database.
2. We then move to the section that enables us to drag and drop the boxes. we then set the variable ‘order’ which contains the data to be posted via ajax to our database update script.
3. Once the ajax request has been executed we then display the response in the div #response

This is the section in the index.php page that retrieves the results from the database in the correct order.


 <div id="list">

    <div id="response"> </div>
    <ul>
      <?php
                include("connect.php");
				$query  = "SELECT id, text FROM dragdrop ORDER BY listorder ASC";
				$result = mysql_query($query);
				while($row = mysql_fetch_array($result, MYSQL_ASSOC))
				{

				$id = stripslashes($row['id']);
				$text = stripslashes($row['text']);

				?>
      <li id="arrayorder_<?php echo $id;?>"><?php echo $id;?> <?php echo $text;?>
        <div class="clear"></div>
      </li>
      <?php } ?>
    </ul>
  </div>

Below is the code from the ‘updateList.php’ file. Its pretty self explanatory, we have a MySQL update script wrapped with a foreach loop which adds the list order number in the database and echo’s the response.


<?php
include("connect.php");
$array	= $_POST['arrayorder'];
if ($_POST['update'] == "update"){
$count = 1;
	foreach ($array as $idval) {
		$query = "UPDATE dragdrop SET listorder = " . $count . " WHERE id = " . $idval;
		mysql_query($query) or die('Error, insert query failed');
		$count ++;
	}
	echo 'All saved! refresh the page to see the changes';
}
?>
  • Comments Off
<a href=“#” OnClick=DoAction(1,’Jose’); > Click </a>

Using POST

function DoAction( id, name )
{
    $.ajax({
         type: “POST”,
         url: “someurl.php”,
         data: “id=” + id + “&name=” + name,
         success: function(msg){
                     alert( “Data Saved: “ + msg );
                  }
    });
}

Using GET

function DoAction( id, name )
{
     $.ajax({
          type: “GET”,
          url: “someurl.php?id=” + id + “&name=” + name,
          success: function(msg){
                     alert( “Data Saved: “ + msg );
                   }
     });
}

 
  • Comments Off

jquery simple

#testDiv {
	background-color:#3399CC;
	font: 12px Tahoma, Arial, sans-serif;
	color:#FFFFFF;
	width: 150px;
	height: 100px;
	text-align:center;
}
.testClass {
	background-color:#FF0000;
	font: 12px Tahoma, Arial, sans-serif;
	color:#FFFFFF;
	width: 150px;
	height: 100px;
	text-align:center;
}

* #testDiv และ .testClass ใน css เอาไว้แค่กำหนดหน้าตาเฉยๆ ไม่มีผลกับ jquery

<script src=“jquery.js” type=“text/javascript”><!–mce:0–></script>

เป็นการเรียกโหลด jquery.js เข้ามา

<a onclick=“$(’#testDiv’).show();” href=“javascript:void(0);”>Show</a>
<a onclick=“$(’#testDiv’).hide();” href=“javascript:void(0);”>Hide</a>

ดสำคัญมันอยู่ตรงนี้ onclick=”$(’#testDiv’).show();” คำสั่งนี้คือการ เลือกที่จะทำงานกับ
div (ขึ้นต้นด้วย #) ที่ชื่อว่า #testDiv นั่นเอง

 
<div id=“testDiv”>ข้อความ .. .. .. ข้อความ</div>
<a onclick=“$(’.testClass’).show();” href=“javascript:void(0);”>Show</a>
<a onclick=“$(’.testClass’).hide();” href=“javascript:void(0);”>Hide</a>

ในทำนองเดียวกัน onclick=”$(’.testClass’).show();” คำสั่งนี้คือการ เลือกที่จะทำงานกับ
class (ขึ้นต้นด้วย .) ที่ชื่อว่า .testClass นั่นเอง

 
<div class=“testClass”>ข้อความ .. .. .. ข้อความ</div>
  • Comments Off

อย่างที่ทราบกันครับว่าการส่ง-รับค่าแบบ AJAX นั้นสามารถส่งได้ 2 แบบ คือแบบ POST และ GET ซึ่งก้อเหมือนเราส่งค่าผ่าน HTML Form ธรรมดาทั่วไปนั้นเอง และ jQuery ก้อมีฟั่งชั่นรองรับที่ง่ายแสนง่าย ให้พวกเราใช้งานอยู่แล้วครับเรามาเริ่มกันเลยครับ
การส่ง-รับค่าแบบ POST
แบบที่ 1 (ไม่มีการส่งค่า Parameter)

  1. $.post(“ชื่อไฟล์ที่ต้องการดึงข้อมูล”, function(data){
  2. //คำสั่ง
  3. });

ตัวอย่าง JavaScript ในไฟล์ test.html

  1. $(document).ready(function(){ //เริ่มทำงานเมื่อโหลดเพจเสร็จ
  2. $.post(“test.php”, function(data){ //data จะเก็บค่าที่ return กลับมา
  3. alert(“Data is ”+data); //แสดงข้อความใน Alert box
  4. });
  5. });

โค๊ดในไฟล์ test.php

  1. echo ‘Hi, AJAX’; //return คำว่า ”Hi, AJAX”

ผลที่ได้คือ
0011.jpg
แบบที่ 2 (มีการส่งค่า Parameter)

  1. $.post(“ชื่อไฟล์ที่ต้องการดึงข้อมูล”, [JSON], function(data){
  2. //คำสั่ง
  3. });

ตัวอย่าง JavaScript ในไฟล์ test2.html

  1. $(document).ready(function(){ //เริ่มทำงานเมื่อโหลดเพจเสร็จ
  2. $.post(“test2.php”,{fname: “ทดสอบ”, lname: “นอนสบาย”}, function(data){ //มีการการส่งค่า fname และ lname ไปด้วย
  3. alert(“Data is ”+data); //แสดงข้อความใน Alert box
  4. });
  5. });

โค๊ดในไฟล์ test2.php

  1. $fname = $_POST["fname"]; // รับค่าแบบ POST
  2. $lname = $_POST["lname"];
  3. echo “Hi, {$fname} {$lanme}”; //return ค่าที่รับมาแบบ POST

ผลที่ได้คือ
0021.jpg
การส่ง-รับค่าแบบ GET
สำหรับการส่งแบบ GET นั้นสามารถทดสอบได้ด้วยโค๊ดเดียวกัน เพียงแต่เปลี่ยนจากฟังก์ชั่น $.pos() เป็น $.get() และเปลี่ยนการรับข้อมูลในไฟล์ PHP จาก $_POST เป็น $_GET

  • Comments Off

jQuery Object Accessors

jQuery Object Accessors

สำหรับ jQuery ในส่วน Object Accessor หรือเรียกว่าเป็นเรื่องของ function ของคำสั่งที่ jQuery เตรียมไว้ให้กับเราซึ่งมีอยู่ ด้วยกันดังนี้

Core / each

ส่วนนี้คือการทำงานในลักษณะ loop ซึ่งทำให้เราสามารถที่จะทำงาน วนข้อมูลง่ายๆ จนกว่าจะครบตามจำนวน Length ของ selector ที่เลือก ซึ่งจะขออธิบายในตัวอย่างนี้นะครับ

จากตัวอย่างนี้ จะเป็นการวน loop ตามจำนวน ที่ selector คือ $(“div”) ทั้งหมด จะสังเกตุเห็นว่า จะมี alert บอกว่ากำลังทำงานกับ tag div ตัวไหนอยู่ และ parameter i ก็จะเป็นตัวบอก รอบที่อยู่ใน loop เริ่มจาก 0

<script language=”javascript”>

$(document).ready(function(){

$( document.body ).click( function () {

$(”div”).each( function(i) {

alert( (i+1) + “. Working in Tag =” + this.innerHTML);

if (this.style.color !=”blue”) {

this.style.color=”blue”;

}else{

this.style.color=””;

}

} );

} );

});

</script>

<style type=”text/css”>

div { color:red; text-align:center; cursor:pointer;

font-weight:bolder; width:300px; }

</style>

มี Tag div อยู่ 3 ตัวซึ่งเราจะมองว่า Tag div แต่ละตัวคือ element แต่ละ element

<div>Click here</div>

<div>to iterate through</div>

<div>these divs.</div>

Core/Length

การใช้ method length เพื่อนับจำนวน array ของ Selector (method length เป็นของ javascript อยู่แล้ว) ซึ่งเราสามารถนำมาประยุกต์ใช้กับงานได้ ยกตัวอย่างเช่น ตัวอย่างการเพิ่มแถวของ Table และ ลบแถวของ Table อย่างง่ายๆ ซึ่งเราจะใช้ manipulation append เพื่อ create element แถวลงไปใน table โดยต่อท้ายแถวสุดท้ายลงไป

<script language=”javascript”>

$(document).ready(function(){

// ปุ่ม Insert

$(”#btnIn”).click(function () {

var txtnum=$(”.trcolor”).length+1;

$(”#tblData”).append($(”<tr class=\”trcolor\”> ” +

“           <td>”+txtnum+”</td>” +

“           <td><input type=\”text\”></td>”+

“           <td><input type=\”text\”></td>”+

“           <td><input type=\”text\”></td>”+

” </tr>”));

getMsg();

});

// ปุ่ม Delete

$(”#btnDel”).click(function (){

$(”.trcolor:last”).remove();

getMsg();

});

// function บอกจำนวนแถว โดยใช้ method length

function getMsg(){

var n=$(”.trcolor”).length;

$(”#lblMsg”).text(”There are ” + n + ” Rows.”);

}

});

</script>

<style type=”text/css”>

.trcolor { background:#F2F2F2; }

</style>

<button id=”btnIn”>RUN</button> | <button id=”btnDel”>Del</button>

<div id=”lblMsg”></div>

<table id=”tblData” width=”600″ border=”0″ cellpadding=”1″ cellspacing=”1″ bgcolor=”#CCCCCC”>

<tr>

<td width=”12%”><strong>Number</strong></td>

<td width=”24%”><strong> Name</strong></td>

<td width=”22%”><strong>Surname</strong></td>

<td width=”42%”><strong>Tel</strong></td>

</tr>

</table>

Core/eq(position)

Argument position คือการใส่ Number Index ลงไป

การใช้ eq สำหรับระบุ ไปยัง Index ของ element แบบเฉพาะเจาะจงลงไปในตัวใดตัวหนึ่ง จะอธิบายด้วยตัวอย่างนี้นะครับ

<script language=”javascript”>

$(document).ready(function(){

/* initial function */

$(”#btnRun”).click(function (){

$(”div”).eq(2).css(”background”,”#CCFFFF”);

});

});

</script>

จากตัวอย่างนี้จะเป็นการ เลือก selector ที่ element Div และกำหนด eq ที่ 2 คือตำแหน่ง index ที่ 2 จากข้อมูลชุดด้านล่างนี้ เพื่อทำการ ใส่สี่ background

<button id=”btnRun”>RUN</button>

<div><a href=”#”>Data 01</a> < — index 0 , first element </div>

<div><a href=”#”>Data 02</a> < — index 1</div>

<div><a href=”#”>Data 03</a> < — index 2</div>

<div><a href=”#”>Data 04</a> < — index 3          , last element</div>

Core/get

การใช้ get เป็นการรับ element จาก selector ซึ่งค่าที่ได้จะเป็นลักษณะ array

<script language=”javascript”>

$(document).ready(function(){

$(”#btnRun”).click(function (){

var objDiv= $(”div”).get(); // ใช้ get ใส่ตัวแปร objDiv

var txt=[];            // ประกาศตัวแปร Array

for (var i =0;i<objDiv.length;i++){

txt.push(objDiv[i].innerHTML);

// push คือ method ของ Javascript เพื่อ ใส่ข้อมูลใน ตัวแปร Array

}

$(”span”).text(txt.join(” , “));

// join คือ method ของ Javascript เพื่อ เชื่อมสมาชิกแต่ละตัว ใน Array ซึ่ง Argument ของ join คือตัวคั่นระหว่าง สมาชิกแต่ละตัว

});

});

</script>

<button id=”btnRun”>RUN</button><br>

Data Divs Join : <span style=”color:#FF0000″></span>

<hr>

<div>One</div>

<div>Two</div>

<div>Three</div>

<div>Four</div>

Core/get (index)

เป็นการรับ element จาก selector แต่ระบุ index เข้าไปเพื่อชี้เฉพาะเจาะจง ลงไป ว่าจะเลือกตัวไหน ยกตัวอย่าง จาก ตัวอย่างด้านบน

$(”div”).get(2).innerHTML หากกำหนด get(2) จะได้ค่า กลับมาเป็น Three เนื่องจากจะไปจับ elements Index ที่ 2 นับจาก 0 ซึ่งก็คือ แทก <div> ที่ข้อความว่า Three

Core/index (subject)

Subject คือ element ที่จะระบุใน Index

เรื่อง index จะเป็นเรื่องการหาค่า index ใน element ว่าอยู่ตำแหน่งที่เท่าไหร่ ซึ่ง subject จะเป็นตัวระบุ current ของ index ที่ถูกกระทำ หาก ยังไม่ได้ถูกกระทำ จะ คืนค่า -1 กลับมา

จากตัวอย่างนี้ เมื่อ click ไปที่ Tag div แต่ละตัว จะแสดง alert current Index ของตัวนั้นออกมา

<script language=”javascript”>

$(document).ready(function(){

$(”div”).click(function (){

var txt=”Index is : ” + $(”div”).index(this);

alert(txt);

});         });

</script>

<div>One</div>

<div>Two</div>

<div>Three</div>

<div>Four</div>

  • Comments Off

Time

Mp3

Msn status

  • manon2029@hotmail.com is

Chat with Meeh

Donate

    If you find an article useful, then please make a donation.

หมวดหมู่

UserOnline

Counter

  • Visitors today: 4
  • Visitors yesterday: 46
  • Visitors per day: 3,560
  • Max. visitors per day: 579
  • Total page views: 600,816
  • Page views of this page: 3,560
  • Currently online: 2
  • Max. online: 192
  • Total visitors: 129,239
  • counterStatistics