0

i have a button in my table , original is using GET to send some variable to the new page user.php , but I don't want to show these information in url . I try to built a form and submitted it to the other user.php, but user.php get nothing:

 $('table').on( 'click', 'td .btn-info', function () {
    var parent = $(this).closest('table').parents('tr').prev();
    var parentIndex = parent.find('.index').text();
    var currentIndex = $(this).closest('tr').index();
    var data = sections[parentIndex][currentIndex];

     var mapForm = document.createElement("form");
      mapForm.target = "Map";
      mapForm.method = "POST"; // or "post" if appropriate
      mapForm.action = "user.php";

     var name= document.createElement("input");
      name.type = "hidden";
      name.id = "name";
      name.value = data.name;
      mapForm.appendChild(name);

      var loc= document.createElement("input");
      loc.type = "hidden";
      loc.id = "loc";
      loc.value = data.loc;
      mapForm.appendChild(loc);
      document.body.appendChild(mapForm );

      map=window.open("", "Map", "height=500,width=800,scrollbars=yes, resizable=yes");
     if (map) {
       mapForm.submit();
     } else {
       alert('You must allow popups for this map to work.');
      }



} );
georgetovrea
  • 537
  • 1
  • 8
  • 28

3 Answers3

0

Use $_POST instead. Post doesn't show the variables in the url. Just redirect to your php file and when you're redirected get the variables using:

$variable = $_POST['input_name'];
Gokhan Kurt
  • 8,239
  • 1
  • 27
  • 51
0

As per the answer given by Evert of Question Window.Open POST

You cannot trigger a javascript popup and then force a post request.

Three options:

  1. Trigger a POST form with target="_blank" using javascript (but this doesn't allow you to disable interface elements such as the menu bar).
  2. Open a popup locally, but don't specify a url. Use the result of window.open to alter the document to generate a form, which you'd then post.

    var myWindow = window.open("", "", "height=600,width=800,scrollbars=1,location=no,menubar=no,resizable=1,status=no,toolbar=no");
    myWindow.document.write("Write a form here and then later on trigger it");
    
  3. You really shouldn't do any of this. If it's bad for users to copy urls, there's a flaw in your application design.

  4. Added after edit: Use the 'empty window' approach, but instead of writing a form and triggering it, do a an XMLHTTPRequest (with POST) in the parent. The result of this request can be used to populate the child-window.

Community
  • 1
  • 1
Praveen Rawat
  • 724
  • 2
  • 12
  • 29
  • I have try to built a form and submitted it to the other php , but user.php get nothing, I have update my question . what's problem with my code ? – georgetovrea Nov 05 '16 at 15:00
0

Use this sample code.....

Put this code in your page's head section,

<script>
    var name = "";
    var loc = "";
    collectData()
    {
        document.getElementById("name").value = name;
        document.getElementById("loc").value = loc;
    }
</script>

Put this code in the table where you want a button,

<form action="user.php" method="POST" onsubmit="collectData()">
    <input type="hidden" id="name">
    <input type="hidden" id="loc">
    <input type=submit value="Button">
</form>

Assign values to variables in your javascript code,

name = data.name;
loc = data.loc;

And use this code to retrieve data in user.php,

$name = $_POST["name"];
$loc = $_POST["loc"];

You can pass any number of values this way... Goodluck !