[英]How to pass this values to javascript and display 1 by 1 on HTML page?
Like this I have 100 values in database 像这样我在数据库中有100个值
I want this values to be passed to javascript via PHP using ajax. 我希望将此值通过使用ajax的PHP传递给javascript。
PHP CODE: PHP代码:
for($i=0;$i<=9;$i++)
{
$random = (10 * $i) + rand(1,10);
$result = mysql_query("SELECT * FROM check_val WHERE id = '$random'") or exit(mysql_error());
if(mysql_num_rows($result) == 1){
$row = mysql_fetch_assoc($result);
$value = $row['val'];
$alpha = $row['alp'];
}
$arr = array("val"=>$value,
"alp"=>$alpha
);
echo json_encode($arr)."<br>";
}
OUTPUT: 输出:
{"val":"1","alp":"A1"}<br>
{"val":"15","alp":"A15"}<br>
{"val":"23","alp":"A23"}<br>
{"val":"37","alp":"A37"}<br>
{"val":"42","alp":"A42"}<br>
{"val":"51","alp":"A51"}<br>
{"val":"67","alp":"A67"}<br>
{"val":"71","alp":"A71"}<br>
{"val":"84","alp":"A84"}<br>
{"val":"100","alp":"A100"}<br>
AJAX code: AJAX代码:
$.ajax({
url: "index.php",
type: "post",
dataType: "json",
success: function(response) {
$("#val").html(response.val);
$("#alp").html(response.alp);
}
});
This ajax code is used when the output is this : 当输出为this时,将使用此ajax代码:
{"val":"1","alp":"A1"}
ie just a single value 即只是一个值
How can I read all the values and display them 1 by 1 when the button is clicked in the HTML page. 当在HTML页面中单击按钮时,如何读取所有值并按1显示它们。
HTML code: HTML代码:
<p id="val"></p><br>
<p id="alp"></p><br>
<button>Click Me</button>
Try this: backend: 试试这个:后端:
$offset = (int) $_POST['offset'];
if ( $offset < 0 ) {
$offset = 0;
}
$result = array();
$resource = mysql_query("SELECT * FROM check_val ORDER BY id LIMIT 1 OFFSET {$offset}");
if ( $resource === false )
{
exit(mysql_error());
} elseif ( mysql_num_rows( $resource ) ) {
$result = mysql_fetch_assoc($resource);
}
echo json_encode( $result );
frontend html: 前端html:
<div class="wrapper">
<p id="val"></p><br>
<p id="alp"></p><br>
</div>
<button>Click Me</button>
frontend javascript: 前端javascript:
$("button").on("click", function () {
var $container = $(".wrapper");
var offset = parseInt( $container.data("offset"), 10 ) || 0;
$.ajax({
url: "index.php",
type: "post",
dataType: "json",
data: {offset: offset},
success: function(response) {
if ( response != undefined && response.val != undefined ) {
$("#val").html(response.val);
$("#alp").html(response.alp);
$container.data("offset", offset+1);
}
}
});
});
I'm not 100% sure if this is the answer you're looking for but I'll try. 我不确定100%是否是您要的答案,但我会尽力的。 I'm also not too sure how you're using the ajax. 我也不太确定您如何使用ajax。
I'd create a script element and JavaScript json variable from the PHP as follows: 我将从PHP中创建脚本元素和JavaScript json变量,如下所示:
echo "<script>var jsonContent = [";
for($i=0;$i<=9;$i++) {
...
echo json_encode($arr) . ","
...
}
echo "]</script>";
Then JavaScript (jQuery): 然后是JavaScript(jQuery):
var clickCount = 0;
$('button').on('click', function() {
$("#val").html(jsonContent[clickCount].val);
$("#alp").html(jsonContent[clickCount].alp);
clickCount++;
});
Each time the button is clicked, the content should start looping through the content in the jsonContent array. 每次单击按钮时,内容应开始循环遍历jsonContent数组中的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.