简体   繁体   中英

Javascript POST to PHP page

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.

Sorry to answer your question with another link. But it will help you :)

Here is the link: Window.open and pass parameters by post method

Look at this answer the most by Mercenary:

  var form = document.createElement("form"); form.setAttribute("method", "post"); form.setAttribute("action", "openData.do"); form.setAttribute("target", "view"); var hiddenField = document.createElement("input"); hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("name", "message"); hiddenField.setAttribute("value", "val"); form.appendChild(hiddenField); document.body.appendChild(form); window.open('', 'view'); form.submit(); 

I hope you figure out! Let me know if you need more help!

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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