I would like to have a webpage post some form data to a page on a remote webserver which then inserts it into a MySQL database on the remote server and returns the Identity to my first script.

I can figure out how to post from the first page to the second page on the remote server and have that script handle the form data and insert it into the database and retrieve the ID of the last insert but how do I get this data back to the first page?

    OK so page A posts the data to page B running on the remote server. Page B inserts the data into the database and reads back the entity of th last insert - I'm just not sure how I then get that data back to page A - are you saying page B posts it back to page A?

      If the data is already in the mysql database, then why can you not just retrieve it from there? it sounds a bit complicated tring to post data to and from servers

        You may be interested in using web services for this.

          Sorry, I didn't explain my reasoning for doing it this way :o - The MySQL database is not accessible remotely so I can't simply read from and write to it from page A

          Page B running on the remote server can however access the database (as they are local to each other) so to get data into and read data out of the database I need to have page B do the work.

            After seeing your last addition to the thread. Web services might well be what you need.

            A web service is remotely accessibly code. So basically you can ask it for data and it will send it back. I'm assuming you have access to develop on the remote server too.

              Why not output the data as XML and read it? (edit: this is along similar lines as what dougal85 is talking about).

              I used to write php files that dumped stuff like this:

              user=blahblah;access=blahblah;date=blahblah;
              

              And then I would explode(';') the data. Not pretty, but worked.

              Nowadays, it's better to write something like this:

              <data>
              <user>blahblah</user>
              <access>blahblah</access>
              <date>blahblah</date>
              </data>
              

              See the xml functions for more information.

              I sure hope you are aware of the security risks, though. :-) (edit: dougal85 is on to something, as you can probably get most of the security/wrapper functions in a neat little package). Buzzwords for some services include "SOAP" "XML-RPC" and lately, XSLT.

                Thanks guys, I'll have a Google for SOAP etc. and see what I can find.

                  Web service is your best option, but if your looking for something quick and simple there are two ways you could do this (though they are somewhat hacks):

                  • I'm assuming you have PHP installed on server that hosts page A *

                  1.) Make sure register_globals = On in php.ini on server that hosts page A. Then just provide a link back to page A, something like this:

                  <a href="pageA.php?var1=$var1&var2=$var2">Link</a>

                  Then $var1 and $var2 will be available in page A.

                  2.) If you don't have register_globals you will need to use a form to post values:

                  <form action="pageA.php" method="POST">
                  <input field="hidden" name="var1" value="<?php echo $var1; ?>">
                  <input field="hidden" name="var2" value="<?php echo $var2; ?>">
                  </form>

                  Then in page A you can access variables by using $POST['var1'] and $POST['var2']

                  Again I think most developers here would cringe at those solutions but they are quick and easy if the scope of your project is pretty small.

                    NeoGeo wrote:

                    1.) Make sure register_globals = On in php.ini on server that hosts page A. Then just provide a link back to page A, something like this:

                    <a href="pageA.php?var1=$var1&var2=$var2">Link</a>

                    Then $var1 and $var2 will be available in page A.

                    Can I just add. Never use register_globals = on. This is depreciated and causes all sorts of security risks. You don't need register globals on for this 'hack' and you don't need to use post. Read more about register_globals here : http://uk.php.net/register_globals

                    if you want to get the variables out of the url. use $GET rather then $POST. Let me explain how to do that above method described by NeoGeo...

                    Pagea (local server) uses

                    $r = file_get_contents('www.remoteserver.com/pageb.php?id=something&info=somethingelse');

                    pageb reads those variables;

                    ie.

                    echo $_GET['id']; // displays 'something'

                    this page then can output some XML or JSON or whatever data encoding you want.

                    Then $r, in the example above contains the returned data to be processed.

                    Make sense? Note this has some security problems - so don't use this method if the data is not public anyway.

                    I'd still recommend web services. (They are very cool things and cross language compatible too. I'm writing one in c#.net 1.1 just now actually)

                      Thanks, some of the data might be sensitive so I'll avoid GET and have a look at web services I think 🙂

                        If the data is 'sensitive', then you'd want to consider SSL (POST'ing data isn't any more secure than sending it in the query string).

                          I've been thinking about that and will use SSL - I guess even though when you post the data it isn't in the query string it will still be transmitted in the clear unless I use SSL.

                            Write a Reply...