0

Why doesent this work?! I want to update my postmeta "doors" from frontend

<?php
if ( isset( $_POST['submit'] ) )
    { //if nonce check succeeds.
        global $post;
        $postid = $post->ID;
        $data = $_POST['doors'];
        update_post_meta($postid, 'doors', '2' );
        echo self::$auto_id. 'doors'.'2';
        echo $postid;

}


$doors = get_post_meta($auto->ID, 'doors', true);
?>

<form method="post" action="">
   <input type='text' name='doors' value='<?php echo $doors ?>' />
   <input type='submit' value='save' />
</form>

This code display the door value in my input but if i write something else there then click save nothing happends... it doesent save my new value.

Why?:(

  • you should try to echo out a value at the top of your IF statement to make sure that you're even getting inside the conditional. If that doesn't work, try to check if doors isset instead of submit – Howdy_McGee Aug 1 '14 at 16:01
  • huuh? can u write an example, im not sure if i understand what u mean:( – user2062819 Aug 1 '14 at 16:04
0

Let's start debugging the problem. First let's make sure that we're getting into our IF conditional when the form $_POSTs. If you replace your code with this, then submit your form, you should see the message We've gotten into the $_POST conditional!"

<?php
if ( isset( $_POST['submit'] ) )
{
    echo "We've gotten into the $_POST conditional!"
    exit;
}
?>

<form method="post" action="">
   <input type='text' name='doors' value='<?php echo $doors ?>' />
   <input type='submit' value='save' />
</form>

According to your comment you did get into the $_POST conditional. So we can update the post meta.

<?php
    global $post;

    if ( isset( $_POST['submit'] ) )
    {   
        if( ! isset( $post ) ) {
            echo 'Error: Post Object Not Set';
            die();
        }
        else if( ! isset( $_POST['doors'] ) && ! empty( $_POST['doors'] ) ){
            echo 'Error: Doors Not Set';
            die();
        }

        update_post_meta( $post->ID, 'doors', sanitize_text_field( $_POST['doors'] ) );
    }

    $doors = get_post_meta($post->ID, 'doors', true);

    echo print_r($doors);
?>

    <form method="post" action="">
       <input type='text' name='doors' value='<?php echo isset($doors) ? $doors : ''; ?>' />
       <input type='submit' value='save' />
    </form>

If you don't receive any errors that means both $post is in scope and set and that doors is also set and not empty. Which means that it should definitely update the post meta. If this doesn't work there could be problems outside of the code given.

  • Hey, im getting in the post in Case A then i followed your text but still, its update all field... I am very thankful that your are trying to helping me out, do u have any other option?:) – user2062819 Aug 1 '14 at 17:02
0

You are saving the door value in $data variable. But you're not using this variable in update_post_meta function.

All time you are save ing static value 2. You will update this line

update_post_meta($postid, 'doors', '2' );

with

update_post_meta($postid, 'doors', $data );

Also you wrote this code $doors = get_post_meta($auto->ID, 'doors', true); But how are getting $auto object. I think that your syntax will be

$doors = get_post_meta($post->ID, 'doors', true);
  • that didnt work.. :( – user2062819 Aug 1 '14 at 16:15
  • change this line <input type='submit' value='save' /> with <input type='submit' value='save' name="submit"/> – Chinmoy Kumar Paul Aug 1 '14 at 16:17
  • yes that works fine if i change $postid = $post->ID; to-> $postid = $auto->ID; but a new problem came..., now it updated all my input on my dashboard like this i58.tinypic.com/2qtd5p4.png – user2062819 Aug 1 '14 at 16:22
  • are you change this line update_post_meta($postid, 'doors', '2' ); by update_post_meta($postid, 'doors', $data ); – Chinmoy Kumar Paul Aug 1 '14 at 16:28
  • global $post; $postid = $auto->ID; $data = $_POST['doors']; update_post_meta($postid, 'doors', $data ); – user2062819 Aug 1 '14 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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