พบกับบทความเกี่ยวกับคอมพิวเตอร์และ programming ได้ที่ http://articles.modoeye.com
Javascript development / ขอสอบถามวิธีทำpopup
รุต
รุต
Tue 28 Feb 2006 19:39:20

รบกวนขอสอบถามวิธีทำpopup  คือผมมีช่องรับข้อมูลอยู่1ช่องครับ และมีปุ่มaddอยู่ข้างๆ1ปุ่ม เพจที่ใช้คือtest1.php และเพจที่จะทำpopupคือtest2.phpในtest2.phpจะมีข้อมูลคือ

ข้อมูลที่1

ข้อมุลที่2

ข้อมูลที่3

ข้อมูลที่4

เรียงกันลงมา   พอกด  ปุ่มที่test1.phpให้ popupเล็กๆtest2.phpขึ้นมา พร้อมกับโชข้อมูลด้านในตามข้างบน  และพอผมกดข้อมูลแต่ละข้อมุล  ให้ข้อมูลนั้นเด้งลงมาที่ช่องรับในtest1.php

รบกวนขอโค้ด ไปศึกษาบ้างครับ

ขอบคุณมากครับ

Administrator
Tue 28 Feb 2006 21:23:17
ผมเข้าใจว่าเป็นการทำ popup เพื่อให้ user ทำการเลือกข้อมูล
ในหน้า test1.php นั้นเพียงเพิ่ม
<script language="javascript" type="text/javascript">
function selectValue(id, val){
    var obj = (document.all)?document.all[id]:document.getElementById(id);
    obj.value = val;
}
</script>

ฟังก์ชั่นนี้เป็นการเซ็ทค่าให้กับ textbox โดยอ้างอิงจาก id ของ object นั้นๆโดยส่งไปเป็น argument ซึ่ง textbox ที่ต้องการรับค่านั้นจึงต้องมีการเพิ่ม attribute id เพื่อไว้อ้างอิงด้วยครับ เช่น
<input type="text" name="text1" id="text1">

จากนั้นใน test2.php นั้นก็เพียงแต่เรียกการทำงานใน test1.php เท่านั้นครับเช่น
<a href="javascript:;" onclick="opener.selectValue('text1', 'ข้อมูลที่1');">ข้อมูลที่ 1</a>
<a href="javascript:;" onclick="opener.selectValue('text1', 'ข้อมูลที่2');">ข้อมูลที่ 2</a>
<a href="javascript:;" onclick="opener.selectValue('text1', 'ข้อมูลที่3');">ข้อมูลที่ 3</a>
<a href="javascript:;" onclick="opener.selectValue('text1', 'ข้อมูลที่4');">ข้อมูลที่ 4</a>

ประมาณนี้ครับ
รุต
รุต
Mon 6 Mar 2006 11:07:50

โทษนะครับ  ผมได้เอาโค้ดไปลองทำดู  มะรู้ว่าต้องรันโปรแกรมอย่างไรบ้างนะครับ

คือผมต้องการให้พอรันtest1.phpแล้วพอกดปุ่ม Submitให้test2.phpรันเป็นแบบwindows POPup ขึ้นมาอยู่ข้างๆปุ่ม  และในนั้นให้มีข้อมูล

ข้อมูลที่ 1
ข้อมูลที่ 2
ข้อมูลที่ 3
ข้อมูลที่ 4

(ซึ่งข้อมูลเหล่านี้ดึงมาจากฐานข้อมูลmysql)   และพอผมเอาเม้าไปคลิ๊กที่popupเลือก ข้อมูลใดข้อมูลนึงในนั้น  ให้ข้อมูลที่เราคลิ๊กเลือกลงไปอยู่ ในช่องรับข้อมูลในtest1.phpพร้อมกับปิดpopupอัตโนมัติ

รบกวนด้วยนะครับ

test1.php

<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=tis-620">
<script language="javascript" type="text/javascript">
function selectValue(id, val){
    var obj = (document.all)?document.all[id]:document.getElementById(id);
    obj.value = val;
}
</script>

