简体   繁体   中英

Autofill a HTML form with database entries

I want to autofill two input elements bi clicking on a link. Here is the code I'm using:

 <script>

    function autoFill(depart_from, depart_to) {
        document.getElementById('depart_from').value = depart_from;
        document.getElementById('depart_to').value = depart_to;
    }

 </script>

And here is the link:

...

echo "<tr class='odd'>";
echo "<td><span class='hover'><ahref='#'onClick='autoFill(" . $row['departure'] . ", " . $row['destination'] . "return false;'>" . $row['departure'] ." » ". $row['destination'] . "</a><span></td>";
echo "</tr>";

Can't get it to work? What am I doing wrong?

Thanx!

For starters, the spacing is broken in your anchor tag:

<ahref='#'onClick='autoFill(

should be:

<a href='#' onClick='autoFill(

Additionally, you never close the function and separate statements with a semi-colon:

autoFill(" . $row['departure'] . ", " . $row['destination'] . "return false;

should be:

autoFill(" . $row['departure'] . ", " . $row['destination'] . ");return false;

Finally, you may want to wrap those method parameters in quotes:

autoFill(" . $row['departure'] . ", " . $row['destination'] . ");return false;

should be:

autoFill('" . $row['departure'] . "', '" . $row['destination'] . "');return false;

There may be more. One thing to keep in mind is to try to not use echo statements to emit large blocks of HTML. It makes the code a little less easy to read, and more importantly it adds another layer of "stringification" which makes including quotes in strings slightly more difficult.

The spacing on your a tag is wrong, you need a space between the a href .

You never close the brackets for the function call, and I think you may need to put the parameters in quote marks, depending on their type:

autoFill('". $row['departure']. "', '". $row['destination']. "'); return false;'

I solved it by looking at this question: JQuery - Click Link Auto fill in input field

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM