input in PHP with fields filled

Asked

Viewed 116 times

1

Script in PHP.

My database has 3 fields:

id, name, telephone.

I need to update the data of 1 record.

On the first page, I ask for an input, the name of the record to change, and a "Search" button to locate the record to be changed.

On the second page I place the 2 fields for editing:

$nome_g1 = isset($_GET["nome"])?$_GET["nome"]:"";
$arma1   = isset($_GET["telefone"  ])?$_GET["telefone"  ]:"";

I would like the fields to be already filled with the data of the selected record.

Example:

1st page I typed "Antonio", and the script found the record with the name "Antonio" and the Telephone "99999.9999".

second page:

Name...: [Antony]

Tel.: [99999.9999]

Button: [update]

The question is: how the data appear in the fields as soon as it enters the second page, before typing, ie in the line of "input"

1 answer

1

Enough for a echo in PHP returning the value in the field value:

If it’s in the HTML part

Nome: <input name="nome" value="<?php echo htmlentities( $nome ); ?>">

or in the PHP part:

echo 'Nome: <input name="nome" value="'.htmlentities( $nome ).'">';

For fields like select:

<select name="estado">
   <option value="SP"<?php echo $estado=="SP"?' selected':''; ?>>

Same thing for radio and checkbox, only replace Selected with the appropriate HTML attribute.

Of course, if you are going to use several options in select, it may be more practical to use a separate function for this, but just understand the above logic.

  • It worked fine, but I couldn’t mount it to "select" and "radio": -- Status: <select name="state_"> <option value="MG" >Minas G</option> <option value="SP" >S paulo</option> <option value="RJ" >R Jan </option> </select> --- <fieldset><Legend> gun: </Legend> <input type="radio" name="state" value="MG" /><label for="MG">MG &nbsp &nbsp </label> <input type="radio" name="state" ="SP value" /><label for="SP">SP &nbsp &nbsp </label> <input type="radio" name="state" value="RJ" /><label for="RJ">RJ </label> </fieldset> ---

  • I added in the answer, but when it is so, you have to already put the details all right in the question.

  • I couldn’t assemble for radio and checkbox. I’m a freshman.

  • Ai uses checked instead of selected. Not that it’s a problem for me to tell you what to do, but if you learn to read HTML documentation and search, it’s much easier for you to learn (otherwise you’ll always be copying and pasting instead of actually programming)

  • That’s what else I’m doing!!!

Browser other questions tagged

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