简体   繁体   中英

Add value to existing form field with javascript

i am trying to add the output of the following code (on page load)

<?php the_field('object_id'); ?>

to an field of an form with the id="sf_objekt". The field has an optional set value "ID". The output of the code should replace the "ID".

I tried using "append", but with no luck.

Any ideas? Ty!

If I have correctly undestood your question, the following snippet should do the trick:

 document.querySelector('#sf_objekt').value = "<?php the_field('object_id'); ?>"
 <input id="sf_objekt" type="text" />

Clearly the <?php> tag will be replaced with the output of the PHP function the_field() only if the page is ran through a PHP server.

This code will auto fill in a form with the result of the php script.

<html>
<body>
    <form>
      <input type="text" id="sf_objekt">
    </form>
<script>
    document.getElementById("sf_objekt").defaultValue= "<?php echo the_field('object_id'); ?>";
</script>

</body>
</html>

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