I am trying to initialize a variable called $btn with a value, and later on use that $btn value in some other php block of code. My code is the following (explaining right after what i want to achieve) :
<?php
session_start();
require '../../vendor/autoload.php';
$m = new MongoDB\Client("mongodb://127.0.0.1/");
$db = $m->users;
$collection = $db->usersInfo;
if(!isset($_SESSION['user'])){
header('Location: ../../home/website.php');
exit;
}
$btn = '';
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(isset($_POST[$btn])){
echo $btn;
echo "</script>alert($btn)</script>";
$query = $db->usersInfo;
$filter = array('_id'=>$_SESSION['user']);
$update = array('$addToSet'=>array('ctfs'=>$btn));
$query->updateOne($filter,$update);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Cyberdemia</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
<!-- font awesome -->
<script src="https://kit.fontawesome.com/dbed6b6114.js" crossorigin="anonymous"></script>
</head>
<body>
<div class="toggle"></div>
<div class = "products">
<nav>
<ul>
<!--Will edit these in the future to get styles such as on-hover etc.-->
<li><a href="../ctfs.php"><small>Back</small></a></li>
<li><a href="../../home/website.php"><small>Home</small></a></li>
<li><a href="../../userProfile/userProfile.php"><small>Profile</small></a></li>
<li><a href="../../signOut/signout.php"><small>Sign out</small></a></li>
</ul>
</nav>
<div class = "container">
<h1 class = "lg-title">Welcome to the Reverse Engineering challenges</h1>
<div class = "product-items">
<?php
$db = $m->users;
$collection = $db->usersInfo;
$collectionAdmin = $db->adminInfo;
$query = $db->adminInfo->findOne(array('email'=>'[email protected]'));
if($query["timesLogged"] == "0"){
$db = $m->ctfs;
$collectionCtfs = $db->ctfsReverse->find();
foreach ($collectionCtfs as $chall){
$btn = $chall["challName"];
#echo $btn;
echo '<div class = "product">
<div class = "product-content">
<div class = "product-img">
<img src = "images/rev.png" alt = "product image">
</div>
<div class = "product-btns">
<form action="rev.php" method="post">
<button type="submit" name="'.$btn.'" value="'.$btn.'" class = "btn-cart"> play later list
<span><i class = "fas fa-plus"></i></span>
</button>
</form>
<button type = "button" class = "btn-buy"> Play now
<span><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-controller" viewBox="0 0 16 16">
<path d="M11.5 6.027a.5.5 0 1 1-1 0 .5.5 0 0 1 1 0zm-1.5 1.5a.5.5 0 1 0 0-1 .5.5 0 0 0 0 1zm2.5-.5a.5.5 0 1 1-1 0 .5.5 0 0 1 1 0zm-1.5 1.5a.5.5 0 1 0 0-1 .5.5 0 0 0 0 1zm-6.5-3h1v1h1v1h-1v1h-1v-1h-1v-1h1v-1z"/>
<path d="M3.051 3.26a.5.5 0 0 1 .354-.613l1.932-.518a.5.5 0 0 1 .62.39c.655-.079 1.35-.117 2.043-.117.72 0 1.443.041 2.12.126a.5.5 0 0 1 .622-.399l1.932.518a.5.5 0 0 1 .306.729c.14.09.266.19.373.297.408.408.78 1.05 1.095 1.772.32.733.599 1.591.805 2.466.206.875.34 1.78.364 2.606.024.816-.059 1.602-.328 2.21a1.42 1.42 0 0 1-1.445.83c-.636-.067-1.115-.394-1.513-.773-.245-.232-.496-.526-.739-.808-.126-.148-.25-.292-.368-.423-.728-.804-1.597-1.527-3.224-1.527-1.627 0-2.496.723-3.224 1.527-.119.131-.242.275-.368.423-.243.282-.494.575-.739.808-.398.38-.877.706-1.513.773a1.42 1.42 0 0 1-1.445-.83c-.27-.608-.352-1.395-.329-2.21.024-.826.16-1.73.365-2.606.206-.875.486-1.733.805-2.466.315-.722.687-1.364 1.094-1.772a2.34 2.34 0 0 1 .433-.335.504.504 0 0 1-.028-.079zm2.036.412c-.877.185-1.469.443-1.733.708-.276.276-.587.783-.885 1.465a13.748 13.748 0 0 0-.748 2.295 12.351 12.351 0 0 0-.339 2.406c-.022.755.062 1.368.243 1.776a.42.42 0 0 0 .426.24c.327-.034.61-.199.929-.502.212-.202.4-.423.615-.674.133-.156.276-.323.44-.504C4.861 9.969 5.978 9.027 8 9.027s3.139.942 3.965 1.855c.164.181.307.348.44.504.214.251.403.472.615.674.318.303.601.468.929.503a.42.42 0 0 0 .426-.241c.18-.408.265-1.02.243-1.776a12.354 12.354 0 0 0-.339-2.406 13.753 13.753 0 0 0-.748-2.295c-.298-.682-.61-1.19-.885-1.465-.264-.265-.856-.523-1.733-.708-.85-.179-1.877-.27-2.913-.27-1.036 0-2.063.091-2.913.27z"/>
</svg></span>
</button>
</div>
</div>
<div class = "product-info">
<div class = "product-info-top">
<h2 class = "sm-title">Ratings</h2>
<div class = "rating">
<span><i class = "fas fa-star"></i></span>
<span><i class = "fas fa-star"></i></span>
<span><i class = "fas fa-star"></i></span>
<span><i class = "fas fa-star"></i></span>
<span><i class = "far fa-star"></i></span>
</div>
</div>
<hr>
<a href = "#" class = "product-name">'.$chall["challName"].'</a>
<hr>
<a href = "#" class = "product-name">'.$chall["difficulty"].'</a>
</div>
</div>';
}
}
?>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.1.js"></script>
<script src="script.js"></script>
</body>
</html>
In the first php block of code i am connecting to a mongodb i am using. In this db, there are is a collection called ctfsReverse and usersInfo . What i am doing is giving the value of each challenge name to the variable $btn :
foreach ($collectionCtfs as $chall){
$btn = $chall["challName"];
Then i use this variable as an html attribute in the value and name fields :
<form action="rev.php" method="post">
<button type="submit" name="'.$btn.'" value="'.$btn.'" class = "btn-cart"> play later list
<span><i class = "fas fa-plus"></i></span>
</button>
</form>
So as you can see in the first block of my php code, i want to check for a post request, and if this post request was due to a button being pressed (where the buttons value is actually the $btn contents) i want to store in my db the name of the button that was pressed :
$query = $db->usersInfo;
$filter = array('_id'=>$_SESSION['user']);
$update = array('$addToSet'=>array('ctfs'=>$btn));
$query->updateOne($filter,$update);
But the $btn variable seems to not be visible to the first php block of code. When i press the button, the $btn value does not get stored in the database.
How can i make the $btn globally defined? I tried using php $GLOBALS but it didnt work. I even tried checking for button being pressed in the second php block of code, but this didnt work either :
<div class = "container">
<h1 class = "lg-title">Welcome to the Reverse Engineering challenges</h1>
<div class = "product-items">
<?php
$db = $m->users;
$collection = $db->usersInfo;
$collectionAdmin = $db->adminInfo;
$query = $db->adminInfo->findOne(array('email'=>'[email protected]'));
if($query["timesLogged"] == "0"){
$db = $m->ctfs;
$collectionCtfs = $db->ctfsReverse->find();
foreach ($collectionCtfs as $chall){
$btn = $chall["challName"];
#echo $btn;
echo '<div class = "product">
<div class = "product-content">
<div class = "product-img">
<img src = "images/rev.png" alt = "product image">
</div>
<div class = "product-btns">
<form action="rev.php" method="post">
<button type="submit" name="'.$btn.'" value="'.$btn.'" class = "btn-cart"> play later list
<span><i class = "fas fa-plus"></i></span>
</button>
</form>
<button type = "button" class = "btn-buy"> Play now
<span><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-controller" viewBox="0 0 16 16">
<path d="M11.5 6.027a.5.5 0 1 1-1 0 .5.5 0 0 1 1 0zm-1.5 1.5a.5.5 0 1 0 0-1 .5.5 0 0 0 0 1zm2.5-.5a.5.5 0 1 1-1 0 .5.5 0 0 1 1 0zm-1.5 1.5a.5.5 0 1 0 0-1 .5.5 0 0 0 0 1zm-6.5-3h1v1h1v1h-1v1h-1v-1h-1v-1h1v-1z"/>
<path d="M3.051 3.26a.5.5 0 0 1 .354-.613l1.932-.518a.5.5 0 0 1 .62.39c.655-.079 1.35-.117 2.043-.117.72 0 1.443.041 2.12.126a.5.5 0 0 1 .622-.399l1.932.518a.5.5 0 0 1 .306.729c.14.09.266.19.373.297.408.408.78 1.05 1.095 1.772.32.733.599 1.591.805 2.466.206.875.34 1.78.364 2.606.024.816-.059 1.602-.328 2.21a1.42 1.42 0 0 1-1.445.83c-.636-.067-1.115-.394-1.513-.773-.245-.232-.496-.526-.739-.808-.126-.148-.25-.292-.368-.423-.728-.804-1.597-1.527-3.224-1.527-1.627 0-2.496.723-3.224 1.527-.119.131-.242.275-.368.423-.243.282-.494.575-.739.808-.398.38-.877.706-1.513.773a1.42 1.42 0 0 1-1.445-.83c-.27-.608-.352-1.395-.329-2.21.024-.826.16-1.73.365-2.606.206-.875.486-1.733.805-2.466.315-.722.687-1.364 1.094-1.772a2.34 2.34 0 0 1 .433-.335.504.504 0 0 1-.028-.079zm2.036.412c-.877.185-1.469.443-1.733.708-.276.276-.587.783-.885 1.465a13.748 13.748 0 0 0-.748 2.295 12.351 12.351 0 0 0-.339 2.406c-.022.755.062 1.368.243 1.776a.42.42 0 0 0 .426.24c.327-.034.61-.199.929-.502.212-.202.4-.423.615-.674.133-.156.276-.323.44-.504C4.861 9.969 5.978 9.027 8 9.027s3.139.942 3.965 1.855c.164.181.307.348.44.504.214.251.403.472.615.674.318.303.601.468.929.503a.42.42 0 0 0 .426-.241c.18-.408.265-1.02.243-1.776a12.354 12.354 0 0 0-.339-2.406 13.753 13.753 0 0 0-.748-2.295c-.298-.682-.61-1.19-.885-1.465-.264-.265-.856-.523-1.733-.708-.85-.179-1.877-.27-2.913-.27-1.036 0-2.063.091-2.913.27z"/>
</svg></span>
</button>
</div>
</div>
<div class = "product-info">
<div class = "product-info-top">
<h2 class = "sm-title">Ratings</h2>
<div class = "rating">
<span><i class = "fas fa-star"></i></span>
<span><i class = "fas fa-star"></i></span>
<span><i class = "fas fa-star"></i></span>
<span><i class = "fas fa-star"></i></span>
<span><i class = "far fa-star"></i></span>
</div>
</div>
<hr>
<a href = "#" class = "product-name">'.$chall["challName"].'</a>
<hr>
<a href = "#" class = "product-name">'.$chall["difficulty"].'</a>
</div>
</div>';
}
}
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(isset($_POST[$btn])){
echo $btn;
echo "</script>alert($btn)</script>";
$query = $db->usersInfo;
$filter = array('_id'=>$_SESSION['user']);
$update = array('$addToSet'=>array('ctfs'=>$btn));
$query->updateOne($filter,$update);
}
}
?>
</div>
</div>
</div>
Edit : Also, if i try to "View page source", the name and value attributes get the correct values :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Cyberdemia</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
<!-- font awesome -->
<script src="https://kit.fontawesome.com/dbed6b6114.js" crossorigin="anonymous"></script>
</head>
<body>
<div class="toggle"></div>
<div class = "products">
<nav>
<ul>
<!--Will edit these in the future to get styles such as on-hover etc.-->
<li><a href="../ctfs.php"><small>Back</small></a></li>
<li><a href="../../home/website.php"><small>Home</small></a></li>
<li><a href="../../userProfile/userProfile.php"><small>Profile</small></a></li>
<li><a href="../../signOut/signout.php"><small>Sign out</small></a></li>
</ul>
</nav>
<div class = "container">
<h1 class = "lg-title">Welcome to the Reverse Engineering challenges</h1>
<div class = "product-items">
<div class = "product">
<div class = "product-content">
<div class = "product-img">
<img src = "images/rev.png" alt = "product image">
</div>
<div class = "product-btns">
<form action="rev.php" method="post">
<button type="submit" name="rev level 1" value="rev level 1" class = "btn-cart"> play later list
<span><i class = "fas fa-plus"></i></span>
</button>
</form>
<button type = "button" class = "btn-buy"> Play now
<span><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-controller" viewBox="0 0 16 16">
<path d="M11.5 6.027a.5.5 0 1 1-1 0 .5.5 0 0 1 1 0zm-1.5 1.5a.5.5 0 1 0 0-1 .5.5 0 0 0 0 1zm2.5-.5a.5.5 0 1 1-1 0 .5.5 0 0 1 1 0zm-1.5 1.5a.5.5 0 1 0 0-1 .5.5 0 0 0 0 1zm-6.5-3h1v1h1v1h-1v1h-1v-1h-1v-1h1v-1z"/>
<path d="M3.051 3.26a.5.5 0 0 1 .354-.613l1.932-.518a.5.5 0 0 1 .62.39c.655-.079 1.35-.117 2.043-.117.72 0 1.443.041 2.12.126a.5.5 0 0 1 .622-.399l1.932.518a.5.5 0 0 1 .306.729c.14.09.266.19.373.297.408.408.78 1.05 1.095 1.772.32.733.599 1.591.805 2.466.206.875.34 1.78.364 2.606.024.816-.059 1.602-.328 2.21a1.42 1.42 0 0 1-1.445.83c-.636-.067-1.115-.394-1.513-.773-.245-.232-.496-.526-.739-.808-.126-.148-.25-.292-.368-.423-.728-.804-1.597-1.527-3.224-1.527-1.627 0-2.496.723-3.224 1.527-.119.131-.242.275-.368.423-.243.282-.494.575-.739.808-.398.38-.877.706-1.513.773a1.42 1.42 0 0 1-1.445-.83c-.27-.608-.352-1.395-.329-2.21.024-.826.16-1.73.365-2.606.206-.875.486-1.733.805-2.466.315-.722.687-1.364 1.094-1.772a2.34 2.34 0 0 1 .433-.335.504.504 0 0 1-.028-.079zm2.036.412c-.877.185-1.469.443-1.733.708-.276.276-.587.783-.885 1.465a13.748 13.748 0 0 0-.748 2.295 12.351 12.351 0 0 0-.339 2.406c-.022.755.062 1.368.243 1.776a.42.42 0 0 0 .426.24c.327-.034.61-.199.929-.502.212-.202.4-.423.615-.674.133-.156.276-.323.44-.504C4.861 9.969 5.978 9.027 8 9.027s3.139.942 3.965 1.855c.164.181.307.348.44.504.214.251.403.472.615.674.318.303.601.468.929.503a.42.42 0 0 0 .426-.241c.18-.408.265-1.02.243-1.776a12.354 12.354 0 0 0-.339-2.406 13.753 13.753 0 0 0-.748-2.295c-.298-.682-.61-1.19-.885-1.465-.264-.265-.856-.523-1.733-.708-.85-.179-1.877-.27-2.913-.27-1.036 0-2.063.091-2.913.27z"/>
</svg></span>
</button>
</div>
</div>
<div class = "product-info">
<div class = "product-info-top">
<h2 class = "sm-title">Ratings</h2>
<div class = "rating">
<span><i class = "fas fa-star"></i></span>
<span><i class = "fas fa-star"></i></span>
<span><i class = "fas fa-star"></i></span>
<span><i class = "fas fa-star"></i></span>
<span><i class = "far fa-star"></i></span>
</div>
</div>
<hr>
<a href = "#" class = "product-name">rev level 1</a>
<hr>
<a href = "#" class = "product-name">difficulty - easy</a>
</div>
</div><div class = "product">
<div class = "product-content">
<div class = "product-img">
<img src = "images/rev.png" alt = "product image">
</div>
<div class = "product-btns">
<form action="rev.php" method="post">
<button type="submit" name="rev level 2" value="rev level 2" class = "btn-cart"> play later list
<span><i class = "fas fa-plus"></i></span>
</button>
</form>
<button type = "button" class = "btn-buy"> Play now
<span><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-controller" viewBox="0 0 16 16">
<path d="M11.5 6.027a.5.5 0 1 1-1 0 .5.5 0 0 1 1 0zm-1.5 1.5a.5.5 0 1 0 0-1 .5.5 0 0 0 0 1zm2.5-.5a.5.5 0 1 1-1 0 .5.5 0 0 1 1 0zm-1.5 1.5a.5.5 0 1 0 0-1 .5.5 0 0 0 0 1zm-6.5-3h1v1h1v1h-1v1h-1v-1h-1v-1h1v-1z"/>
<path d="M3.051 3.26a.5.5 0 0 1 .354-.613l1.932-.518a.5.5 0 0 1 .62.39c.655-.079 1.35-.117 2.043-.117.72 0 1.443.041 2.12.126a.5.5 0 0 1 .622-.399l1.932.518a.5.5 0 0 1 .306.729c.14.09.266.19.373.297.408.408.78 1.05 1.095 1.772.32.733.599 1.591.805 2.466.206.875.34 1.78.364 2.606.024.816-.059 1.602-.328 2.21a1.42 1.42 0 0 1-1.445.83c-.636-.067-1.115-.394-1.513-.773-.245-.232-.496-.526-.739-.808-.126-.148-.25-.292-.368-.423-.728-.804-1.597-1.527-3.224-1.527-1.627 0-2.496.723-3.224 1.527-.119.131-.242.275-.368.423-.243.282-.494.575-.739.808-.398.38-.877.706-1.513.773a1.42 1.42 0 0 1-1.445-.83c-.27-.608-.352-1.395-.329-2.21.024-.826.16-1.73.365-2.606.206-.875.486-1.733.805-2.466.315-.722.687-1.364 1.094-1.772a2.34 2.34 0 0 1 .433-.335.504.504 0 0 1-.028-.079zm2.036.412c-.877.185-1.469.443-1.733.708-.276.276-.587.783-.885 1.465a13.748 13.748 0 0 0-.748 2.295 12.351 12.351 0 0 0-.339 2.406c-.022.755.062 1.368.243 1.776a.42.42 0 0 0 .426.24c.327-.034.61-.199.929-.502.212-.202.4-.423.615-.674.133-.156.276-.323.44-.504C4.861 9.969 5.978 9.027 8 9.027s3.139.942 3.965 1.855c.164.181.307.348.44.504.214.251.403.472.615.674.318.303.601.468.929.503a.42.42 0 0 0 .426-.241c.18-.408.265-1.02.243-1.776a12.354 12.354 0 0 0-.339-2.406 13.753 13.753 0 0 0-.748-2.295c-.298-.682-.61-1.19-.885-1.465-.264-.265-.856-.523-1.733-.708-.85-.179-1.877-.27-2.913-.27-1.036 0-2.063.091-2.913.27z"/>
</svg></span>
</button>
</div>
</div>
<div class = "product-info">
<div class = "product-info-top">
<h2 class = "sm-title">Ratings</h2>
<div class = "rating">
<span><i class = "fas fa-star"></i></span>
<span><i class = "fas fa-star"></i></span>
<span><i class = "fas fa-star"></i></span>
<span><i class = "fas fa-star"></i></span>
<span><i class = "far fa-star"></i></span>
</div>
</div>
<hr>
<a href = "#" class = "product-name">rev level 2</a>
<hr>
<a href = "#" class = "product-name">difficulty - medium</a>
</div>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.1.js"></script>
<script src="script.js"></script>
</body>
</html>