เอาข้อมูลไปใช้ยังงัยครับ ช่วยที

test.php
กรณีที่ผมแสดงข้อความออกมาเป็น echo"SELECT * FROM project_db where pm='3'";

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

showtext.php

  <html>
    <head>
new.Request("test.php?id=5",
{
method:
"GET",
onComplete:showdata
}
);
}
function showdata
(result){.getElementById("showdata").innerHTML=result.responseText;
}
  
</head>

    <body>

document
Ajax

      <table>
        <tr>
          <td><span id="showdata"></span>
        </tr>
        <tr>
          <?php   ?>

     
</table>
     </body>
  </html>

สรุปคือ ในไฟล์ showtext.php ใน tag เทเบิลก็จะโชว์คำว่า SELECT * FROM project_db where pm='3' ใช่เปล่าครับ
   แต่ที่ผมต้องการคือ ต้องการที่จะนำ SELECT * FROM project_db where pm='3' ไปใช้ต่อครับ คือตอนนี้มันแสดงผลได้อย่างเดียว
      คือต้องการไปใช้กับ tag <?php ?> คือจะทำการ select ข้อมูลโดยใช้ค่าที่ได้มาจาก ไฟล์ test.php อะครับ มันต้องแปลง หรือดึงข้อมูล
        ยังงัยหรอครับ

04 พ.ย. 2551 10 3,748

ท่านพี่ไม่ลองเก็บไว้ในตัวแปร
แล้วค่อยเรียกใช้มันอีกทีดูล่ะฮ่ะ
#1

หรือลองแกะโค้ดจาก phpmyadmin ดูก็ได้
#2

เก็บยังงัยหรอครับ ช่วยบอกทีซิครับ ขอตัวอย่างด้วยก็ดีนะ

#3

คล้ายๆ อะไรแบบนี้กระมั๊ง ลองแงะดูนะเผื่อเจอ "ทางตัน" เอ่ย! "ทางออกสิ"

<!-- Query box, sql file loader and bookmark support -->
<a name="querybox"></a>
<form method="post" action="read_dump.php"<?php if ($is_upload) echo ' enctype="multipart/form-data"'; echo "
"; ?>
    onsubmit="return checkSqlQuery(this)">
    <input type="hidden" name="is_js_confirmed" value="0" />
    <?php //echo PMA_generate_common_hidden_inputs($db); ?>
    <input type="hidden" name="pos" value="0" />
    <input type="hidden" name="goto" value="db_details.php" />
    <input type="hidden" name="zero_rows" value="<?php echo htmlspecialchars($strSuccess); ?>" />
    <input type="hidden" name="prev_sql_query" value="<?php echo ((!empty($query_to_display)) ? htmlspecialchars($query_to_display) : ''); ?>" />
    <?php //echo sprintf($strRunSQLQuery, htmlspecialchars($db)) . ' ' . PMA_showMySQLDocu('Reference', 'SELECT'); ?>&nbsp;:<br />
    <div style="margin-bottom: 5px">
<textarea name="sql_query" cols="<?php echo $cfg['TextareaCols'] * 2; ?>" rows="<?php echo $cfg['TextareaRows']; ?>" wrap="virtual" dir="<?php echo $text_dir; ?>"<?php echo $auto_sel; ?>>
<?php
if (!empty($query_to_display)) {
    echo htmlspecialchars($query_to_display);
} else {
    echo htmlspecialchars(str_replace('%d', PMA_backquote($db), $cfg['DefaultQueryDatabase']));
}
?></textarea><br />
        <input type="checkbox" name="show_query" value="1" id="checkbox_show_query" checked="checked" />&nbsp;
        <label for="checkbox_show_query"><?php echo $strShowThisQuery; ?></label><br />
#4

ไม่ได้ถนัด MySQL ซะด้วย

วอนจอมยุทธในใต้หล้าช่วยด้วยล่ะกัน
#5

คือจริงๆแล้วมันไม่เกี่ยวกับ mysql หรอกครับ ปัญหามันมีอยู่ว่าผมจะ เอาข้อมูลที่ได้จาก result.responseText ไปใช้ยังงัยเท่านั้นเองครับ คือจะเอาไปกำหนดตัวแปร ใน php  แต่ทำไม่เป็น

#6

อยากนำ
SELECT * FROM project_db where pm='3'
มาแสดงใน
<span id="showdata"></span>
เป็นแบบนี้
<span id="showdata">SELECT * FROM project_db where pm='3'</span>
แล้วนี้
SELECT * FROM project_db where pm='3'
ไปใช้ต่อ
<table>
  <tr>
    <td><span id="showdata"></span>
  </tr>
  <tr>
          <?php ในนี้ ?>
  </tr>

</table>
ถูกต้องมั้ย

 

งั้นมาดูวิธีแก้กัน

ใน function showdata();
เพิ่มโค้ด var ตัวแปร = respMessage; เข้าไป
เป็น
function showdata(result){.getElementById("showdata").innerHTML=result.responseText;
var ตัวแปร = respMessage;
}

 

เวลาเรียกใช้ก็

<?php
$con = mysql_connect("host","user","password");
if (!$con){
  die('ติดต่อฐานข้อมูลไม่ได้: ' . mysql_error());
}

mysql_select_db("ชื่อฐานข้อมูล", $con);

$result = mysql_query("$ตัวแปร");

while($row = mysql_fetch_array($result)){
  echo "<td>";
  echo $row['ฟิลด์ที่ต้องการแสดง'];
  echo "</td>";
}

?>


โค้ดจะออกมาในรูปนี้

<html>
<head>
new.Request("test.php?id=5",
{
method:"GET",
onComplete:showdata
}
);
}
function showdata(result){.getElementById("showdata").innerHTML=result.responseText;
var ตัวแปร = respMessage;
}
</head>
<body>
document Ajax
<table>
  <tr>
    <td><span id="showdata"></span>
  </tr>
  <tr>
 <?php
 $con = mysql_connect("host","user","password");
 if (!$con){
   die('ติดต่อฐานข้อมูลไม่ได้: ' . mysql_error());
 }

 mysql_select_db("ชื่อฐานข้อมูล", $con);

 $result = mysql_query("$ตัวแปร");

 while($row = mysql_fetch_array($result)){
   echo "<td>";
   echo $row['ฟิลด์ที่ต้องการแสดง'];
   echo "</td>";
 }

 ?>
  </tr>

</table>
</body>
</html>

 

 

 

ถ้าจะเอาสั้นๆ กว่านั้นก็
ดูตรง function showdata(result)


แล้วเรียกใช้หยั่งงี้

<?php
$con = mysql_connect("host","user","password");
if (!$con){
  die('ติดต่อฐานข้อมูลไม่ได้: ' . mysql_error());
}

mysql_select_db("ชื่อฐานข้อมูล", $con);

$result = mysql_query("$result");

while($row = mysql_fetch_array($result)){
  echo "<td>";
  echo $row['ฟิลด์ที่ต้องการแสดง'];
  echo "</td>";
}

?>


ก็จะเป็นแบบนี้

<html>
<head>
new.Request("test.php?id=5",
{
method:"GET",
onComplete:showdata
}
);
}
function showdata(result){.getElementById("showdata").innerHTML=result.responseText;
}
</head>
<body>
document Ajax
<table>
  <tr>
    <td><span id="showdata"></span>
  </tr>
  <tr>
 <?php
 $con = mysql_connect("host","user","password");
 if (!$con){
   die('ติดต่อฐานข้อมูลไม่ได้: ' . mysql_error());
 }

 mysql_select_db("ชื่อฐานข้อมูล", $con);

 $result = mysql_query("$result");

 while($row = mysql_fetch_array($result)){
   echo "<td>";
   echo $row['ฟิลด์ที่ต้องการแสดง'];
   echo "</td>";
 }

 ?>
  </tr>

