22813

Javascript POST to PHP page

Question:

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 <a href="http://mentaljetsam.wordpress.com/2008/06/02/using-javascript-to-post-data-between-pages/" rel="nofollow">http://mentaljetsam.wordpress.com/2008/06/02/using-javascript-to-post-data-between-pages/</a>

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.

Answer1:

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

Here is the link: <a href="https://stackoverflow.com/questions/3951768/window-open-and-pass-parameters-by-post-method" rel="nofollow">Window.open and pass parameters by post method</a>

Look at this answer the most by Mercenary:

<pre class="snippet-code-js lang-js prettyprint-override"> 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!

Answer2:

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.

<a href="http://api.jquery.com/jQuery.ajax/" rel="nofollow">http://api.jquery.com/jQuery.ajax/</a>

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>

Recommend

  • Multiple CKEditors with different height
  • AEM 6.3, maven-bundle-plugin automate OSGi SCR metadata for OSGi annotated classes
  • How can I integrate this Scrollable Tab library in Eclipse?
  • sed newline and carriage return pattern capture
  • AMD - Building library example
  • How to capture and parse JSON returned from Google Maps v3 API?
  • String values look the same but don't “.equals()” each other
  • Batch For loop.. wildcards to determine length of searched files?
  • matlab gui radio buttons
  • | operator operation in linq?
  • Help with eraser from buffered Image
  • placing widget in listbox/dropdown in gwt
  • How to focus menuBar() with Qt
  • Using third party libraries
  • How to redirect or show a page rather than “Forbidden” when i have directory listings off (htaccess/
  • Bokeh custom ToolTips {safe} tag displays nothing
  • Why does CollectionViewSource.GetDefaultView(…) return the wrong CurrentItem from inside a Task thre
  • Change the width of the JQM panels
  • why fullscreen functionality is not working in chrome?
  • Mapping two workspace folders to the same working directory
  • Javascript (MVC) load image (byte array) from database
  • Opening two instances of InAppBrowser (_system and _blank) prevents events from triggering
  • Replace Fragment with another on back button
  • Send data from edittext to listview
  • Portable JRE on Linux - possible?
  • Jquery Mobile pageLoading() Method how does it work?
  • SQL Query - Table Joining Problems
  • Can't remove headers after they are sent
  • Intel-64 and ia32 atomic operations acquire-release semantics and GCC 5+
  • Zoom in and out of jPanel
  • Simulate click Geckofx vb,net
  • Firefox Extension - Monitor refresh and change of tab
  • How to use JavaScript to determine whether a file exists in a directory?
  • Saving Changes After In-App Purchase Has Been Purchased
  • How do I pass the string value parameter of the selected list item from an auto-populated dropdown l
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • To display the title for the current loaction in map in iphone
  • failed to connect to specific WiFi in android programmatically