如何使用ajax触发器php函数在数组中存储值
我使用Mysql表和php在页面上打印一些数据,每个数据值旁边都有一个按钮。当为每个值(具有唯一id)单击按钮时,将进行ajax调用,以触发将按钮id插入数组中的php函数 问题是,当我单击其中一个按钮时,值将插入数组中,但是当单击另一个按钮时,数组中的现有值将被删除,并且只有新值存在 我不确定如何停止现有值以保留在数组中 下面是我的函数代码phpfile.php如何使用ajax触发器php函数在数组中存储值,php,mysql,ajax,Php,Mysql,Ajax,我使用Mysql表和php在页面上打印一些数据,每个数据值旁边都有一个按钮。当为每个值(具有唯一id)单击按钮时,将进行ajax调用,以触发将按钮id插入数组中的php函数 问题是,当我单击其中一个按钮时,值将插入数组中,但是当单击另一个按钮时,数组中的现有值将被删除,并且只有新值存在 我不确定如何停止现有值以保留在数组中 下面是我的函数代码phpfile.php public function storing_value(){ $qry= $_GET['passedvalue']
public function storing_value(){
$qry= $_GET['passedvalue'];
$info = array();
$info[$qry]= $qry;
foreach($info as $key => $value)
{
echo $value;
}
}
索引页
<script> <!---------- A J A X PART --- no need to worry about this---->
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","phpfile.php?passedvalue="+str,true);
xmlhttp.send();
}
}
</script> <!---------- A J A X PART --- no need to worry about this---->
<div id="txtHint">
<b>All array data will be printed here every time button is clicked</b>
</div>
<?php foreach($values as $value) {?>
<button class="<?= $value['id']; ?>"
value="<?= $value['id']; ?>"
onclick="showUser(this.value)" >Like</button>
<?php } ?>
函数showUser(str){
如果(str==“”){
document.getElementById(“txtHint”).innerHTML=“”;
返回;
}否则{
if(window.XMLHttpRequest){
//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}否则{
//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(this.readyState==4&&this.status==200){
document.getElementById(“txtHint”).innerHTML=this.responseText;
}
};
open(“GET”、“phpfile.php?passedvalue=“+str,true”);
xmlhttp.send();
}
}
每次单击按钮时,都会在此处打印所有阵列数据
在文件的第一行中启动会话,并使用一个会话变量存储数组值,如下所示
<?php session_start(); ?>
$info[$qry]=$qry代码>意味着您总是有一个数组,其中一个元素的索引为$QRYOW非常好,非常感谢@Mahipal Patel。是否每次使用我的方法都会重置数组值?我以为数组会存储每个密钥,直到你销毁它
public function storing_value(){
$qry= $_GET['passedvalue'];
$_SESSION['myarray'][$qry]= $qry;
foreach($_SESSION['myarray'] as $key => $value)
{
echo $value;
}
}