|
 |
|
PHP & Javascript(AJAX) --> How Pass Javascript Array to PHP (POST or GET) |
|
 |
|
|
 |
 |
|
ผมสอบถามการส่ง Array ใน Javascript ส่งไปยัง PHP หน่อยครับ
echo ออกมา เห็นค่านะครับ แต่ไม่ทราบว่าจะนำไปใช้งานได้อย่างไร
คือจะเอา Array (PHP) นี้ ไป SELECT ฐานข้อมูล แบบ WHERE ... IN (..., ..., ...) น่ะครับ
Code (JavaScript)
var dataQuery = new Array();
dataQuery[0] = "01";
dataQuery[1] = "02";
dataQuery[2] = "03";
dataQuery[3] = "04";
dataQuery[4] = "05";
dataQuery[5] = "06";
var url = 'php_post.php';
var pmeters = 'myName='+dataQuery;
HttPRequest.open('POST',url,true);
HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
HttPRequest.setRequestHeader("Content-length", pmeters.length);
HttPRequest.setRequestHeader("Connection", "close");
HttPRequest.send(pmeters);
Code (PHP)
<?php
$s = $_POST["myName"];
echo $s;
?>
แล้วถ้า Array ที่ผมสร้างมา บางตำแหน่งเป็นค่าว่าง จะมีปัญหาหรือไม่ครับ
เพราะตัว Array นี้ ผมเอาไว้เก็บ Input จากผู้ใช้น่ะครับ เป็น Checkbox มี 6 ช่อง
เว้นช่องไหน Array ตำแหน่นั้นก็จะว่าง ขอบคุณครับ
Tag : PHP, Ms SQL Server 2012, HTML/CSS, JavaScript, jQuery

|
ประวัติการแก้ไข 2014-02-07 17:22:21
|
 |
 |
 |
 |
Date :
2014-02-07 17:22:26 |
By :
seattlesonic |
View :
1031 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองดูครับประมาณนี้ครับ
Code (PHP)
$count=count($s)-1;
$i=0;
$sql_in="";
foreach($s as $s_value){
if($s_value!=""){
if($i!=$count){
$sql_in="$s_value,";
}else{
$sql_in.="$s_value";
}
}
}
//SELECT ฐานข้อมูล แบบ WHERE ... IN ($sql_in)
|
 |
 |
 |
 |
Date :
2014-02-07 19:23:57 |
By :
narubet |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ลองใช้ jQuery ดูครับ
Code (PHP)
<script type="text/javascript">
$(function () {
var dataQuery = new Array();
dataQuery[0] = "01";
dataQuery[1] = "02";
dataQuery[2] = "03";
dataQuery[3] = "04";
dataQuery[4] = "05";
dataQuery[5] = "06";
$.ajax({
url: 'php_post.php',
type: 'post',
data: {'myName[]':dataQuery},
success: function (data) {
data
}
});
});
</script>
|
 |
 |
 |
 |
Date :
2014-02-07 19:59:53 |
By :
Krungsri |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณนะครับ แต่ลองดูแล้ว ไม่ได้ผลน่ะครับ หรือผมทำผิดพลาดอะไรไปก็ไม่ทราบได้
ผมลองใช้วิธีนี้
Code (JavaScript)
HttPRequest.onreadystatechange = function()
{
if (HttPRequest.readyState == 3)
document.getElementById("mySpan").innerHTML = "Processing Request";
if (HttPRequest.readyState == 4)
document.getElementById("mySpan").innerHTML = HttPRequest.responseText;
}
responseText เหมือนจะส่งค่ากลับมาเป็น text ก้อนเดียว ไม่ว่าตัวแปรที่ echo ที่ไฟล์ php จะเป็น array
responseText ก็จะเป็น text ค่าเดียว เหมือนเอา string มาต่อกัน
พอจะแนะนำได้บ้างมั้ยครับ
|
 |
 |
 |
 |
Date :
2014-02-10 19:28:01 |
By :
seattlesonic |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 00
|