How to Redirect and Send Data via POST in PHP Without an HTML Form?

DDD
Release: 2024-10-27 10:30:30
Original
145 people have browsed it

How to Redirect and Send Data via POST in PHP Without an HTML Form?

Redirecting and Sending Data via POST in PHP

The scenario involves the need to submit an HTML form with hidden fields to an online gateway through a PHP script, despite the absence of any HTML forms. The question arises regarding how to accomplish this using POST data transfer, as opposed to the GET method, which is demonstrated in the example provided.

Answer

Unfortunately, it is not possible to directly redirect and send data via POST using PHP alone. Unlike GET, which allows data to be appended to the target URL, POST data transmission requires an actual form submission.

Alternatives

  1. cURL: cURL can be employed to simulate a client and submit the form on the server-side. However, this approach transforms PHP into the client rather than the browser.
  2. Form Generation and JavaScript Submission:

    • Generate the HTML form dynamically using PHP, including the hidden fields with the necessary data.
    • Use the window.onload event in JavaScript to retrieve and submit the form automatically upon page load.

The above is the detailed content of How to Redirect and Send Data via POST in PHP Without an HTML Form?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!