刷新AJAX请求后,PHP会话变种变种、AJAX、PHP

2023-09-10 14:34:01 作者:川化秀树

我有的index.php callSession04.php 。当的index.php 并AJAX请求PHP会话变量在 callSession04.php 设置为保存当前页面和行每页但的index.php PHP会话变量留为初始状态,直到我刷新的index.php

I've index.php and callSession04.php. When index.php does an AJAX request PHP SESSION variables are set on callSession04.php to store the current page and rows per page but on index.php PHP SESSION variables stay as initial state until I refresh index.php

您可以看到这里的例子,需要每一个AJAX请求之前刷新页面:

You can see the example here, Need to refresh page before each AJAX request:

http://www.sanchezvalero.com/DWS/pracSESIONES/ej4/ sesion04.php

这里是code:

的index.php

index.php

<? session_start(); ?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Listado de empleados</title>
<script type="text/javascript" src="jquery-1.10.2.min.js"></script>
</head>
<body>
<div id="content" align="center"></div>
<p>
    <div align="center">
        <label for="fldRows">Resultados por página:</label>
        <input name="fldRows" type="text" id="fldRows" size="2" />
    </div>
</p>
<p>
    <div id="manage" align="center">
        <input name="btnFirst" type="button" id="btn1" value="|&lt;" />
        <input name="btnBefore" type="button" id="btn2" value="&lt;" />
        <input name="btnAfter" type="button" id="btn3" value="&gt;" />
        <input name="btnLast" type="button" id="btn4" value="&gt;|" />
        <p><a href="destroy.php">Reset</a></p>
    </div>
</p>
<script type="text/javascript">
$(document).ready(function() {
    <? if(!isset($_SESSION['rows'])){ ?>
        $("#fldRows").val("10");
    <? } else { ?>
        $("#fldRows").val("<? echo $_SESSION['rows']; ?>");
    <? } if(!isset($_SESSION['actp'])){ ?>
        $actp=0;
    <? } else { ?>
        $actp=<? echo $_SESSION['actp']; ?>;
    <? } ?>
    $.ajax({type: "GET",
            url: "callSesion04.php",
            data: "rows="+$("#fldRows").val()+"&actp="+$actp,
            success: function(data) {
                $("#content").html(data);
            }
    });
});
$("#fldRows").keyup(function() {
    if($(this).val()>=0){
        $.ajax({type: "GET",
                url: "callSesion04.php",
                data: "rows="+$(this).val()+"&actp=0",
                success: function(data) {
                    $("#content").html(data);
                }
        }); 
    }
});
$("body").on("click","#manage input",function(){
    $id=$(this).attr('id').substr($(this).attr('id').search(/\d/));
    $.ajax({type:"GET",
            url:"callSesion04.php",
            data:"pag="+$id+"&actp=<? echo $_SESSION['actp']; ?>&rows=<? echo $_SESSION['rows']; ?>",
            success: function(data) {
                $("#content").html(data);
            }
    });
});
</script> 
</body>
</html>

callSession04.php

callSession04.php

<? session_start();

$dom = new DOMDocument();
$dom->load('empleados.xml');
$empleados=$dom->getElementsByTagName('RECORD');
foreach($empleados as $empleado){
    $ids=$empleado->getElementsByTagName('ID_EMPLEADO');
    $id=$ids->item(0)->nodeValue;
    $array_ids[]=$id;
    $nombres=$empleado->getElementsByTagName('NOMBRE');
    $nombre=$nombres->item(0)->nodeValue;
    $array_nombres[]=$nombre;
    $apellidos=$empleado->getElementsByTagName('APELLIDOS');
    $apellido=$apellidos->item(0)->nodeValue;
    $array_apellidos[]=$apellido;
    $fechas=$empleado->getElementsByTagName('FECHA_NACIMIENTO');
    $fecha=$fechas->item(0)->nodeValue;
    $array_fechas[]=$fecha;
    $tipos=$empleado->getElementsByTagName('TIPO_EMPLEADO');
    $tipo=$tipos->item(0)->nodeValue;
    $array_tipos[]=$tipo;
    $hijos=$empleado->getElementsByTagName('NUM_HIJOS');
    $hijo=$hijos->item(0)->nodeValue;
    $array_hijos[]=$hijo;
}

