dqg63264 2014-10-01 03:48
浏览 57

Javascript POST到PHP页面

My goal is to establish whether a page was opened via a JavaScript pop-up, and carry a related URL variable through to other page(s). Originally I thought I could do this with a URL variable however as JavaScript runs client-side and php is server-side there is no way to verify whether the window is a JavaScript window especially given the possibility that someone returned on a favorite or bookmark.

I originally made a similar post to this however as the idea has evolved and being stuck with the variable in the URL, this may be a better approach.

I think I have figured out the basics but the mechanics involve JavaScript that I am not so familiar with.

Here is what I have in a page right now

<script>
    function PopupCenter(pageURL, title,w,h) {

        var left = (screen.width/2)-(w/2);
        var top = (screen.height/2)-(h/2);
        var targetWin = window.open (pageURL, title, 'toolbar=no, location=no, directories=no, status=no,  menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);

    }
</script>

<a onclick="PopupCenter('/customconfig/index.php, 'CustomBuilder1',1080,700);" href="javascript:void(0);"> <img border=0  src = "/images/Animated.GIF"></a>

Now I found this page http://mentaljetsam.wordpress.com/2008/06/02/using-javascript-to-post-data-between-pages/

Which tells me how to put a POST in a JavaScript with this code:

<script>
    function postwith (to,p) { 

        var myForm = document.createElement("form");
        myForm.method="post" ;
        myForm.action = to ;

        for (var k in p) {
            var myInput = document.createElement("input") ;
            myInput.setAttribute("name", k) ;
            myInput.setAttribute("value", p[k]);
            myForm.appendChild(myInput) ;
        }

        document.body.appendChild(myForm) ;
        myForm.submit() ;
        document.body.removeChild(myForm) ;

    }
</script>

Of course the advantage of this method is that there is no URL variable when I go to the first page and I can also carry the POST value through all PHP pages.

My biggest question is how to merge the code for loading the centered JavaScript window with the code for passing the POST variable, and making them work happily together.

This means that if someone goes back on a bookmark or favorite the POST value will be NULL, and I can carry that NULL value through the subsequent page(s) as a POST hidden value.

EDIT--------

I guess I need to be clearer. I do not know much about java. I need to combine the existing java pop-up which centers the page with passing the POST. It seems doable and logical enough. Getting into AJAX which I know NOTHING about would only serve to confuse the issue further. I can not learn a new language every time I need to solve a problem.

  • 写回答

2条回答 默认 最新

  • doubi1818 2014-10-01 03:58
    关注

    I would highly recommend using something like jquery to help you with this. You'll want to use an AJAX call rather than submitting an HTML form.

    http://api.jquery.com/jQuery.ajax/

    Add to the top of your HTML file the jQuery script:

    <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>

    Change the script for sending the ajax call:

    <script> function postwith (to,p) { $.ajax({ type: "POST", url: to, data: p }); } </script>

    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退