Dynamic Form

2
I'm trying to fill out an HTML form with random values, so I'm using a array with predefined values and the rand() function of PHP. The problem is that the value attribute of input is showing PHP code instead of using the variable's value itself. The code is as follows:

<?php

$randomName = Array("Alice", "Thor", "Pereirinha", "Golias", "Poseidom", "Morpheu", "Titio avo");
$randomValue = rand(0,6);
    $nome=  $randomName[$randomValue];
    echo $nome;

    ?>  
<form name="form1" action="registrarusuarios.php" method="post">
        <fieldset>
            <legend>Dados Pessoais</legend>

    <!-- label: for se refere ao id do input -->
    <label for="nome">Nome:</label>
    <input type="text" name="nome" id="nome" class="texto"  value="<?php echo $nome; ?>"  /><br/>

Well, that's it folks, thanks!

    
asked by anonymous 04.05.2015 / 05:31

3 answers

2

I copied your code and it works on my machine. Check if your file exemption is correct and you are opening the file correctly.

I also take this opportunity to give my opinion. - Attention to badly closed tags, you are not closing the fieldset and the form - I think you should be looking for the length of the array to make the "thing" more scalable.

ex.:

<?php

$randomName = Array("Alice", "Thor", "Pereirinha", "Golias", "Poseidom", "Morpheu", "Titio avo");

// Get array lenght
$randomName_len = count($randomName) - 1;

$randomValue = rand(0, $randomName_len);
$nome=  $randomName[$randomValue];
echo $nome;

?>  
<form name="form1" action="registrarusuarios.php" method="post">
    <fieldset>
        <legend>Dados Pessoais</legend>
        <!-- label: for se refere ao id do input -->
        <label for="nome">Nome:</label>
        <input type="text" name="nome" id="nome" class="texto"  value="<?php echo $nome; ?>"  /><br/>
    </fieldset>
</form>
    
04.05.2015 / 12:26
2

Are you looking like this?

ThisappearedherewhenIopenitdirectly,withoutbeingwithaphpinterpreter,thatshouldbewhy.

WhenIopenitbyxampp...

    
04.05.2015 / 11:44
1

Three possible problems:

  • extension is .html, change it to .php.
  • you are accessing the file via file: \\ protocol, you must access the localhost + path to the project (which must be inside of htdocs if it is XAMPP).
  • You do not have an http server and / or PHP interpreter installed, you can quickly install XAMPP, WAMPP (this are applications that combine several technologies providing tools that facilitate the creation of dynamic websites, that is, features that change the site based on user interaction.)
  • 04.05.2015 / 12:37