1

I made a text box to input number and after the user click the save button, it will save the value into the wp_postmeta.

But, the problem is that, after I refresh the page the number will content with space character.

Like this:

enter image description here

Code:

<div class = "meta-td03">
                <input type="text" class="dwwp-row-content03" name="bonus_point" id="bonus_point" value="
                    <?php 
                        if ( ! empty ( $dwwp_stored_meta['bonus_point'] ) ) {
                            echo esc_attr($dwwp_stored_meta['bonus_point'][0] );
                        } 
                    ?>"
               />
            </div>

Save function:

if ( isset( $_POST[ 'bonus_point' ] ) ) {
        update_post_meta( $post_id, 'bonus_point', sanitize_text_field( $_POST[ 'bonus_point' ] ) );
    }

And I go to the database, and the value actually is 86.

This problem is came after I changed the editor to Eclipse from netBean. And I tried to go back netBean, but it isn't work. How to fix this problem? Thx a lot.

3
  • 1
    The problem is new lines inside value attribute. Have you tried: value="<?php if (...) ?>".
    – wpclevel
    Aug 25, 2016 at 4:21
  • Oh YS thx! Problem is fixed. But why this problem appear?
    – Capslock10
    Aug 25, 2016 at 4:34
  • @Capslock10 the problem is that you hit the enter key after your value=" then you opened your <?php tag, so before the <?php tag and after your = you have a new line!
    – bynicolas
    Aug 25, 2016 at 4:38

1 Answer 1

1

Like @Dan9 said, you have a new line inside your value attribute.

you can write your conditional statement this way too

<div class = "meta-td03">
  <input type="text" class="dwwp-row-content03" name="bonus_point" id="bonus_point" value="<?php ! empty( $dwwp_stored_meta['bonus_point'] ) ? echo esc_attr($dwwp_stored_meta['bonus_point'][0] ) : ''; ?>" />
</div>
3
  • Thx a lot. Is it the attribute should inside one line? As it make the line so long...
    – Capslock10
    Aug 25, 2016 at 4:39
  • You can break line inside the <?php ?> tag but any new line outside of PHP tags will be considered HTML so it will be rendered by the browser. I don't make new lines when it's not necessary, especially to avoid this kind of issues!
    – bynicolas
    Aug 25, 2016 at 4:43
  • 1
    OK. THX A LOT!!
    – Capslock10
    Aug 25, 2016 at 4:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.