PrestaShop Product Saving Fails? The Hidden PHP Version Incompatibility Behind CSRF Errors

PrestaShop Product Saving Fails? The Hidden PHP Version Incompatibility Behind CSRF Errors

The "The CSRF token is invalid" error can be a major roadblock for PrestaShop merchants, especially when it prevents essential tasks like product management. This community insight from the PrestaShop forums reveals a common yet often overlooked culprit for such issues: PHP version incompatibility. We'll explore a specific case on PrestaShop 1.7.6.1 that highlights how a fundamental server-side mismatch can cascade into complex front-end and backend errors.

The Problem: Product Management Locked Down on PrestaShop 1.7.6.1

A merchant, Zana, operating PrestaShop version 1.7.6.1 with PHP 7.4.33, faced a critical challenge: the inability to add, delete, or amend products. Every attempt to save a product was met with the error message: "The CSRF token is invalid. Please try to resubmit the form." Additionally, product descriptions displayed raw HTML, indicating a potential issue with the rich text editor or content rendering.

Initial Troubleshooting & Deeper Investigation

Initial advice focused on common CSRF fixes: clearing browser cache and cookies, and checking server-side session paths. Zana diligently followed these, clearing browser data, restarting, and testing in incognito mode, but the errors persisted. Even after involving the hosting provider (SiteGround) to check server logs, session storage, and hosting cache, no clear server-level cause was found. Clearing PrestaShop's application cache and disabling CSS/JavaScript optimization also failed. However, SiteGround did report PHP compatibility warnings – a crucial detail.

Zana's thorough debugging with browser developer tools provided vital clues. The console reported a JavaScript error: Cannot read properties of undefined (reading 'split') originating from product form.js. This error coincided with a blank tax-included price field. The server response to the save request was an HTTP 400 error, reiterating the CSRF issue and listing "This value should not be blank" for essential fields like price and weight. Critically, a visual inspection of submitted form data revealed the absence of form[step2] fields and the security token, form[_token]. This strongly suggested the JavaScript error was preventing the complete product form and its security token from being properly submitted.

The Breakthrough: PHP Version Incompatibility

The turning point came with insights from community experts, Fred at PrestaHeroes and Knowband Plugins. They pinpointed the root cause: PrestaShop 1.7.6.1 is not compatible with PHP 7.4. The officially recommended PHP version for PrestaShop 1.7.6.x was PHP 7.2. Running an unsupported PHP version can lead to unpredictable behavior, including JavaScript execution failures, which in turn break form submissions and security token validation.

This incompatibility explained the entire chain of events: the incorrect PHP version caused the JavaScript error in product form.js, preventing the proper rendering and submission of critical form fields (like price) and the CSRF token. Without these, the server correctly rejected the incomplete and unverified form submission, resulting in the "CSRF token is invalid" and "value should not be blank" errors.

The Solution: Downgrade PHP and Clear Cache

The recommended action was clear and effective:

  1. Switch PHP Version: Access your hosting panel (e.g., SiteGround) and change the site's PHP version from 7.4 to 7.2.
  2. Clear PrestaShop Cache: Navigate to Advanced Parameters > Performance in the PrestaShop back office and clear the cache.
  3. Re-test: Attempt to save a product again.

This solution directly addresses the underlying compatibility issue, allowing PrestaShop's backend scripts and JavaScript to function as intended, ensuring all form fields and the CSRF token are correctly submitted.

Key Takeaway for PrestaShop Merchants & Developers

This case highlights the critical importance of adhering to PrestaShop's official PHP compatibility charts. While upgrading PHP is generally beneficial, doing so without verifying compatibility with your specific PrestaShop version can lead to severe and perplexing backend issues, including CSRF errors, broken forms, and display glitches. Always consult official documentation for recommended PHP versions to ensure stable PrestaShop operations.

Start with the tools

Explore migration tools

See options, compare methods, and pick the path that fits your store.

Explore migration tools