</table>
</body>
</html>

 

 

 

 


 

#7

ขอแก้ไขนิดนิง
ตรง var ตัวแปร = respMessage;
เปรียวพึ่งจะสังเกตเห็นว่า พี่ใช้เป็น responseText;
เพราะฉะนั้น มันควรจะเป็น
var ตัวแปร = responseText;
.
และอีกที่ ที่ผิดอย่างไม่น่าให้อภัย
อ้างอิงจาก ความคิดเห็น #354
ถ้าจะเอาสั้นๆ กว่านั้นก็
ดูตรง function showdata(result)


แล้วเรียกใช้หยั่งงี้

<?php
$con = mysql_connect("host","user","password");
if (!$con){
  die('ติดต่อฐานข้อมูลไม่ได้: ' . mysql_error());
}




อ้างอิงจาก ความคิดเห็น #354
มันไม่ได้เรียก $result
จาก function showdata(result)
แต่มันน่าจะต้องเรียก $responseText
ที่ function showdata(result){.getElementById("showdata").innerHTML=result.responseText;




แต่ทางที่ดี ที่คิดว่าชัว เปรียวว่าน่าจะใช้วิธีแรกนะฮะ

mysql_select_db("ชื่อฐานข้อมูล", $con);

$result = mysql_query("$result");

while($row = mysql_fetch_array($result)){
  echo "<td>";
  echo $row['ฟิลด์ที่ต้องการแสดง'];
  echo "</td>";
}


?>

#8

น้า goragod ช่วยแก้ bug หน่อยฮะ


ขอแก้ไขนิดนิง
ตรง
var ตัวแปร = respMessage;
เปรียวพึ่งจะสังเกตเห็นว่า พี่ใช้เป็น responseText;
เพราะฉะนั้น มันควรจะเป็น
var ตัวแปร = responseText;
.
และอีกที่ ที่ผิดอย่างไม่น่าให้อภัย

ถ้าจะเอาสั้นๆ กว่านั้นก็
ดูตรง
function showdata(result)


แล้วเรียกใช้หยั่งงี้

<?php
$con = mysql_connect("host","user","password");
if (!$con){
  die('ติดต่อฐานข้อมูลไม่ได้: ' . mysql_error());
}

mysql_select_db("ชื่อฐานข้อมูล", $con);

$result = mysql_query("$result");

while($row = mysql_fetch_array($result)){
  echo "<td>";
  echo $row['ฟิลด์ที่ต้องการแสดง'];
  echo "</td>";
}


?>


มันไม่ได้เรียก $result
จาก function showdata(result)
แต่มันน่าจะต้องเรียก $responseText
ที่ function showdata(result){.getElementById("showdata").innerHTML=result.responseText;



แต่ทางที่ดี ที่คิดว่าชัว เปรียวว่าน่าจะใช้วิธีแรกนะฮะ

 

#9

ถ้าใช้ Ajax นะ ....

Ajax มันเป็น Javascript ต้องใช้ Javascript ในการทำงาน

รูปแบบง่ายสุดก็

<script type="text/javascript">
document.getElementById( 'elemid' ).innerHTML = req.responseText;
</scrit>

โค้ดข้างบนเป็นแค่แนวทางนะ ใช้จริงมีรายละเอียดอีกเยอะ

ต้องไปเริ่มต้นทำความเข้าใจเกี่ยวกับ Ajax บนเว็บซะก่อน ถึงจะทำได้ ลองอ่านดู ไม่อ่านทำไม่ได้นะเออ!


ไม่มีใครเป็นแต่เกิด แต่พอโตขึ้น ถ้าไม่หัด ก็ทำไม่เป็น
#10
ความคิดเห็น
ไฟล์อัปโหลด ชนิด jpg, jpeg ขนาดไฟล์ไม่เกิน 1024
^