GORAGOD.com

GORAGOD.com

freelance, web developer, web designer, hosting, domain name

ช่วยดู Code PHP กับ Ajax ทีจ้า มันไม่ดึงในฐานข้อมูลมาใช้อ่าคะ

เขียนไฟล์ PHP แล้วต้องการใช้งาน Ajax เข้ามาช่วย จะเรียกใช้ Form Select อะคะ แล้วมันไม่เรียกในฐานข้อมูลมาโชว์ ช่วยดู Code ให้ทีนะคะ



FileNameshowUser.php


<?php


$con = mysql_connect("localhost","root","1234");


if(!$con)


{


die("Could not connect: ' . mysql_error()");


}


mysql_select_db("DBAJax",$con);


mysql_query("INSERT INTO Customers (id,FirstName,LastName,Age,Hometown,Job) VALUES('1','Peter','Griffin','41','Quahog','Brewery')");


mysql_query("INSERT INTO Customers (id,FirstName,LastName,Age,Hometown,Job) VALUES('2','Lois','Griffin','40','Newport','Piano Teacher')");


mysql_query("INSERT INTO Customers (id,FirstName,LastName,Age,Hometown,Job) VALUES('3','Joseph','Swanson','39','Quahog','Police Officer')");


mysql_query("INSERT INTO Customers (id,FirstName,LastName,Age,Hometown,Job) VALUES('4','Glenn','Quaomire','41','Quahog','Pilot')");


mysql_close($con);


?>


 


<html>


<head>


<script type="text/javascript">


function showUser(str)


{


if (str=="")


 {


document.getElementById("txtHint").innerHTML="";


return;


 }


 if (window.XMLHttpRequest)


{ // code for IE7+,Firefox,Chrome,Opera,Safari


xmlhttp=new XMLHttpRequest();


}


 else


{// code for IE6,IE5


xalhttp=new ActiveXObject("Microsoft.XMLHTTP");


}


xmlhttp.onreadystatechange=function()


{


if(xmlhttp.readyState==4 && xalhttp.status==200)


{


document.getElementById("txtHint").innerHTML = xmlhttp.responseTest;


}


}


//ช่วงดึง method getuser.php มาใช้งาน


xmlhttp.open("GET","getuser.php?q="+str,true);


xmlhttp.send(); //แล้วส่งข้อมูลไปที่ Object .send


}


</script>


</head>


<body>


<form>


<select name="users" onchange="showUser(this.value)">


<option value="">Select a person</option>


<option value="1">Peter Griffin</option>


<option value="2">Lois Griffin</option>


<option value="3">Joseph Swanson</option>


<option value="4">Glenn Quagmire</option>


</select>


</form>


<br />


<div id="txtHint"><b>Person info will be listed here</b></div>


</body>


</html>



getuser.php


<?php


$q=$_GET["q"];


$con = mysql_connect('localhost','root','1234');


if (!$con) //เทสว่า connect ได้หรือป่าว


  {


  die("Could not connect: ' . mysql_error()");


  }


mysql_select_db("DBAJax",$con); //อ้างอิงชื่อ DB ของคุณ,อ้างอิง Connect String


$sql="SELECT * FROM Customers WHERE id = ' ".$q." ' ";


$result = mysql_query($sql);


echo "<table border='1'>


<tr>


<th>id</th>


<th>Firstname</th>


<th>Lastname</th>


<th>Age</th>


<th>Hometown</th>


<th>Job</th>


</tr>";


while($row = mysql_fetch_array($result))


{


echo"<tr>";


echo"<td>" . $row['id']."</td>";


echo"<td>" . $row['FirstName']."</td>";


echo"<td>" . $row['LastName']."</td>";


echo"<td>" . $row['Age']."</td>";


echo"<td>" . $row['hometown']."</td>";


echo"<td>" . $row['Job']."</td>";


echo"</tr>";


}


echo "</table>";


mysql_close($con);


?>



ลองทำดูแล้วก็ไม่ขึ้น เลยใช้อีกวิธีคือ สร้าง+เรียก ข้อมูลมาเองในฐานข้อมูล แล้วก็กำหนด Primary Key เป็น id ปรากฏว่าใช้งานได้คะ



แต่อยากทราบ วิธีที่เขียน Code ที่สามารถสร้าง+เรียกใช้งานในฐานข้อมูลเอาเอง ไม่ทราบว่าต้องแก้ Code ตรงไหนคะ

รบกวนด้วยนะคะ ขอบตุณล่วงหน้ามากๆๆคะ


10 ม.ค. 2554 2 3,100


 if (window.XMLHttpRequest)


{ // code for IE7+,Firefox,Chrome,Opera,Safari


xmlhttp=new XMLHttpRequest();


}


 else


{// code for IE6,IE5


xalhttp=new ActiveXObject("Microsoft.XMLHTTP");


}


xmlhttp.onreadystatechange=function()


{



ใช้ตัวไหนแน่ ?....



ลองเรียก getuser.php?q=xxx ตรงๆดูว่าเรียกแล้วได้ผลลัพท์ตามต้องการมั้ย ซึ่งปกติควรได้



ในกรณีที่ข้อมูลที่ส่งมีช่องว่าง เช่น ชื่อคน ควรเข้ารหัสข้อมูลก่อนส่ง



str = encodeURIComponent(str);



ตอนรับ ไม่ต้องถอด เพราะ php ถอดให้



วิธีอื่นๆ เช่น ใช้ POST แทน GET จะชัวร์กว่า (ปกติผมจะไม่ใช้ GET เลย)



สุดท้าย ลองเปลี่ยนเป็น UTF-8

#1

ขอบคุณค่า แก้ได้แล้ว พิมผิดนี่เอง = ="
#2
ความคิดเห็น
ไฟล์อัปโหลด ชนิด jpg, jpeg ขนาดไฟล์ไม่เกิน 64M
^