Frage

Hi there I have got a form and a list of records however I want to make it so when i click an item from the list the whole form will be populated. After a bit of research I have come up with this that populates the first field but want to know how to populate all fields

 <li> <a href=\"#\" onclick=\"document.addAssignment.fTitle.value='" . $row ["title"] . "'\" >

Here is my attempt to do it on two different fields

 <li> <a href=\"#\" onclick=\"document.addAssignment.fTitle.value='" . $row ["title"] . "' document.addAssignment.fModule.value='" . $row ["module"]."'\" >
War es hilfreich?

Lösung

document.addAssignment.fTitle.value='" . $row ["title"] . "' is a line of JavaScript that will populate fTitle with the value you give it. if you want to populate more fields, you can simple add more lines of JavaScript like so

document.addAssignment.fTitle.value='" . $row ["title"] . "';
document.addAssignment.fTitle2.value='" . $row ["title"] . "';
document.addAssignment.fTitle3.value='" . $row ["title"] . "';

Andere Tipps

In your example you only use JavaScript. Is this what you want to use? If so, your question is tagged wrong. You also do not specify how the values are stored.

However, you can do this through PHP and it's the route I would have taken.

<?php

if(isset($_GET['id']) && $_GET['id'] != '') {
    $id = $_GET['id'];
    $values = new ClassThatHandlesValues;
    $form_values = array(
        'form_id_1' => $values->get($id, 'form_id_1'),
        'form_id_2' => $values->get($id, 'form_id_1'),
    );
} else {
    $form_values = array(
        'form_id_1' => '',
        'form_id_2' => '',
    );

}

?>

<form>
    <input id="form_id_1" value="<?=$form_values['form_id_1'] ?>" type="text" />
    <input id="form_id_2" value="<?=$form_values['form_id_2'] ?>" type="text" />
</form>

Here, the class ClassThatHandlesValues might connect to a database or read a file or whatever. It gets the values somehow. If no ID has ben specified, a default array is defined. Here it's empty, but setting some default values might be useful.

Note: This reloads the page. If you don't want to reload the page, JavaSript is the answer and not PHP.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top