How to Connect ReactJS as a Front-end with PHP as a Back-end ?
Last Updated :
03 Apr, 2025
Connecting the front end of React with the PHP backend is a classic approach to building a web application with the front end for the user interface and the back end or server side for handling databases, programming logic, and other operations.
In this article, we’ll discuss the steps required to connect React with a PHP backend using API endpoints, HTTP requests (GET and POST), and asynchronous data transfer methods like Ajax, Fetch, or Axios.
Prerequisite:
Approach
To connect React with PHP, we'll use API endpoints and HTTP requests. Communication between the front end and back end will be handled through GET and POST requests. We will use asynchronous data transfer techniques, such as Ajax, Fetch, or Axios, to send and receive data between React and PHP.
Steps to Connect React with PHP
Step 1: Initialize React Project
Create a React application using the following command:
npx create-react-app folderName
Step 2: Move to the Project Directory
Once the project is created, navigate to the project directory using the following command:
cd folderName
Step 3: Setup PHP files
Now, you need to create the PHP files that will handle the server-side logic. Create a new folder named PHP in the root directory of your project. Inside the PHP folder, create a file named server.php.
Project Structure:

Step 4: Define React Component to Communicate
Next, you need to create a React component that will handle the user input and communicate with the PHP backend. For this example, we’ll create a simple form where a user can input their name, which will be sent to the PHP server. The server will then return a message, which will be displayed in the React app.
JavaScript
// Filename - App.js
import { useState } from "react";
import $ from "jquery";
import "./App.css";
function App() {
const [name, setName] = useState("");
const [result, setResult] = useState("");
const handleChange = (e) => {
setName(e.target.value);
};
const handleSubmit = (e) => {
e.preventDefault();
const form = $(e.target);
$.ajax({
type: "POST",
url: form.attr("action"),
data: form.serialize(),
success(data) {
setResult(data);
},
});
};
return (
<div className="App">
<form
action="http://localhost:8000/server.php"
method="post"
onSubmit={(event) => handleSubmit(event)}
>
<label htmlFor="name">Name: </label>
<input
type="text"
id="name"
name="name"
value={name}
onChange={(event) =>
handleChange(event)
}
/>
<br />
<button type="submit">Submit</button>
</form>
<h1>{result}</h1>
</div>
);
}
export default App;
Step 5: Install jQuery
Before using jQuery in your project, you must first install it. Run the following command in the project directory:
npm install jquery
This will install the jQuery library and make it available for use in your React application. Once installed, you can import it using:
import $ from "jquery";
Step 6: Start PHP Backend
Start your PHP server inside the PHP folder by going to the console and changing directory to the PHP folder, then run this command:
php -S localhost:8000
This will start the PHP server on localhost:8000.
Step 7: Create the PHP Server (server.php)
Now, you need to create the server.php file that will handle the data sent from React. The PHP file will accept the POST request and return a message.
PHP
// FileName - server.php
<?php
header('Access-Control-Allow-Origin: http://localhost:3000');
$user = $_POST['name'];
echo ("Hello from server: $user");
?>
Steps to run the application: Now check your website by running the command in the project directory:
npm start
Output: Now go to the browser window and type URL http://localhost:3000/
Output on submitting the formConclusion
By following these steps, you can easily connect a React front end with a PHP backend. This method uses HTTP requests (GET and POST), along with asynchronous data transfer using Ajax, Fetch, or Axios. You’ve learned how to create a React component, handle form data, send it to the PHP backend, and display the server’s response in the React app.
Similar Reads
How to Connect Django with Reactjs ?
Connecting Django with React is a common approach for building full-stack applications. Django is used to manage the backend, database, APIs and React handles the User Interface on frontend. Prerequisites:A development machine with any OS (Linux/Windows/Mac).Python 3 installed.Node.js installed (ver
9 min read
How to Connect Front End and Backend
"Connecting front-end to back-end is like building a bridge between worlds. How can you construct a pathway that seamlessly connects user interface with server functionality?"Let's dive into this in detail......Every successful web application is built on the synergy between how frontend interacts w
9 min read
How to Connect ReactJS with flask API ?
Connecting React with Flask API allows us to create a full-stack web application with React handling the user interface at frontend and Flask serving the data in backend. Here React makes the HTTP request to interact with the flask API to perform operations with the database. ApproachTo connect Reac
4 min read
How To Connect MongoDB with ReactJS?
MongoDB is a popular NoSQL database known for its scalability, flexibility, and high performance. When building modern web applications with ReactJS, itâs common to connect your frontend with a backend server that interacts with a database like MongoDB.PrerequisiteReact JSNode JSMongo DBApproach to
5 min read
How To Build a Basic CRUD App With Node and React ?
In this article, we will explore how to build a simple CRUD (Create, Read, Update, Delete) application using Node.js for the backend and React for the frontend. Additionally, we will integrate MongoDB as the database to store our data.Preview of final output:App functionalityCreate a new student (CR
11 min read
How To Connect Node with React?
To connect Node with React, we use React for the frontend (what users see) and Node.js for the backend (where the server logic lives). The frontend sends requests to the backend, and the backend responds with data or actions. There are many ways to connect React with Node.js, like using Axios or Fet
4 min read
How to connect ReactJS with MetaMask ?
To Connect React JS with MetaMask is important while making Web 3 applications, It will be done with Meta mask wallet which is the most used browser tool for sending and receiving signed transactions .MetaMask is a crypto wallet and a gateway to blockchain DAPP. It is used to connect our app to web3
3 min read
How to Fetch Data From an API in ReactJS?
ReactJS provides several ways to interact with APIs, allowing you to retrieve data from the server and display it in your application. In this article, weâll walk you through different methods to fetch data from an API in ReactJS, including using the built-in fetch method, axios, and managing the st
8 min read
How to send Basic Auth with Axios in React & Node ?
Basic Auth is a simple authentication scheme. It involves sending a username and password with each request to the server through HTTP. Axios is a popular HTTP client for making requests in JavaScript. In this article, we will create a React App to demonstrate sending basic Auth with Axios and discu
5 min read
How to view React App in a different devices ?
Usually, when we create a React App, we view it at localhost:3000 on the browser. It is also possible to view React App on mobile and other devices. It will be done with the approach mentioned below. Approach to view React App in different devices We can easily view the React App on different device
2 min read