</head>

<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<form name="form1" method="post" action="">
<input type="text" name="text1" id="text1">
  <input type="submit" name="Submit" value="Submit">
</form>
</body>

---------------------------------------

test2.php

<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<a href="javascript:;" onclick="opener.selectValue('text1', 'ข้อมูลที่1');">ข้อมูลที่ 1</a><br>
<a href="javascript:;" onclick="opener.selectValue('text1', 'ข้อมูลที่2');">ข้อมูลที่ 2</a><br>
<a href="javascript:;" onclick="opener.selectValue('text1', 'ข้อมูลที่3');">ข้อมูลที่ 3</a><br>
<a href="javascript:;" onclick="opener.selectValue('text1', 'ข้อมูลที่4');">ข้อมูลที่ 4</a>

</body>

รบกวนขอโค้ดตัวอย่างไปศึกษาบ้างนะครับ

ขอบคุณมากครับ

Administrator
Mon 6 Mar 2006 11:38:28
จากโค๊ด Javascript ใน test.php นั้นเป็นฟังก์ชั่นในการเซ็ทค่าให้กับ textbox โดยการระบุ id ของ textbox และ ค่าที่ต้องการส่งให้ครับ

ส่วนใน test2.php นั้นก็เป็นการเรียกใช้งานฟังก์ชั่นที่อยู่ใน test.php เพื่อทำการเซ็ทค่าให้กับ textbox ซึ่งถ้าคุณต้องการประยุกต์ใช้กับ MySQL นั้นก็สามารถทำได้โดย

<?
//make connection
while($row = mysql_fetch_array($res, MYSQL_ASSOC)){
    echo "<a href=\"javascript:;\" onclick=\"opener.selectValue('textbox1', ".$row["field_value"].")\">".$row["field_name"]."</a>
}
//close connection
?>

จากตัวอย่างผมสั่งให้เป็นการเซ็ทค่าให้กับ textbox ชื่อ textbox1 ดังนั้นใน test.php จะต้องมี <input type="text" id="textbox1"> อยู่ด้วยครับ
รุต
รุต
Mon 6 Mar 2006 12:02:35

รบกวนขอโค้ดที่เพจtest1.phpให้พอกดปุ่มแล้วให้เด้งopen windows popup เพจtest2.phpขึ้นมานะครับ

รบกวนด้วยนะครับ

ขอบคุณมากครับ

Administrator
Mon 6 Mar 2006 12:13:25
<input type="button" value="Open" onclick="window.open('text2.php', 'newwin', width=200, height=400')">
รุต
รุต
Mon 6 Mar 2006 15:14:34

รบกวนสอบถามนิดนึงครับ

ผมได้เอาโค้ดที่ให้ไปลองทำดูแล้วครับ ใช้ได้แล้วครับชอบมากๆ  จะรบกวนขอเพิ่มอีกนิดนึงนะครับ  คืออยากให้พอคลิ๊กข้อมูลที่ต้องการในเพจtest2.phpแล้ว ข้อมูลเด้งลงมาที่ช่องtextboxของเพจtest.phpเรียบร้อยแล้วอยากให้test2.php ปิดอัตโนมัตินะครับ

test1.php--------------------------------

