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 change list items in ReactJS when an item is clicked ?
In ReactJS, changing items in the list when an item of the list is clicked can be done by triggering the event onClick() on the item that is currently clicked. For, reflecting the change, we also have to maintain the state in react so that after the change when the page is rendered again the changes
3 min read
How to use events in ReactJS ?
Modern webpages rely on user interactions, triggering events like clicks or keypresses. React facilitates event handling with built-in methods, allowing developers to create listeners for dynamic interfaces and responses. JavaScript events, inherent to the DOM, use bubbling propagation by default, m
2 min read
Where in a React component should you make an AJAX request ?
In most modern web applications, the back end is separated from the frontend. Therefore, it must fetch data from a remote endpoint(server), by making AJAX requests.Prerequisites:NodeJS or NPMReact JSAJAXApproach:Import React, useState, and useEffect:Import React along with the `useState` and `useEff
2 min read
How to Prevent Access to Admin Pages using Node.js and React.js ?
To prevent access to admin pages using Node.js and React.js is a common task to avoid unauthorized access. In many websites, the content available to the end-user is limited until he/she proves his/her authenticity by logging in. During the development of MERN Stack Applications, when the server is
6 min read
How to do CRUD operations in ReactJS ?
CRUD (Create, Read, Update, Delete) CRUD (Create, Read, Update, Delete) operations are fundamental for managing data in applications. In ReactJS, you can easily perform CRUD operations by manipulating local state, using forms for data input, and integrating with local storage for persistent data.In
9 min read
Build an Online Code Compiler using React.js and Node.js
In this article, we will learn how to build an online code compiler using React.js as frontend and Express.js as backend. Users will be able to write C, C++, Python, and Java code with proper syntax highlighting as well as compile and execute it online. The main objective of building an online compi
7 min read
Basic Registration and Login Form Using React Hook Form
In ReactJS, creating a form can be a nightmare as it involves handling inputs and validating inputs. React-hook-form is a ReactJS library that simplifies the process of creating forms.The library provides all the features that a modern form needs. It is simple, fast, and offers isolated re-renders f
6 min read
How to make async functions in PHP ?
In this article, we'll learn about the async function in PHP. Let us understand what is the meaning of Async (Asynchronous): A program does not need to be executed line by line. The program calls the function and moves further (does not wait for the function to return). It will execute in the backgr
4 min read
Explain lifecycle of ReactJS due to all call to this.setState() function
In React, the states are used to store data for specific components. These states can be updated accordingly using this.setState function. Updating of state leads to the rendering of UI. Different lifecycle methods are called during these renderings. Lifecycle methods called when the app is loaded f
2 min read
How to display values from database in real time without refreshing the webpage ?
Display values from database in real time without refreshing the webpage simply refers to auto fetch and re-rendering the data if there is any change due to user interaction or any update in the data from the APIs. In React JS, it can be done by using the states that re-render the UI on updates. Pre
3 min read