$rows=$_GET['rows'];
$actp=$_GET['actp'];
$pag=$_GET['pag'];

$_SESSION['rows']=$rows;

if($rows>0){
    $tpag=intval(count($array_ids)/$rows);
}

if($pag=='1'){
    $actp=0;
}else if($pag=='2' && $actp>0){
    $actp--;
}else if($pag=='3' && $actp<$tpag){
    $actp++;
}else if($pag=='4'){
    $actp=$tpag;
}

$_SESSION['actp']=$actp;

$minrow=$rows*$actp;
$maxrow=$rows*$actp+$rows;

if($maxrow>count($array_ids)){
    $maxrow=count($array_ids);
}

echo "<p align='center'><strong>EMPLEADOS</strong></p>";
echo "<table border='1' cellspacing='0' cellpadding='5'>";
echo "<tr><td>ID</td><td>Nombre</td><td>Apellidos</td><td>Nacimiento</td><td>Tipo</td><td>Hijos</td></tr>";
for($i=$minrow;$i<$maxrow;$i++){
    echo "<tr><td>".$array_ids[$i]."</td><td>".$array_nombres[$i]."</td><td>".$array_apellidos[$i]."</td>
          <td>".$array_fechas[$i]."</td><td>".$array_tipos[$i]."</td><td>".$array_hijos[$i]."</td></tr>";
}   
echo "</table>";
?>

我需要知道如何在index.php文件withouth的preSS F5刷新PHP会话瓦尔。

I need to know how refresh PHP SESSION VARS on index.php withouth press F5.

推荐答案

最后,我解决了这个,解决方案,JSON。不需要刷新PHP会话瓦尔上的index.php,只在callSession04.php,只是我已经使用AJAX回调,以反映当前的服务器状态从callSession04.php解析JSON阵列上的index.php那么你可以设置新的当前每页页和行瓦尔。

Finally I solved this, the solution, JSON. Is not necessary to refresh PHP SESSION vars on index.php, only on callSession04.php, simply I've to use AJAX callback to reflect the current server state parsing JSON array on index.php from callSession04.php then you can set new current page and rows per page vars.

的index.php

index.php

<? session_start(); ?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Listado de empleados</title>
<script type="text/javascript" src="jquery-1.10.2.min.js"></script>
</head>
<body>
<div id="content" align="center"></div>
<p>
    <div align="center">
        <label for="fldRows">Resultados por página:</label>
        <input name="fldRows" type="text" id="fldRows" size="2" />
    </div>
</p>
<p>
    <div id="manage" align="center">
        <input name="btnFirst" type="button" id="btn1" value="|&lt;" />
        <input name="btnBefore" type="button" id="btn2" value="&lt;" />
        <input name="btnAfter" type="button" id="btn3" value="&gt;" />
        <input name="btnLast" type="button" id="btn4" value="&gt;|" />
        <p><a href="destroy.php">Reset</a></p>
    </div>
