I was wondering if anyone knows how to submit POST data without placing it in a form and having the user click a button to submit the data. I just simply want to send POST data to another page when i want through code.

I tried using cURL and the page i am sending the data is having a fit so I do not think it is an option.

Any help you can provide would be greatly appreciated!!!

    You can do it through Javascript. It requires that you create form with hidden fields and manually load the data into it.

    I use this code in one of my sites to submit a form and data, without acutally showning a form. This, I find is especially useful if you want to create link which when clicked submits information via the post method to a script elsewhere:

    function submitForm(action, method, inputArray)
    {	
    	var theForm = document.createElement('form');
    	var child, x;
    
    theForm.setAttribute('action', action);
    theForm.setAttribute('method', method);
    theForm.setAttribute('target', '_blank');
    
    for(x = 0; x < inputArray.length; x++)
    {
    	child = document.createElement('input');
    
    	child.setAttribute('type', 'hidden');
    	child.setAttribute('name', inputArray[x][0]);
    	child.setAttribute('value', inputArray[x][1]);
    
    	theForm.appendChild(child);
    }
    
    document.body.appendChild(theForm);
    theForm.submit();
    document.body.removeChild(theForm);
    }
    

    Below is an example of how to use it:

    <a title="Click AWB Number for POD information"
    		       href="http://www.dhl.co.uk/" target="_blank"
                                                  onclick="submitForm('http://www.dhl.co.uk/cgi-bin/tracking.pl', 'POST',
                         	                  Array(	    		  	                                          
    Array('AWB', '12345678') , Array('TID', 'GB_EN') , Array('docheck', 'on') )); return false;" >12345678</a>

      Hard Answer:

      <script type="text/javascript">
      <!--
      formSubmit()
      {
         documents.myform.submit();
      }
      //-->
      </script>
      
      <input type=hidden name=data value=<?= $data; ?>>
      
      <a href=nextpage.html onClick='formSubmit();'>Click here for next page</a>

      Easy Answer:
      If it doesn't HAVE to be POST data, use session data.

        Thanks for the the feedback. I am currently doing it through JavaScript but i am a bit concerned about users who have JavaScript disabled.

        I was just wondering if there was another way.

          Curiosity is getting the better of me...

          You're using PHP, you want to send data from page to page... why through POST? Why not through session data?

            If you are sending data to another page on your site, then you should be storing it on the server using sessions. However, if you are sending data to another site, you can only do this via Javascript, a HTML form which the user submits manually or by making an HTTP request from the PHP script (bear in mind, if you did this then it would be your server connecting and posting the data - not the user).

              visualAd wrote:

              However, if you are sending data to another site, you can only do this via Javascript

              wrong, you can send post data with php using either fopen or the curl library.

                Sessions seem to be the best option like others have said. Curl should also work nicely. What problems were you having with it? I've never had problems with curl and post. Post curl example

                  Thanks for the responses.

                  To give you a little insight into why this is important is that i have a client that would like thousands of their employees to access a known retailers website with a username and password. The problem is each user has several usernames and passwords to the retailer’s site due to different purchasing options. The client would like the user to have to remember only one generic username and password.

                  The user would then only have to enter the generic username and password and select from a dropdown different purchasing options. The script I am attempting to write then would validate the generic username and password and then post the correct username and password (determined by the purchasing options selected from the dropdown) to the retailer’s site and automatically log them in.

                  I tried cURL and I got unexpected results. This is the first time I have used cURL so, by no means do I really know what I am doing, but it seemed straightforward.

                  The PHP I used to test cURL is as follows...

                  $param .= "LoginType=A&";
                  $param .= "LOGINID=demo2005&";
                  $param .= "PASSWORD=demo2005&";
                  $param .= "MenuType=P&";
                  $param .= "logouturl=";
                  
                  $ch = curl_init();
                  curl_setopt($ch, CURLOPT_URL, 'https://bsd.officedepot.com/postLogin.do');
                  curl_setopt($ch, CURLOPT_POST, 1);
                  curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
                  
                  $output = curl_exec($ch);
                  curl_close($ch);
                  print $output;

                  It seems correct but gives unexpected results. you can run the above code at...
                  http://www.odams.net/login/curl.php

                  Once again any help that you could provide would be greatly appreciated!!

                    By logging into the site for them using curl, you are in effect becoming a proxy. Any further request to that site will have to go through your site and your site and your site will also have to save session info (I think curl does this transparently however).

                    With regards to the code you are using, you are not passing the correct variables, I had a look at the form on this page and you should be sending the following post data:

                    loginName=username
                    password=password
                    login.forward.action=%2Findex.do
                    autologin=on

                    Make sure you send all the data, otherwise the serverside script their end will fail.

                      Write a Reply...