$_POST variable empty when using add_action save_post
-
Hi.
I’m adding some meta_boxes:
add_meta_box('ss_product','Simple Sales fields','simple_sales_show_boxes', 'ss_product', 'normal', 'high');
This is my callback simple_sales_show_boxes
function simple_sales_show_boxes(){ global $post; $ID=$post->ID; echo '<input type="hidden" name="simple_sales_meta_boxes_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />'; $box_fields= array( array( 'name' => 'price', 'desc' => 'Price of Item', 'id' => 'product_price', 'type' => 'text', 'value' => get_post_meta($ID,'product_price',true) ), array( 'name' => 'ordered', 'desc' => 'Items ordered', 'id' => 'ordered', 'type' => 'number', 'value' => get_post_meta($ID,'ordered',true) ), array( 'name' => 'stock', 'desc' => 'Stock remaining', 'id' => 'stock', 'type' => 'number', 'value' => get_post_meta($ID,'ordered',true) ) ); foreach($box_fields as $field){ echo '<tr> <div style="width:20%"><label for="'. $field['id']. '">'. $field['desc'].'</div>'; switch($field['type']){ case 'text': echo '<input type="text" name="'. $field['id']. '" id="'. $field['id']. '" value="'.$field['value']. '" size="30" style="width:50%" /><br />'; break; case 'hidden': echo " <input id='".$field['id']."' type='hidden' name='".$field['id']."' value='".$field['value']."'>"; break; case 'photos': echo '<input id="upload_image" type="text" size="36" name="upload_image" value="" /> <input id="upload_image_button" type="button" value="Upload Image" />'; break; case 'number': echo '<input id="'.$field['id'].'" type="text" size="36" name="upload_image" value="" onkeypress="return (event.charCode >= 48 && event.charCode <= 57)||event.charCode ==46||event.charCode==45" /><br />'; break; } echo '</label>'; } }
I then use the save_post add_action with the callback simple_sales_save_products
add_action('save_post','simple_sales_save_products'); function simple_sales_save_products(){ if(!check_nonces_an_all('simple_sales_meta_boxes_nonce','ss_product')){ return; } global $post; update_post_meta($post->ID, "product_price", $_POST["price"]); update_post_meta($post->ID, "stock", $_POST["stock"]); update_post_meta($post->ID, "ordered", $_POST["ordered"]); }
However, when the $_POST array is checked it’s empty.
$_POST = array ( );
And the database table post_meta has NULL values in the meta_value fields corresponding to the meta_keys ‘product_price’, ‘ordered’ and ‘stock’.
What am I doing wrong?
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘$_POST variable empty when using add_action save_post’ is closed to new replies.