I’m a junior developer and I wanted to know that how to enter text in multiple entries/fields/indexes in an array. Currently I’m doing a small project. So I am stuck in inputting the text repeatedly. Kindly help me in this regard. I can enter only one entry, i also tried to make use of classes but no luck.
<!-- Tic Tac Toe Game -->
<html>
<head>
<title>Tic Tac Toe</title>
<style>
body {
background-color: whitesmoke;
text-align: center;
}
#enteries{
border-radius: 50px;
border: 2px solid black;
padding: 50px;
width: 200px;
height: 15px;
margin-bottom: 20px;
margin-top: 20px;
margin-right: 20px;
font-size: 30px;
}
#done{
border-radius: 10px;
border: 2px solid black;
padding: 50px;
width: 160px;
height: 15px;
margin-bottom: 20px;
margin-top: 20px;
margin-right: 20px;
font-size: 15px;
}
</style>
</head>
<body>
<h1>Welcome! X player</h1>
<form name = "ticktactoe" method = "post" action = "practice.php">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<?php
$winner = 'tied';
$x = array('','','','','','','','','');
if (isset($_POST["gobtn"]))
{
$x[0] = $_POST["x0"];
$x[1] = $_POST["x1"];
$x[2] = $_POST["x2"];
$x[3] = $_POST["x3"];
$x[4] = $_POST["x4"];
$x[5] = $_POST["x5"];
$x[6] = $_POST["x6"];
$x[7] = $_POST["x7"];
$x[8] = $_POST["x8"];
if(($x[0] == 'x' && $x[1] == 'x' && $x[2] == 'x') || ($x[3] == 'x' && $x[4] == 'x' && $x[5] == 'x') || ($x[6] == 'x' && $x[7] == 'x' && $x[8] == 'x') || ($x[0] == 'x' && $x[3] == 'x' && $x[6] == 'x') || ($x[1] == 'x' && $x[4] == 'x' && $x[7] == 'x') || ($x[2] == 'x' && $x[5] == 'x' && $x[8] == 'x') || ($x[0] == 'x' && $x[4] == 'x' && $x[8] == 'x') || ($x[2] == 'x' && $x[4] == 'x' && $x[6] == 'x') )
{
$winner = 'x';
Print "<h1>X Wins!</h1>";
}
$blank = 0; //if the field is blank
for ($i = 0; $i <= 8 ; $i++)
{
if($x[$i] == '')
{
$blank = 1;
}
}
if($blank == 1) //if the field contains value
{
$i = rand() % 8;
while($x[$i] != '')
{
$i = rand() % 8;
}
$x[$i] = 'o';
if(($x[0] == 'o' && $x[1] == 'o' && $x[2] == 'o') || ($x[3] == 'o' && $x[4] == 'o' && $x[5] == 'o') || ($x[6] == 'o' && $x[7] == 'o' && $x[8] == 'o') || ($x[0] == 'o' && $x[3] == 'o' && $x[6] == 'o') || ($x[1] == 'o' && $x[4] == 'o' && $x[7] == 'o') || ($x[2] == 'o' && $x[5] == 'o' && $x[8] == 'o') || ($x[0] == 'o' && $x[4] == 'o' && $x[8] == 'o') || ($x[2] == 'o' && $x[4] == 'o' && $x[6] == 'o') )
{
$winner = 'o';
Print "<h1>O Wins!</h1>";
}
}
else if ($winner == 'tied')
{
$winner = 'draw';
Print "<h1>Match is tied</h1>";
}
}
for($i = 0; $i <=8; $i++)
{
printf('<input type="text" id = "enteries" name = "x%s" value = "%s">', $i, $x[$i]);
printf('<input type="radio" name="x" id="choose" value="x">');
printf('<button type="button" onclick="getvalue();">click</button>');
if ($i == 2 || $i == 5 || $i == 8){
print("<br>");
}
}
if($winner == 'tied')
{
//submitting
print('<input type = "submit" name = "gobtn" style="margin-left: 10px" value = "Enter" id = "done">');
}
else
{
//play again when the matched is finished
print('<input type = "button" name = "newgamebtn" value = "Play Again" id = "done" onclick = "window.location.href='practice.php'">');
}
?>
</form>
<script>
function getvalue(){
var values = document.getElementById("choose").value;
document.getElementById("enteries").value="x";
}
</script>
</body>
</html>