简体   繁体   English

重新加载值$ _SESSION-PHP + AJAX

[英]Reload Value $_SESSION - PHP + AJAX

i have a page called 我有一个页面叫做

" index.php " “ index.php”

where have some forms that users can fill it , after this when click at "Submit" all my form its send via AJAX to another page called 在哪里有一些用户可以填写的表格,之后单击“提交”将我的所有表格通过AJAX发送到另一个页面

"download.php " “ download.php”

this is how i send the form , via AJAX: 这就是我通过AJAX发送表单的方式:

<script src="js/jquery-3.2.1.js" type="text/javascript"></script>
<script type="text/javascript">

$( document ).ready(function() {

    $('#button_final').hide();

    function enviar(arg1,arg2){

        document.getElementById('qtamostra').value = arg1;
        $("#warning_alerta").hide();
        $("#input_vazio").hide();
        $("#login_erro").hide();

        var formData = new FormData($('#form_principal')[0]);
        $("#loading").show();
        setTimeout(function() {
            $.ajax({url: "/tkclientespdo/etiquetaslog/carrefour/cumbuca/download.php",
                type: "post",
                data: formData,
                cache: false,
                async:true,
                contentType: false,
                processData: false,
                success: function(result){
                    if(result == 1)//logado
                    {
                        //$("#hide").show();
                        //$('#text_modal').html('Foi enviado 2 Etiquetas de Amostras');
                        $('#modal-container-188642').modal(arg2);
                    }
                    else if (result == 3)
                    {               
                        $('#modal-container-188642').modal(arg2);
                    }
                    else if (result == 300)
                    {
                        $("#login_sucesso").show();
                        setTimeout(function(){
                            window.location = "index.php";},3000);
                        }
                        $("#loading").hide();
                    }
                }
            )
        },300);
    };
</script>

on "download.php" , i have a variable that return the timestamp when the user click at " Send ". 在“ download.php”上,我有一个变量,当用户单击“发送”时,该变量返回时间戳。

$horaenviooff = date("dmYGis").'OFF';

after this , on "download.php" i have a code $_SESSION['user_job'] = $horaenviooff , that fill with the value . 之后,在“ download.php”上,我有一个代码$_SESSION['user_job'] = $horaenviooff ,其中填充了值。

after this , i made a echo 3 like the following $_SESSION['user_job'] = $horaenviooff;echo 3; 在此之后,我进行了如下所示的echo 3 $_SESSION['user_job'] = $horaenviooff;echo 3; .

Where on "index.php" its showed a modal with $_SESSION['user_job'] , but , only show $_SESSION['user_job'] , if i reload the page index.php. 在“ index.php”上的哪个位置显示了$ _SESSION ['user_job']的模式,但是,如果我重新加载页面index.php,则仅显示$ _SESSION ['user_job']。

its possible for example , after echo 3 , show a div at index.php with my $_SESSION['user_job'] ? 例如,在echo 3 ,可以用$ _SESSION ['user_job']在index.php上显示div吗?

Thanks. 谢谢。

Javascript can't access the $_SESSION variable directly, as $_SESSION lives on the Server while Javascript is a client-side technology. Javascript无法直接访问$_SESSION变量,因为$ _SESSION驻留在服务器上,而Javascript是一种客户端技术。 To get around this, one solution could be: Don't just write $horaenviooff to the $_SESSION , send it back to the calling AJAX function, too. 要解决此问题,一种解决方案可能是:不要只是将$horaenviooff写入$_SESSION$horaenviooff将其发送回调用的AJAX函数。 You could for example let your "download.php" return JSON to easily transfer and access multiple values: 例如,您可以让您的“ download.php”返回JSON,以轻松地传输和访问多个值:

$response = [
    'result' => 3,
    'user_job' => $horaenviooff
];
header('Content-Type: application/json');
echo json_encode($response);

You can then access the response's values in Javascript like: 然后,您可以使用Javascript访问响应的值,例如:

/*...*/
success: function(response){
    if(response.result == 3){
        alert(response.user_job);
    }
} 
/*...*/    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM