Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I do a POST the value of a png dice?

Tags:

html

php

I'm trying to get the value of a dice that is a .png file and with a form, send that value to another page.

Here is the function that generates the random img:

function dadoAleatorio(){

    $arr1=array('dado1.png','dado2.png','dado3.png','dado4.png','dado5.png','dado6.png');

    $numAleatorio = rand(0,5);

    echo '<div class="col-xs-2"><img class="dado" src="img/'.$arr1[$numAleatorio].'"></img></div>';

}

What I want to do here is to get the value of the random generated dice and, when I hit the submit button, automatically checks if the numbers written on the text fields match the random number of the dice.

<div class="container">    
<div class="row">
    <?php 

        //Esta función genera un numero aleatorio y asigna ese número a la url de la imagen.

        dadoAleatorio();
        dadoAleatorio();

    ?>

    <!-- Formulario en el que se comprueba el valor del dado con el del campo de texto -->

    <div class='container'>
    <div class='row'>
        <div class='col-xs-3'>
            <form role="form" action="resultado.php" method="post">
                  <div class="form-group">
                    <label for="text">Dado 1</label>
                    <input type="text" class="form-control" name="num1" id="text">
                  </div>

                  <div class="radio">
                    <label><input type="radio" name="signo"> + </label><br>
                    <label><input type="radio" name="signo"> - </label>

                  </div>

                  <div class="form-group">
                    <label for="text">Dado 2</label>
                    <input type="text" name="num2" class="form-control">
                  </div>
                  <button type="submit" class="btn btn-default">Enviar</button>
            </form>
        </div>
    </div>
    </div>

</div>

The problem comes when I try to get the value of the dice.

I hope I have explained well, this is my first time in Stackoverflow.

Thank you.

like image 876
hejuso Avatar asked Nov 24 '25 01:11

hejuso


1 Answers

The easiest here would be to use hidden inputs. Add this to your function:

echo '<input type="hidden" name="die[]" value="'.$numAleatorio.'">';

Then in PHP you'll have a $_POST['die'] array presumably with two entries since you call the function twice.

Though I prefer the above, an alternate would be to add this instead:

function dadoAleatorio($die){
    // your code 
    echo '<input type="hidden" name="die'.$die.'" value="'.$numAleatorio.'">';
}

And then call it like this:

dadoAleatorio(1);
dadoAleatorio(2);

Then you'll have a $_POST['die1'] and $_POST['die2'].

like image 158
AbraCadaver Avatar answered Nov 26 '25 14:11

AbraCadaver