Form Dynamic

Asked

Viewed 202 times

2

Speak my friends night programmers, well my doubt is the following, I’m trying to fill an HTML form with random values, for this I’m using a array with predefined values and function rand() PHP. The problem is that the attribute value of input is showing the PHP code instead of using the value itself of the variable. 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 guys, thanks!

  • What is the? PHP or HTML file extension? does the first part of PHP also appear on the client side? or just what’s on value?

  • Hi Rafael, please mark one of the answers as accepted if she was the most important to resolve the problem. Thank you!

3 answers

2

You’re showing up like this?

inserir a descrição da imagem aqui

This appeared here when I open direct, other than with a php interpreter, should be why.

When I open it by shampooing...

inserir a descrição da imagem aqui

2


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

I would also like to take this opportunity to express my opinion. - Attention to poorly closed tags, you are not closing the fieldset and the form - I think you should be getting the array length 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>

1

Three possible problems:

  1. the extension is . html, change it to . php.
  2. you are accessing the file via protocol file: , you must access by localhost+path to the project (which should be inside htdocs if it is XAMPP).
  3. you do not have an http server and/or PHP interpreter installed, you can as a quick solution install XAMPP, WAMPP (these are applications that combine various technologies providing tools that facilitate the creation of dynamic websites , ie features that change the site according to user interaction.)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.