1

Alright, so I've looked at a ton of questions, but I only found 1 that resembled what I am trying to do. Here is the link to it: Passing POST data from one web page to another with PHP

I want to pass data from one PHP file(we'll call it editData.php) to another PHP file(we'll call it submitData.php). Neither file has any HTML elements (pure PHP code I mean). The first file(editData.php) receives $_POST data, edits it, and needs to send it to the second file. The second file(submitData.php) needs to be able to read in the data using $_POST. No sessions nor cookies can be used I'm afraid.

In the linked question above, the answer accepted was to create hidden fields inside a form and POST the data from there. This worked for the OP because he had user interaction on his "editData.php", so when the user wanted to go to "submitData.php", he would POST the data then.

I can't use this solution(at least, I don't think I can), because I am accessing (and sending $_POST data to) editData.php from a javascript AJAX call and there will be no user interaction on this page. I need the modified data to be POSTed by code, or some other way that does the transfer 'automatically'(or 'behinid-the-scenes' or whatever you want to call it). submitData.php will be called right after editData.php.

I don't know if I can rewrite submitData.php to accept GET data, so count that out as well (it's a matter of being able to access the file). I really don't want to echo stuff back to my original JavaScript function(and then AJAX again). I am encrypting info in editData.php, and (while it sounds silly to say it) I don't want to make it easy for someone to develop a cipher for my encryption. Returning values after being encrypted(viewable with Inspect Element) would make it too easy to decipher if you ask me.

I feel like this issue could come up a lot, so I'd expect that there is something obvious I'm missing. If so, please tell me.

tl;dr? How can I send data to a PHP file via the POST method while only using code in another PHP file?

11
  • 1
    You could use PHP curl. Commented Jul 18, 2012 at 19:52
  • Do I need to install anything for that? Someone told me it was an external library. Commented Jul 18, 2012 at 19:53
  • 1
    Do they have to be seperate PHP files? AFAIK you may end up running into AJAX problems as it will be requesting a specific PHP file; and the data is going to be returned from another. (In other words, I'm not sure if XMLHttpRequest will work with one PHP file calling another which will actually return the data; it's not liking working with a browser that can be redirected) Commented Jul 18, 2012 at 19:53
  • Yes, they have to be separate files. I don't need to return anything from the files, so don't worry about AJAX getting incorrect responses. Commented Jul 18, 2012 at 19:55
  • 1
    @jeroen Using "include(submitData.php);" doesn't send POST data. Commented Jul 18, 2012 at 20:01

1 Answer 1

4

Well you might consider just streamlining your approach and including the submitData logic at the end of the editData file. But assuming that this is not possible for some reason (files live on different systems, or whatver), your best bet might be to use cURL functionality to post the data to the second script.

If the files are on the same server though I would highly recommend not posting the data to the second script as this will basically just double the amount of requests your web server needs to handle related to this script.

Sign up to request clarification or add additional context in comments.

11 Comments

+1 Your code should be modular so that you can include the logic from submitData.php easily on other pages like editData.php.
@Xeoncross My submitData.php is usually called via JS AJAX, but in this instance I need it accessible from a PHP file.
@user1454408, and that is my point. Your designed the system without proper separation of components which now places you in this tough situation where you can't easily use logic from one place in another place.
Yes. But with good design, you could have all your logic for submitting data in its own include file which could simply be included at the end of editData.php or submitData.php to due the submitting. In other words if AJAX call to editData.php occurs, it reads the posted data, does whatever it needs to do, and then executes the submit include file. If the AJAX call goes directly to submitData.php, then that files reads the posted data and does whatever it needs to do before also include the submit include file. Same functionality used from two different places.
I actually inherited this system from some guy who quit. Trust me, I'd redesign it if I could. I would like to do what you are describing Mike, just having some trouble getting it to do that. Say that I did include a submitting file at the end of "editData.php", how would you suggest I pass my data to it?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.