<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=tis-620">
<script language="javascript" type="text/javascript">
<!--
function selectValue(id, val){
    var obj = (document.all)?document.all[id]:document.getElementById(id);
    obj.value = val;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
//-->
</script>

</head>

<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<form name="form1" method="post" action="">
<input type="text" id="textbox1">
  <input type="button" onclick="MM_openBrWindow('test2.php','','width=200,height=400')" value="Open">
</form>
</body>

test2.php----------------------------------------------------------

อยากให้พอเลือกข้อมูลใดข้อมูลหนึ่งแล้วให้มันปิดอัตโนมัตินะครับ

<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<a href="javascript:;" onclick="opener.selectValue('textbox1', 'ข้อมูลที่1');">ข้อมูลที่ 1</a><br>
<a href="javascript:;" onclick="opener.selectValue('textbox1', 'ข้อมูลที่2');">ข้อมูลที่ 2</a><br>
<a href="javascript:;" onclick="opener.selectValue('textbox1', 'ข้อมูลที่3');">ข้อมูลที่ 3</a><br>
<a href="javascript:;" onclick="opener.selectValue('textbox1', 'ข้อมูลที่4');">ข้อมูลที่ 4</a>

</body>

รบกวนขอโค้ดเพิ่ม เพื่อเอาไปเป็นตัวอย่างบ้างนะครับ

ขอบคุณมากครับ

Administrator
Mon 6 Mar 2006 20:09:38
<a href="javascript:;" onclick="opener.selectValue('textbox1', 'ข้อมูลที่1'); window.close();">ข้อมูลที่ 1</a><br>
รุต
รุต
Tue 7 Mar 2006 10:45:54

ขอบคุณมากครับสำหรับโค้ดที่ให้เพิ่มมาด้านบนนี้ จะขอเพิ่มอีกนิดนึงครับ คือผมอยากให้popupที่เกิดขึ้นให้มันเกิดตรงที่ผมกำหนดได้ไหมครับ  คืออยากให้พอกดปุ่มopenแล้วให้popupมันโชตรงข้างๆปุ่มเสมอนะครับ  คือตอนนี้พอผมกดปุ่มopen  popupมันดันไปโชทับปุ่มนะครับ  อยากให้มันโชข้างขวาติดกับปุ่มเสมอนะครับ

รบกวนขอโค้ดเวลากดปุ่มopenแล้วให้popupมันมาโชข้างปุ่มด้านขวาติดกับปุ่มเสมอ

ขอบคุณมากครับ

รุต
รุต
Tue 7 Mar 2006 12:43:02

รบกวนนิดนึงครับคือผมลองเอาtest2.phpไปเปลี่ยนใช้เป็นดึงข้อมูลจากmysqlแล้วมันขึ้นerror รบกวนดูโค้ดให้ทีครับ

Parse error: parse error in c:\appserv\www\รtest\testpopup\test2.php on line 18

test2.php

<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<?php
$connection = mysql_connect('localhost','root999','1234') or die('Can not connect');
mysql_select_db('DBpopup',$connection);
//make connection
while($row = mysql_fetch_array($res, MYSQL_ASSOC)){
    echo "<a href=\"javascript:;\" onclick=\"opener.selectValue('textbox1', ".$row["Testvalue"].")\">".$row["Testname"]."</a>
}
?>
</body>

รบกวนขอโค้ดเวลากดปุ่มopenแล้วให้popupมันมาโชข้างปุ่มด้านขวาติดกับปุ่มเสมอ

ขอบคุณมากครับ

Administrator
Tue 7 Mar 2006 16:53:12
Parse error นั้นคือไม่สามารถนำค่ามาแสดงผลได้ครับ จากโค๊ดด้านบนคุณไม่มี " (double quote) ปิดท้ายครับ
echo "<a href=\"javascript:;\" onclick=\"opener.selectValue('textbox1', ".$row["Testvalue"].")\">".$row["Testname"]."</a>";

ส่วนการให้ popup แสดงผลในตำแหน่งที่ต้องการนั้นให้เพิ่มตำแหน่งของ popup เข้าไปครับ เช่น
<input type="button" value="Open" onclick="window.open('text2.php', 'newwin', width=200, height=400, left=250, top=250')">
รุต
รุต
Tue 7 Mar 2006 17:21:25

รบกวนนิดนึงครับผมลองเอาโค้ดไปแก้ดูแล้วมันขึ้นerrorแบบนี้นะครับ

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\appserv\www\รtest\testpopup\test2.php on line 13

test2.php----รบกวนดูให้ทีครับมะรู้ว่าผิดตรงไหนนะครับ

<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<?php
$connection = mysql_connect('localhost','root999','1234') or die('Can not connect');
mysql_select_db('DBpopup',$connection);
//make connection
while($row = mysql_fetch_array($res, MYSQL_ASSOC)){
    echo "<a href=\"javascript:;\" onclick=\"opener.selectValue('textbox1', ".$row["Testvalue"].")\">".$row["Testname"]."</a>";
}
?>
</body>

Administrator
Tue 7 Mar 2006 17:26:16
คุณยังไม่ได้ทำการเขียนคำสั่ง query ข้อมูลครับ
รุต
รุต
Wed 8 Mar 2006 15:12:26

รบกวนอีกนิดนึงครับ  ผมได้เพิ่มquery แล้วครับ  แต่มันก็ยังขึ้นerrorตามด้านล่างนี้อยู่เลยครับ
รบกวนดูให้ทีครับว่าผมผิมตรงไหนครับ

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\appserv\www\รtest\testpopup\test2.php on line 15

รบกวนดูโค้ดด้านล่างนี้ทีครับ

<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<?php
$connection = mysql_connect('localhost','root999','1234') or die('Can not connect');
mysql_select_db('DBpopup',$connection);
$sql = "SELECT * FROM TBpopup";
$result = mysql_query($sql,$connection);
//make connection
while($row = mysql_fetch_array($res, MYSQL_ASSOC)){
    echo "<a href=\"javascript:;\" onclick=\"opener.selectValue('textbox1', ".$row["Testvalue"].")\">".$row["Testname"]."</a>";
}
?>
</body>

-------------------------------------------------------------------------------------

ผมใช้โค้ดด้านล่างนี้ลองติดต่อฐานข้อมูลและก็ดึงได้ตามปกติครับ

<?php
$connection = mysql_connect('localhost','root999','1234') or die('Can not connect');
mysql_select_db('DBPopup',$connection);
$sql = "SELECT * FROM TBpopup";
$result = mysql_query($sql,$connection); 
while ($records = mysql_fetch_assoc($result)) {  
print($records['Testvalue'] . ':' . $records['Testname']);
}
?>

รบกวนด้วยนะครับ

Administrator
Wed 8 Mar 2006 19:20:32
$result = mysql_query($sql,$connection);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
//....

ผิดตรงชื่อตัวแปรไม่ตรงกันครับ
รุต
รุต
Thu 9 Mar 2006 09:43:41

รบกวนนิดนึงครับโค้ดไม่ทำงานนะครับ  คือตอนนี้ผมดึงฐานข้อมูลได้แล้ว  พอผมรันtest1.phpแล้วกดปุ่มOPENมันก็pupup  test2.phpขึ้นมาในtest2.phpก็ดึงฐานข้อมูลขึ้นมาด้วย

แต่พอคลิ๊กเลือกข้อมูลในtest2.phpมันกลับไม่ยอมทำงานใดๆเลยรบกวนช่วยแก้ให้ทีครับ

test1.php-----------------------------------------------

<script language="javascript" type="text/javascript">
<!--
function selectValue(id, val){
    var obj = (document.all)?document.all[id]:document.getElementById(id);
    obj.value = val;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
//-->
</script>

</head>

<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<form name="form1" method="post" action="">
<input type="text" id="textbox1">
  <input type="button" onclick="MM_openBrWindow('test2.php','','width=200,height=80,top=104,left=203')" value="Open">
</form>
</body>

test2.php-----------------------------------------------------------

<?php
$connection = mysql_connect('localhost','root999','1234') or die('Can not connect');
mysql_select_db('DBpopup',$connection);
$sql = "SELECT * FROM TBpopup";
$result = mysql_query($sql,$connection);
//make connection
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
   // echo "<a href=\"javascript:;\" onclick=\"opener.selectValue('textbox1', ".$row["Testvalue"].")\">".$row["Testname"]."</a>","<br>";
 echo "<a href=\"javascript:;\" onclick=\"opener.selectValue('textbox1', ".$row["Testvalue"].")\"window.close();\">".$row["Testname"]."</a>","<br>";
}
?>

------------------------------------------------------------------------------------------

รบกวนด้วยนะครับ

Administrator
Thu 9 Mar 2006 11:21:23
echo "<a href=\"javascript:;\" onclick=\"opener.selectValue('textbox1', '".$row["Testvalue"]."')\">".$row["Testname"]."</a>"."<br>";
รุต
รุต
Thu 9 Mar 2006 12:02:00

ขอบคุณมากครับ

รุต
รุต
Sat 18 Mar 2006 18:31:54

รบกวนขอสอบถามเพิ่มครับ  คือพอคลิ๊กปุ่มopenที่test1.phpแล้วpopupหน้าtest2.phpก็จะเด้งขึ้นมา พร้อมกับข้อมูลด้านในพอผมกดเลือกที่ข้อมูลมันก็จะลงมาอยู่ในtextboxในtest1.php ที่นี้คือ  มีวิธีที่จะเพิ่มให้มันสามารถแดรกเม้าที่ข้อมูลแถวที่ต้องการในtest2.phpแล้วลากลงมาในtextboxของtest1.phpไหมครับ (ลากข้อมูลโดยใช้เม้าคลุมข้อมูลที่ต้องการแล้วแดรกเม้าลากลงมาในtestboxของtest1.php) (โดยให้เพิ่มความสามารถเพิ่มเติมเข้าไปนะครับ)

รบกวนขอโค้ดเพิ่มบ้างครับ

-----------------------------------------------------------------------------

test1.php

<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=tis-620">
<script language="javascript" type="text/javascript">
<!--
function selectValue(id, val){
    var obj = (document.all)?document.all[id]:document.getElementById(id);
    obj.value = val;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
//-->
</script>

</head>

<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<form name="form1" method="post" action="">
<input type="text" id="textbox1">
  <input type="button" onclick="MM_openBrWindow('test2.php','','width=200,height=80,top=104,left=203')" value="Open">
</form>
</body>

-----------------------------------------------------------------------------------------

test2.php

<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<?php
$connection = mysql_connect('localhost','root999','1234') or die('Can not connect');
mysql_select_db('DBpopup',$connection);
$sql = "SELECT * FROM TBpopup";
$result = mysql_query($sql,$connection);
//make connection
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
 echo "<a href=\"javascript:;\" onclick=\"opener.selectValue('textbox1', '".$row["Testvalue"]."');window.close();\">".$row["Testname"]."</a>","<br>";
}
?>
</body>

รบกวนด้วยนะครับ

ขอบคุณมากครับ

รุต
รุต
Wed 22 Mar 2006 08:17:51

มีวิธีที่จะเพิ่มให้มันสามารถแดรกเม้าที่ข้อมูลแถวที่ต้องการในtest2.phpแล้วลากลงมาในtextboxของtest1.phpไหมครับ (ลากข้อมูลโดยใช้เม้าคลุมข้อมูลที่ต้องการแล้วแดรกเม้าลากลงมาในtestboxของtest1.php) (โดยให้เพิ่มความสามารถเพิ่มเติมเข้าไปนะครับ)

รบกวนขอโค้ดเพิ่มบ้างครับ

Administrator
Wed 22 Mar 2006 10:44:49
ต้องขออภัยจริงๆครับ ความสามารถนี้ยังไม่เคยเห็นโค๊ดเลยครับ ถ้าจะเป็นไปได้ใน Web-based application คงมีได้เฉพาะใน Java Applet กับ Flash ครับ
Reply
Name:
E-mail:
Home | Services | Forum | Classified | Directories | Support | Contact
ATOM feed RSS 0.9 feed RSS 1.0 feed RSS 2.0 feed
Copyright © 2005 - 2007 Modoeye.com, All Rights Reserved.
Disclaimer | Privacy policy | Term of Use | Term of Services
Valid XHTML Valid CSS! PHP: Hypertext Preprocessor MySQL database Apache Powered! FreeBSD Power to serve
Modoeye Sitemap Client login