• cheerychops

    (@cheerychops)


    I cannot see what is wrong with the statements below which try to identify an error. Nothing is saved and nothing printed – why?

    For printing do I need to create a text field in the form?

    if ( (! isset($_POST["wizpart[sku]"])) || (! isset($_POST["wizpart[quant]"])))
    //if ( (empty($_POST["wizpart[sku]"])) || (empty($_POST["wizpart[quant]"]))) {
    //if  (! isset($_POST["wizpart[sku]"], $_POST["wizpart[quant]"])) {
    			print '<p class= "error">Fields not completed!</p>';
    			$okay = FALSE;
    		}

    Complete code

    <?php
    
    
    add_action( 'add_meta_boxes', 'wizpart_meta_box_init' );
    
    // meta box functions for adding the meta box and saving the data
    
    		function wizpart_meta_box_init() {	
    		// create our custom meta box
    		add_meta_box( 'wizpart_meta', // Unique ID
    					 'Parts Information', // Title
    					 'wizpart_meta_box', // Callback function
    					 'product', //  post type
    					 'normal', // Context
    					 'high'  // Priority
    					);
    		}
    
    
    function wizpart_meta_box( $post ) {
    // Leading HTML
    
    	
        // retrieve the custom meta box values
        $wizpart_meta_full = get_post_meta( $post->ID, '_wizpart_data');
    	echo var_dump($wizpart_meta_full).'<br />';
    		if (! empty($wizpart_meta_full)) {
    			foreach( $wizpart_meta_full as $entry){ 
    			// print out each part
    			print 'Product comprises '.$entry["quant"].' units of '.$entry["sku"].' <br />';
    			}
    			}	
    
        //nonce for security
    	wp_nonce_field( 'meta_box_save', 'wizpart_plugin' );
    
       // Form HTML
    	echo '<div class = wizpart>';
    	echo '<table>';
    	echo '<tr>';
    	echo '<td> Enter correct SKU of part: </td>';
    	echo '<td> <input type="text" name="wizpart[sku]" value = "" size="5" > </td>';
    	echo '</tr>';
    	
    	echo '<tr>';
    	echo '<td> Enter quantity of parts required as integer: </td>';
    	echo '<td> <input type="text" name="wizpart[quant]" value = "" size="5" > </td>';
    	echo '</tr>';
    	echo '</table>';
    	echo '</div>';	
    }
    
    // hook to save  meta box data when the post is saved
    add_action( 'save_post', 'wizpart_save_meta_box' );
    
    function wizpart_save_meta_box( $post_id ) {
    	// Flag variable
    	$okay = TRUE;
        // catch incomplete form
    if ( (! isset($_POST["wizpart[sku]"])) || (! isset($_POST["wizpart[quant]"])))
    //if ( (empty($_POST["wizpart[sku]"])) || (empty($_POST["wizpart[quant]"]))) {
    //if  (! isset($_POST["wizpart[sku]"], $_POST["wizpart[quant]"])) {
    			print '<p class= "error">Fields not completed!</p>';
    			$okay = FALSE;
    		}
    		// if auto saving skip saving our meta box data
    		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
    			return;
    		//check nonce for security
    		wp_verify_nonce( 'meta_box_save', 'wizpart_plugin' );
    		// store data in an array
    		// Get the post ID from the from SKU
    		$wizpart_data = $_POST['wizpart'];
    		// use array map function to sanitize options
    
    		$wizpart_data = array_map( 'sanitize_text_field', $wizpart_data) ;
    
    		// save the meta box data as post meta using the post ID as a unique prefix
    		// using add_post_meta to increment the array with values
    		if ($okay) {
    			add_post_meta( $post_id, '_wizpart_data', $wizpart_data );
    		print '<p class= "error">Success!</p>';
    		} else {
    		print '<p class= "error">Encountered error</p>';
    		}
    	}
    ?>

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello @cheerychops can you please try to use replace ” name=”wizpart[sku]” ” with “name=”wizpart_sku” “

    It will fix your issue.

    Thread Starter cheerychops

    (@cheerychops)

    I’m afraid not, the code works to enter values when I comment out the ‘if’ statement

    if  ( (! isset($_POST["wizpart[sku]"])) || (! isset($_POST["wizpart[quant]"]))) {
    	print '<p class= "error">Fields not completed!</p>';
    	$okay = FALSE;
    }

    to give the following output;

    array(1) { [0]=> array(2) { [“sku”]=> string(3) “PA1” [“quant”]=> string(1) “5” } }
    Product comprises 5 units of PA1

    But with that code commented out using the post’s UPDATE button it will keep adding Null data

    array(3) { [0]=> array(2) { [“sku”]=> string(3) “PA1” [“quant”]=> string(1) “5” } [1]=> array(2) { [“sku”]=> string(0) “” [“quant”]=> string(0) “” } [2]=> array(2) { [“sku”]=> string(0) “” [“quant”]=> string(0) “” } }
    Product comprises 5 units of PA1
    Product comprises units of
    Product comprises units of

    The bottom ‘if’ appears to work as intended

    if ($okay) {
    add_post_meta( $post_id, '_wizpart_data', $wizpart_data );
    print '<p class= "error">Success!</p>';
    } else {
    print '<p class= "error">Encountered error</p>';
    }

    Still nothing is printed though.

    • This reply was modified 9 months, 4 weeks ago by cheerychops. Reason: minor edit

    You need to work on dynamic indexing to get it done.

    if  ( (! isset($_POST["wizpart[sku]"])) || (! isset($_POST["wizpart[quant]"]))) {
    	print '<p class= "error">Fields not completed!</p>';
    	$okay = FALSE;
    }

    This will not work because your are using index with in the index $_POST[“wizpart[sku]”] try to use it like $_POST[“wizpart”][“sku”]

    you can use foreach loop to get this done after post

    Hope you will get this working other wise you can ping me

    • This reply was modified 9 months, 4 weeks ago by Bitcraftx.
    Thread Starter cheerychops

    (@cheerychops)

    Hi thanks for your input here are the results, no success though

    Try 1. caused “Fatal error: Uncaught Error: Undefined constant ““wizpart”” in C:\xampp\htdocs\wiz_invent\wp-content\themes\storefront-child\functions.php”

    if  ( (! isset($_POST[“wizpart”][“sku”])) || (! isset($_POST[“wizpart”][“quant”]))) {
    			print '<p class= "error">Fields not completed!</p>';
    			$okay = FALSE;
    		}

    Try 2. This produces no code error but does not prevent the saving of empty text fields as presumably $okay = FALSE

    if  ( (! isset($_POST['wizpart']['sku'])) || (! isset($_POST['wizpart']['quant']))) {
    			print '<p class= "error">Fields not completed!</p>';
    			$okay = FALSE;
    		}	

    Try 3. No errors but does not save data. And commenting the ‘if ($okay)’ results in empty string added.

    if  ( (! isset($_POST[0]['wizpart']['sku'])) || (! isset($_POST[0]['wizpart']['quant']))) {
    			print '<p class= "error">Fields not completed!</p>';
    			$okay = FALSE;
    		}

    Try 4. No code errors still saving empty form text fields.

    foreach( $_POST[$wizpart] as $entry){ 
    			// print out each part
    			if (! isset($entry) ) {
    			$okay = FALSE;
    			return;
    			}
    			}

    Try 6. Using ‘if ( empty($entry) )’ in above also fails to prevent saving of empty text fields.

    The output from a successful save is;

    array(2) { [0]=> array(2) { [“sku”]=> string(0) “” [“quant”]=> string(0) “” } [1]=> array(2) { [“sku”]=> string(3) “PA1” [“quant”]=> string(1) “6” } }
    Product comprises units of
    Product comprises 6 units of PA1

    Entire code

    add_action( 'add_meta_boxes', 'wizpart_meta_box_init' );
    
    // meta box functions for adding the meta box and saving the data
    
    		function wizpart_meta_box_init() {	
    		// create our custom meta box
    		add_meta_box( 'wizpart_meta', // Unique ID
    					 'Parts Information', // Title
    					 'wizpart_meta_box', // Callback function
    					 'product', //  post type
    					 'normal', // Context
    					 'high'  // Priority
    					);
    		}
    
    
    function wizpart_meta_box( $post ) {
    // Leading HTML
    
    	
        // retrieve the custom meta box values
        $wizpart_meta_full = get_post_meta( $post->ID, '_wizpart_data');
    	echo var_dump($wizpart_meta_full).'<br />';
    		if (! empty($wizpart_meta_full)) {
    			foreach( $wizpart_meta_full as $entry){ 
    			// print out each part
    			print 'Product comprises '.$entry['quant'].' units of '.$entry['sku'].' <br />';
    			}
    		}	
    
        //nonce for security
    	wp_nonce_field( 'meta_box_save', 'wizpart_plugin' );
    
       // Form HTML
    	echo '<div class = wizpart>';
    	echo '<table>';
    	echo '<tr>';
    	echo '<td> Enter correct SKU of part: </td>';
    	echo '<td> <input type="text" name="wizpart[sku]" value = "" size="5" > </td>';
    	echo '</tr>';
    	
    	echo '<tr>';
    	echo '<td> Enter quantity of parts required as integer: </td>';
    	echo '<td> <input type="text" name="wizpart[quant]" value = "" size="5" > </td>';
    	echo '</tr>';
    	echo '</table>';
    	echo '</div>';	
    }
    
    // hook to save  meta box data when the post is saved
    add_action( 'save_post', 'wizpart_save_meta_box' );
    
    function wizpart_save_meta_box( $post_id ) {
    	// Flag variable
    	$okay = TRUE;
        // catch incomplete form
    		
    		/*if  ( (! isset($_POST[0]['wizpart']['sku'])) || (! isset($_POST[0]['wizpart']['quant']))) {
    			print '<p class= "error">Fields not completed!</p>';
    			$okay = FALSE;
    		}*/
    	
    			foreach( $_POST[$wizpart] as $entry){ 
    			// print out each part
    			if (! isset($entry) ) {
    			$okay = FALSE;
    			return;
    			}
    			}
    	
    	
    			// if auto saving skip saving our meta box data
    			if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    			//check nonce for security
    			wp_verify_nonce( 'meta_box_save', 'wizpart_plugin' );
    			// store data in an array
    			// Get the post ID from the from SKU
    			$wizpart_data = $_POST['wizpart'];
    			// use array map function to sanitize options
    
    			$wizpart_data = array_map( 'sanitize_text_field', $wizpart_data) ;
    
    			// save the meta box data as post meta using the post ID as a unique prefix
    			// using add_post_meta to increment the array with values
    			if ($okay) {
    				add_post_meta( $post_id, '_wizpart_data', $wizpart_data );
    			print '<p class= "error">Success!</p>';
    			} else {
       			print '<p class= "error">Encountered error</p>';
    			}
    	}

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘“isset” not working in from submission to WP metabox’ is closed to new replies.