简体   繁体   English

张贴后的jQuery重定向

[英]jquery redirect after post

I have 3 php files. 我有3个php文件。

  • file1.php contains PHP + HTML design and jQuery scripts. file1.php包含PHP + HTML设计和jQuery脚本。
  • file2.php contains just PHP and mySQL processing file2.php仅包含PHP和mySQL处理
  • file3.php generates a PDF using TCPDF, based on 2 variables that it receives in $_REQUEST, and displays the resulting PDF file3.php根据在$ _REQUEST中收到的2个变量,使用TCPDF生成PDF,并显示生成的PDF

    1. I call from a button in file1.php a $.post towards file2.php . 我从file1.php中的按钮调用$ .post到file2.php
    2. Some data processing happens in file2.php (mysql inserts and updates) and I obtain var1 and var2 . 一些数据处理发生在file2.php (mysql插入和更新)中,我获得了var1var2
    3. When processing is done in file2.php I want to go and display the PDF that is created by file3.php based on var1 and var2 file2.php中完成处理后,我要显示基于file1.php基于var1var2创建的PDF

So the jQuery code in file1.php where I call post is: 所以在file1.php中我称之为post的jQuery代码是:

        $("#saveinv").click(function() { // button is clicked
            var listview_array = new Array();
            // here I populate the array with values then...
            $.post("file2.php", 
               {json: JSON.stringify(listview_array)}, 
               function(data){alert("success"); 
            });
        });

In file2.php there is nothing to see. 在file2.php中没有任何内容。 After much processing, I end up with 2 variables that help me call the file3.php 经过大量处理,我最终得到2个变量,这些变量可以帮助我调用file3.php

file3.php must be called with 2 params: var1 and var2 so something like: 必须使用2个参数调用file3.php :var1和var2,例如:

file3.php?var1=5&var2=77

But I have no idea where to place the call or what exact syntax to use. 但是我不知道在哪里放置调用或使用什么确切的语法。 I tried different solutions without success. 我尝试了不同的解决方案,但没有成功。 Maybe I was doing it wrong and you can help me. 也许我做错了,您可以帮助我。

  • I tried to place at the end of file2.php a header('Location: file3.php?var1=5&var2=77'); 我试图在文件2.php的末尾放置一个标头('Location:file3.php?var1 = 5&var2 = 77');
  • I tried to : 我试过了 :

    • add at the end of file2.php the line : return 'file3.php?var1=5&var2=77'; 在file2.php的末尾添加以下行: return'file3.php?var1 = 5&var2 = 77';
    • then replace the alert('success') in file1.php with window.location = data.redirect but I keep getting redirected to "undefined". 然后用window.location = data.redirect替换file1.php中的alert('success'),但是我一直被重定向到“ undefined”。
  • If I do window.location='file3.php?var1=5&var2=77' in file1.php then all works perfect, but I need those variables to be sent from file2.php. 如果我在file1.php中执行window.location ='file3.php?var1 = 5&var2 = 77' ,那么一切都完美,但是我需要从file2.php发送这些变量。 They are not constants. 它们不是常数。

What should I do, and where? 我该怎么办,在哪里?

Thank you 谢谢

Just put your result as JSON in file2.php: 只需将结果作为JSON放在file2.php中:

echo '({"var1":"1","va2":"2"})';

where you get it as data inside your ajax callback function in file1.php, then use eval 在file1.php中的ajax回调函数中将其作为data获取的位置,然后使用eval

var args = eval(data);
window.location = 'file3.php?var1=' + args.var1 + '&var2=' + args.var2;

add at the end of file2.php the line : return 'file3.php?var1=5&var2=77'; 在file2.php的末尾添加以下行: return 'file3.php?var1=5&var2=77';

You cannot use return , just use print or echo and it should work. 您不能使用return ,而只能使用printecho ,它应该可以工作。

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

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