Bonjour, je teste actuellement AMP Form.

J'ai donc un fichier FORM.PHP à la racine de mon site /

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!doctype html>
<html amp lang="fr">
<head>
    <title>Test formulaire AMP</title>
	<meta charset="utf-8" />
	<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
	<link rel="canonical" href="/form">
	<script async src="https://cdn.ampproject.org/v0.js"></script>
	<script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
	<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>
	<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
</head>
<body>
<form method="post" action-xhr="https://MONSITE.COM/form-xhr.php" target="_blank">
	<p><label for="email">Un texte : </label><input type="text" name="name" value="" required></p>
	<p><label> </label><input type="submit" value="Envoyer"></p>
 
	<div submit-success>
		<template type="amp-mustache">Le texte renvoyé par PHP : {{name}}</template>
	</div>
	<div submit-error>
		<template type="amp-mustache">Erreur !</template>
	</div>
</form>
</body>
</html>
et un fichier FORM-XHR.php également à la racine de mon site

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: https://'.$_SERVER['HTTP_HOST']);
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin');
header('AMP-Access-Control-Allow-Source-Origin: https://'.$_SERVER['HTTP_HOST']);
 
$data = array();
$data['name'] = $_POST['name'];
echo json_encode($data);
?>
Super Tout Fonctionne bien à la RACINE du site.

Mais comme je développe mon site non pas à la racine mais en amont de la racine
/Code
/Public (racine du site)

Je voudrais remonter FORM-XHR.php dans /Code
Il faut donc que je redirige https://MONSITE.COM/form-xhr.php vers /Code/form-xhr.php

Avec .htaccess je fais tout pointer vers index.php (précontrolleur).

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
<IfModule mod_rewrite.c>
# Active la réécriture d'URL :
	RewriteEngine On
# Si le fichier demandé n'existe pas :
	RewriteCond %{REQUEST_FILENAME} !-f
# Si le dossier demandé n'existe pas non plus :
	RewriteCond %{REQUEST_FILENAME} !-d
# On redirige vers index.php
	RewriteRule ^(.*)$ index.php/$1 [QSA,L]
</IfModule>

J'ai donc un fichier index.php à la racine

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
<?php
if( $_SERVER['REQUEST_URI'] === '/form') {
	echo 'Ma Page<br>';
	echo 'Mon Adresse : ' . $_SERVER['REQUEST_URI'] . '<br>';
	require_once dirname( __DIR__ ) . '/Code/form.php';
}
if( $_SERVER['REQUEST_URI'] === '/form-xhr') {
	require_once dirname( __DIR__ ) . '/Code/form-xhr.php';
}
if( $_SERVER['REQUEST_URI'] !== '/form' && $_SERVER['REQUEST_URI'] !== '/form-xhr' ) {
	echo 'En Construction';
}
puis un FORM.PHP le même qu'avant mais en supprimant .php au niveau de action-xhr="https://MONSITE.COM/form-xhr"

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!doctype html>
<html amp lang="fr">
<head>
    <title>Test formulaire AMP</title>
	<meta charset="utf-8" />
	<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
	<link rel="canonical" href="/form">
	<script async src="https://cdn.ampproject.org/v0.js"></script>
	<script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
	<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>
	<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
</head>
<body>
<form method="post" action-xhr="https://MONSITE.COM/form-xhr" target="_blank">
	<p><label for="email">Un texte : </label><input type="text" name="name" value="" required></p>
	<p><label> </label><input type="submit" value="Envoyer"></p>
 
	<div submit-success>
		<template type="amp-mustache">Le texte renvoyé par PHP : {{name}}</template>
	</div>
	<div submit-error>
		<template type="amp-mustache">Erreur !</template>
	</div>
</form>
</body>
</html>

et FORM-XHR.php qui se trouve maintenant dans /Code (avant la racine) mais le code n'a pas changé.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: https://'.$_SERVER['HTTP_HOST']);
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin');
header('AMP-Access-Control-Allow-Source-Origin: https://'.$_SERVER['HTTP_HOST']);
 
$data = array();
$data['name'] = $_POST['name'];
echo json_encode($data);
?>
MAIS LA CELA NE FONCTIONNE PLUS !!!

Pour résumer le script de départ fonctionne si FORM.php et FORM-XHR.php sont à la racine du site (/public)

L'objectif est de les remonter dans /Code avant la racine en passant par un index.php point d'entrée unique.

Si je tape /form-xhr dans l'url cela génère bien directement le fichier json (vide car appel direct mais qui démontre que index.php fait bien son travail) parcontre form action-xhr="https://MONSITE.COM/form-xhr" n'arrive pas à récupérer le fichier json valide.

Je n'arrive pas à comprendre la raison