How can I save the contents of echo inside a variable? I have a php file and a js file with a function. I need to apply this function to a list containing what I have inside echo. So to do that I suppose I have to save the contents of echo inside a variable, and then add the variable to the list. My problem is that inside echo I am using html tag, so I can't define the variable first and then call echo on it. How can I do? This is the php file:
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>NomeSito</title>
<!-- Bootstrap core CSS -->
<link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<!-- my CSS -->
<link href="css/home.css" rel="stylesheet">
</head>
<body>
<?php
$db = mysqli_connect('localhost','root','','DBsito');
if (!$db)
{
die('Could not connect to database: ' . mysqli_error());
}
$db_select = mysqli_select_db($db, 'DBsito');
$username = $_SESSION['username'];
$categorie = mysqli_query($db, "SELECT * FROM categoria WHERE BINARY categoria.user_utente = BINARY '$username'");
?>
<div class="row">
<ul id="list">
<?php
/* fetch associative array */
while ($row = mysqli_fetch_assoc($categorie)) {
echo '
<li class="list-item"><a href="categoria.php?categoria='.urlencode($row["nome_categoria"]).'">
<img class="card-icone" shadow-lg p-3 mb-5 src="img/upload/'. $row["icona"] . '" >
</a>
</li>
';
}
?>
<script type="text/javascript" src="js/home.js">
</script>
</ul>
</div>
<?php
//SET SESSION VARIABLES
$_SESSION["username"] = $username;
?>
</body>
</html>
You can write:
const phpEcho = '<li class="list-item"><a href="categoria.php?categoria=<?=urlencode($row["nome_categoria"])?>" ><img class="card-icone" shadow-lg p-3 mb-5 src="img/upload/<?=$row["icona"] ?>" ></a></li>';
Then you call code inside function like:
document.getElementById("myList").innerHTML = phpEcho;
you have to replace myList with the Id of your list
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.