</p>
<script type="text/javascript">
$(document).ready(function() {
    <? if(!isset($_SESSION['rows'])){ ?>
        $("#fldRows").val("10");
        $rows=10;
    <? } else { ?>
        $("#fldRows").val("<? echo $_SESSION['rows']; ?>");
        $rows=<? echo $_SESSION['rows']; ?>;
    <? } if(!isset($_SESSION['actp'])){ ?>
        $actp=0;
    <? } else { ?>
        $actp=<? echo $_SESSION['actp']; ?>;
    <? } ?>
    $.ajax({type: "GET",
            url: "callSesion04.php",
            data: "rows="+$("#fldRows").val()+"&actp="+$actp,
            success: function(data) {
                var json = $.parseJSON(data);
                $("#content").html(json.html);
            }
    });
});
$("#fldRows").keyup(function() {
    if($(this).val()>=0){
        $.ajax({type: "GET",
                url: "callSesion04.php",
                data: "rows="+$(this).val()+"&actp=0",
                success: function(data) {
                    var json = $.parseJSON(data);
                    $rows=json.rows;
                    $("#content").html(json.html);
                }
        }); 
    }
});
$("body").on("click","#manage input",function(){
    $id=$(this).attr('id').substr($(this).attr('id').search(/\d/));
    $.ajax({type:"GET",
            url:"callSesion04.php",
            data:"pag="+$id+"&actp="+$actp+"&rows="+$rows,
            success: function(data) {
                    var json = $.parseJSON(data);
                    $actp=json.actp;
                    $("#content").html(json.html);
            }
    });
});
</script> 
</body>
</html>

callSession04.php

callSession04.php

<? session_start();

$dom = new DOMDocument();
$dom->load('empleados.xml');
$empleados=$dom->getElementsByTagName('RECORD');
foreach($empleados as $empleado){
    $ids=$empleado->getElementsByTagName('ID_EMPLEADO');
    $id=$ids->item(0)->nodeValue;
    $array_ids[]=$id;
    $nombres=$empleado->getElementsByTagName('NOMBRE');
    $nombre=$nombres->item(0)->nodeValue;
    $array_nombres[]=$nombre;
    $apellidos=$empleado->getElementsByTagName('APELLIDOS');
    $apellido=$apellidos->item(0)->nodeValue;
    $array_apellidos[]=$apellido;
    $fechas=$empleado->getElementsByTagName('FECHA_NACIMIENTO');
    $fecha=$fechas->item(0)->nodeValue;
    $array_fechas[]=$fecha;
    $tipos=$empleado->getElementsByTagName('TIPO_EMPLEADO');
    $tipo=$tipos->item(0)->nodeValue;
    $array_tipos[]=$tipo;
    $hijos=$empleado->getElementsByTagName('NUM_HIJOS');
    $hijo=$hijos->item(0)->nodeValue;
    $array_hijos[]=$hijo;
}

$rows=$_GET['rows'];
$actp=$_GET['actp'];
$pag=$_GET['pag'];

if($rows>0){
    $tpag=intval(count($array_ids)/$rows);
}

if($pag=='1'){
    $actp=0;
}else if($pag=='2' && $actp>0){
    $actp--;
}else if($pag=='3' && $actp<$tpag){
    $actp++;
}else if($pag=='4'){
    $actp=$tpag;
}

$_SESSION['rows']=$rows;
$_SESSION['actp']=$actp;

$minrow=$rows*$actp;
$maxrow=$rows*$actp+$rows;

if($maxrow>count($array_ids)){
    $maxrow=count($array_ids);
}

$html = "<p align='center'><strong>EMPLEADOS</strong></p>";
$html .= "<table border='1' cellspacing='0' cellpadding='5'>";
$html .= "<tr><td>ID</td><td>Nombre</td><td>Apellidos</td><td>Nacimiento</td><td>Tipo</td><td>Hijos</td></tr>";
for($i=$minrow;$i<$maxrow;$i++){
    $html .= "<tr><td>".$array_ids[$i]."</td><td>".$array_nombres[$i]."</td><td>".$array_apellidos[$i]."</td>";
    $html .= "<td>".$array_fechas[$i]."</td><td>".$array_tipos[$i]."</td><td>".$array_hijos[$i]."</td></tr>";
}   
$html .= "</table>";
$aPag = array("rows"=>$rows,"actp"=>$actp,"html"=>$html);
echo json_encode($aPag);
?>