Question

I'm running into some trouble when trying to dynamically change an input value with PHP/CI when using a JQuery dialog box.

Here is the pertinent code from the view:

<span id="emailDialog" hidden>
<?php
    echo form_open('', array('id' => 'emailForm'));
    if (isset($this->session->userdata)){
        $name = $this->session->userdata('firstName') . ' ' . $this->session->userdata('lastName');
        $email = $this->session->userdata('email');
    } else {
        $name = '';
        $email = '';
    }
    echo form_input(array(
                 'id' => 'name',
               'name' => 'name',
              'value' => $name,
        'placeholder' => 'Name'
    ));
    echo form_input(array(
                 'id' => 'email',
               'name' => 'email',
              'value' => $email,
        'placeholder' => 'Email'
    ));
    echo form_textarea(array(
                 'id' => 'message',
               'name' => 'message',
               'cols' => 35,
               'rows' => 15,
              'value' => '',
        'placeholder' => 'We look forward to hearing from you!'
    ));
    echo form_close();
?>
<span id="mailErrors"></span>

And the JavaScript/JQuery(1.10.3) code:

function loginForm() {
    $('#login').dialog({
        autoOpen: false,
        height: 450,
        width: 300,
        position: ['center-top'],
        show: 'fade',
        title: 'Login',
        modal: true,
        buttons: {
            Login: function () {
                validateCredentials();
            },
            'Sign Up': function() {
                loadRegister();
            }
        }
    });
    $('#login').dialog('open');
}

What's happening is, if I completely bypass the PHP code, the value for the name input remains '', allowing the "placeholder" to show, however, when using the PHP code and no userdata is set, the name input field (which should be blank) has a space prepended to it, causing no placeholder to appear (and since I'm using placeholders in lieu of label, causing confusion as to what that input is for, though it should be obvious); if userdata is set, the name and email values populate as expected with no ill behavior.

What I've tried:

**Instantiating the blank variable outside the if statement, foregoing the else clause.

**Wrapping JavaScript to change the value in PHP if statement, e.g...

<?php if(isset($this->session->userdata)): ?>
    <script type="text/javascript">
        $('#name').val("<?php echo $this->session->userdata('firstName') ?>");
        $('#email').val("<?php echo $this->session->userdata('email') ?>");
    </script>
<?php endif; ?>

...as well as other variations of the above(.text() rather than .val(), removing the quotes from inside the .val(), and one other thing that's completely escaping me now).

I've done a little research to find that there are a few bug fixes for the dialog widget in 1.11.0, but couldn't see (definitively) that this specific issue was addressed.

I realize this is fairly inconsequential however, I can assure you, it's incredibly annoying. So, I'd really just like to know if anyone else has run into this issue (and possibly fixed it), or if anyone can make any suggestions as to a fix/workaround.

Thanks! -J

Was it helpful?

Solution

Looks like the white space is due to your session, :D:

if (isset($this->session->userdata)){
    $name = $this->session->userdata('firstName') . ' ' . $this->session->userdata('lastName');
    $email = $this->session->userdata('email');
} else {
    $name = '';
    $email = '';
}

Somehow, you're setting the session with empty values, and that's why the $name gets the ' ', XD, CI gives you back two false (firstName and lastName) with a space character between them.

Could you check that?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top