0

I have built a contact form for my wordpress site. Four fields are there - name, email, subject, message. For logged in users I want their name and email to be auto-filled in their respective fields in the form and those name, email fields will be disabled for them to edit. And for non-logged in users they will put name, email fields manually. I have put this code in the page template file -

    <?php

    $current_user = wp_get_current_user();
    $user_email = $current_user->user_email;
    $user_name = $current_user->user_firstname.' '.$current_user>user_lastname;

    if ( 0 != $current_user->ID ) {

     echo '<script type="text/javascript">
     document.getElementById("curUserName").value = "'.$user_name.'";
     document.getElementById("curUserName").disabled = "disabled";
     document.getElementById("curUserEmail").value = "'.$user_email.'";
     document.getElementById("curUserEmail").disabled = "disabled";
     </script>';
     }
     ?>

But this code is disabling name, email fields for both users (logged in and non-logged in). I have controlled the script through if condition. Still the javascript is applying for both users. Please advise where I have gone wrong.

Here is the form html -

    <form action="/success.php" method="post">
<label>Name :</label><br>
<input type="text" id="curUserName" name="sender_name" required><br>
<label>Email :</label><br>
<input type="text" id="curUserEmail" name="sender_email" required><br>
<label>Subject :</label><br>
<input type="text" name="sender_sub"><br>
<label>Message</label><br>
<textarea name="sender_msg" rows="4" cols="60" required></textarea><br>
<input type="submit" name="submit" value="Send">
</form>
3
  • document.getElementById("curUserName").disabled can only be assigned the values that are true of false. By assigning the string "disabled", you are assigning is a true value, therefore it is disabled. You mentioned conditionals. I see no if statements in your js. Commented Mar 22, 2018 at 15:25
  • I am nesting the javascript in php if condition. Commented Mar 22, 2018 at 15:32
  • Can you do var_dump($current_user); and let us know what you get? Commented Mar 22, 2018 at 15:40

2 Answers 2

1

The source you are going to change has disabled option already.. so just remove it if the user is not logged in :)

<?php

    $current_user = wp_get_current_user();
    $user_email = $current_user->user_email;
    $user_name = $current_user->user_firstname.' '.$current_user>user_lastname;

    echo '<script type="text/javascript">';
    if ( 0 != $current_user->ID )
    {

        echo 'document.getElementById("curUserName").value = "'.$user_name.'" 
        document.getElementById("curUserName").disabled = "disabled"
        document.getElementById("curUserEmail").value = "'.$user_email.'"
        document.getElementById("curUserEmail").disabled = "disabled"';
     }
     else
     {

        echo 'document.getElementById("curUserName").disabled = false;
        document.getElementById("curUserEmail").disabled = false;';         
     }
     echo '</script>';
 ?>
Sign up to request clarification or add additional context in comments.

Comments

1

Don't echo javascript with php. It's bad practice.

Try using value tags in your inputs, check if user logged in with a ternary operator, and if so, echo to value tag their credentials.

<?php (is_user_logged_in()) ? $current_user = wp_get_current_user() : $current_user = false; ?>

<label>Name</label>
<input type="text" id="curUserName" name="sender_name" value="<?php ($current_user) ? echo $current_user->user_name : '' ?>" required>
<label>Email</label>
<input type="text" id="curUserEmail" name="sender_email" value="<?php ($current_user) ? echo $current_user->user_email : '' ?>" required>

Comments

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.