Redirection JavaScript + variables
Scipt complet
Première partie: La page
test-redirection-js.php
<a id="retour-test"></a>
<br> <br>
<h1>Formulaire redirection :</h1>
<?php
if (isset($_POST['Redirect_Test'])) {
// L'action est exécutée
// On récupère les $_POST du formulaire
$nom;$prenom;$age;
if(isset($_POST['nom'])){
$nom=$_POST['nom'];
}
if(isset($_POST['prenom'])){
$prenom=$_POST['prenom'];
}
if(isset($_POST['age'])){
$age=$_POST['age'];
}
// On vérifie le champ name="age" de l'input hidden, mais vous pourriez tout aussi bien effectuer ici une requête sur votre base.
// l'idée étant d'attribuer la valeur 1 à $Redir_OK si une condition est remplie
if($age == "ma-variable-age"){
$Redir_OK = 1;
}
?>
<script>
if(<?php echo $Redir_OK; ?>==1) {
location.href = "page-test-redirection.php?nom=<?php echo $nom; ?>&prenom=<?php echo $prenom; ?>";
// On redirige avec 2 variabbles
// Vous pouvez également rediriger vers un autre site
}
</script>
<?php
}
?>
<br />
<form method="post" action="">
<label for="nom" style="width: 50%;"><span style="color:#ff0000;">*</span> Nom</label>
<input name="nom" id="nom" type="text" style="margin-left:80px;width: 30%;" class="" value="<?php echo $nom; ?>" required="">
<br /> <br />
<label for="prenom" style="width: 50%;"><span style="color:#ff0000;">*</span> Prenom</label>
<input name="prenom" id="prenom" type="text" style="margin-left:56px;width: 30%;" class="" value="<?php echo $prenom; ?>" required="">
<input type="hidden" name="age" value="ma-variable-age" />
<!-- value pour la champ age peut bien entendu être une variable récupérée depuis une précédente requête -->
<br /> <br />
<input type="submit" id="submit" name="Redirect_Test" class="btn" value="Tester la Redirection">
</form>
<br />
<br />
Deuxiemme partie: La page
page-test-redirection.php
<?php
$recup_nom = $_GET['nom'];
$recup_prenom = $_GET['prenom'];
// Récupération de nos variables
?>
<h1>Vérification Redirection JavaScript</h1>
<br />
Récupération des variables transmises >>
<br /> <br />
<span style="width: 50%;"><span style="color:#ff0000;">*</span> Nom</span>
<span style="margin-left:80px;width: 30%;color: #69B417;" class=""><?php echo $recup_nom; ?></span>
<br /> <br />
<span style="width: 50%;"><span style="color:#ff0000;">*</span> Prenom</span>
<span style="margin-left:56px;width: 30%;color: #69B417;" class="" ><?php echo $recup_prenom; ?></span>
<br /> <br />
<a class="btn" href="<?php echo $_SERVER['HTTP_REFERER'] ; ?>#retour-test" title="Retour">Retour Page Tuto</a>
<br />
<br />