1

I have the following PHP+HTML form:

public function doContactForm() {
    ob_start();
    $current_user = wp_get_current_user();
    ?>
<div id="vue_contact_form_app">
    <div class="basic-message" v-bind:class="{ 'error-message' : status == 'error', 'success-message' : status == 'sent' || status == 'sending' }" v-on:click="closeMessage">
            <p v-show="status != 'clean'">{{ actionMessage }}</p>
    </div>
    <form class="vue-form" @submit.prevent="submit">
        <fieldset>
            <div>
                <label class="label" for="name">Your Name:</label>
                <input type="text" name="name" id="name" placeholder="Your Name" required="" maxlength="80" v-model="name">
            </div>
            <div>
                <label class="label" for="email">Your Email:</label>
                <input type="email" name="email" id="email" placeholder="[email protected]" required="" maxlength="40" :class="{ email , error: !email.valid }" v-model="email.value">
            </div>
            <div>
                <label class="label" for="subject">Subject:</label>
                <input type="text" name="subject" id="subject" placeholder="An iteresting idea." required="" maxlength="120" v-model="subject">
            </div>
            <div>
                <label class="label" for="textarea">Message:</label>
                <textarea class="message" name="textarea" id="textarea" placeholder="Dear Mr. Dmitry, ..." required="" v-model="message.text" :maxlength="message.maxlength"></textarea>
                <span class="counter">{{ message.text.length }} / {{ message.maxlength }}</span>
            </div>
            <div class="g-recaptcha" data-sitekey="<?php echo $this->getOption('PublicKey');?>"></div>
            <div>
                <input type="submit" value="Send">
            </div>
        </fieldset>
    </form>
</div>

and the following Vue.js model:

function createVueContactForm(sendUrl) {
new Vue({
    // root node
    el: "#vue_contact_form_app",
    // the instance state
    data: function () {
        return {
            name: "",
            email: {
                value: "",
                valid: true
            },
            subject: "",
            message: {
                text: "",
                maxlength: 64 * 1024
            },
            status: "clean", //error, sending, sent
            actionMessage: "",
        }
    },
    
    computed: {
        hasMessage: function () {
            return this.status != "clean";
        }
    },

How do I set a default value for Your Name input field as $current_user->display_name?

Is there a better (or easier) way than this?

My first idea was that I do this via HTML, but it doesn't work.

See the full source code of my WordPress plugin (it is open-source)

6
  • 1
    You could read the value from the field (if you actually output it into there via your PHP code) when you are initializing the whole thing, something like name: { value: document.querySelector('name').value, valid: true } Commented Jul 25, 2023 at 9:46
  • @CBroe tried this: name: { value: document.querySelector('name').value, valid: true },, it without a success, probably name should be selected as vue_contact_form_app->vue-form->name? (I am to sure where is it in the document) Commented Jul 25, 2023 at 10:25
  • Did you actually pre-fill the field value via PHP? Commented Jul 25, 2023 at 10:28
  • @CBroe I hope, yes, see github.com/dmitriano/vue-contact-form/blob/… (the code is open-source) Commented Jul 25, 2023 at 10:57
  • @CBroe When I cleared the browser cache, the form disappeared, see developernote.com/contact Commented Jul 25, 2023 at 11:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.