Support

Account

Home Forums ACF PRO Can\'t get the name value to use $_POST

Solving

Can\'t get the name value to use $_POST

  • I’m using a ACF Form on one of my pages (on the front-end) which all works fine. But I need some of the values to be “saved/stored” to write to a external database. Right now if I submit the form on the front-end it makes the page and shows all information in WordPress. But I can’t seem to get the $POST_ data I need to write to the external database. Here is some of my code.

    <div id="primary" class="content-area">
        <div id="content" class="site-content" role="main">
            <?php while ( have_posts() ) : the_post(); ?>
                <?php acf_form(array(
                    'post_id'       => 'new_post',
                    'new_post'      => array(
                        'post_type'     => 'db_test',
                        'post_status'       => 'publish'
                    ),
                    'submit_value'      => 'Create a new event',
                    // 'return' => '/wp-content/themes/MY_THEME/includes/new-employee.php'
                )); ?>
            <?php endwhile; ?>
        </div>
    </div>

    Which creates the following HTML:

    <form id="acf-form" class="acf-form" action="" method="post">
    	<div class="acf-fields acf-form-fields -top">
    		<div id="Firstname" class="acf-field acf-field-text acf-field-5968834df2ac1 voornaam" style="width:40%;" data-name="voornaam" data-type="text" data-key="field_5968834df2ac1" data-width="40">
    			<div class="acf-label">
    				<label for="acf-field_5968834df2ac1">Voornaam</label>
    			</div>
    			<div class="acf-input">
    				<div class="acf-input-wrap"><input type="text" id="acf-field_5968834df2ac1" class="" name="acf[field_5968834df2ac1]" value="" placeholder="" /></div>	</div>
    			</div>
    		</div>
    	<div class="acf-form-submit">
    		<input type="submit" class="acf-button button button-primary button-large" value="Create a new event" />			<span class="acf-spinner"></span>			
    	</div>
    </form>

    Now I would like to store the field acf[field_5968834df2ac1]. So I did what I would normally do:

    <?php
        $Firstname = $_POST['acf']['field_5968834df2ac1'];
        echo 'data' . $Firsname;
    ?>

    But this does nothing? I can’t get the data value of the name to use somewhere else. What am I doing wrong?

  • Is the code you posted the exact code you’re using? If it is then you have a typo in your field name

    
    <?php
        $Firstname = $_POST['acf']['field_5968834df2ac1'];
        echo 'data' . $Firsname; // this is different than the line above.
    ?>
    

    When are you attempting to get these values and save them? Is it in an acf/save_post action? https://www.advancedcustomfields.com/resources/acf-save_post/

  • Hi John,

    Sorry, there was a typo in my post. I changed it but still no go. I’ve tried to figure out the acf/save_post documentation. But I can’t seem to get it to work, don’t know how.

  • what version of ACF are you using?

  • Advanced Custom Fields PRO Versie: 5.5.14

  • If you can’t get the post value then either it is not set or you’re trying to get it at the wrong time.

    When are you trying to get this value to put into the other DB? When is this code run? Will need more information on when and how you are trying to do this.

  • At this point, I’m just trying to echo the value if I cant do that I can load this value in my database. I’ve tried both scripts (hooks) the before and after. But I don’t know exactly how to edit those hooks so they’ll work for me…

    I’m trying to echo the value when you press submit

  • this does not tell me anything

    Iā€™m trying to echo the value when you press submit

    Please provide code that is specific to what you are attempting to do, whatever it is that you’re attempting to do. If this is an a filter that you are creating then the information on how you are adding the filter and the code of that filter would be helpful.

  • I’ll try. But I’m repeating my self here. But maybe this will clarify.

    This is my ACF form:

    <div id="primary" class="content-area">
        <div id="content" class="site-content" role="main">
            <?php while ( have_posts() ) : the_post(); ?>
                <?php acf_form(array(
                    'post_id'       => 'new_post',
                    'new_post'      => array(
                        'post_type'     => 'db_test',
                        'post_status'       => 'publish'
                    ),
                    'submit_value'      => 'Create a new event',
                    // 'return' => '/wp-content/themes/MY_THEME/includes/new-employee.php'
                )); ?>
            <?php endwhile; ?>
        </div>
    </div>

    It gives me this HTML

    <form id="acf-form" class="acf-form" action="" method="post">
    	<div class="acf-fields acf-form-fields -top">
    		<div id="Firstname" class="acf-field acf-field-text acf-field-5968834df2ac1 voornaam" style="width:40%;" data-name="voornaam" data-type="text" data-key="field_5968834df2ac1" data-width="40">
    			<div class="acf-label">
    				<label for="acf-field_5968834df2ac1">Voornaam</label>
    			</div>
    			<div class="acf-input">
    				<div class="acf-input-wrap"><input type="text" id="acf-field_5968834df2ac1" class="" name="acf[field_5968834df2ac1]" value="" placeholder="" /></div>	</div>
    			</div>
    		</div>
    	<div class="acf-form-submit">
    		<input type="submit" class="acf-button button button-primary button-large" value="Create a new event" />			<span class="acf-spinner"></span>			
    	</div>
    </form>

    All I want is when you click on submit it writes the entry to the WP database and returns the value of this acf[field_5968834df2ac1] field using an echo.

    Im trying to do that using this code:

    <?php
        $FirstName = $_POST['acf']['field_5968834df2ac1'];
        echo 'data' . $FirstName; ?>

    But that doesn’t give me any results. So my question was/is how do I get the value of this field when I clicked on submit and the page reloads.

    Here is my whole code:

    <?php acf_form_head(); ?>
    <?php /* Template name: Nieuwe Collega's */ get_header(); ?>
    <?php session_start();
    $_SESSION['foo'] = $_POST['acf']['field_5968834df2ac1'];
    ?>
    
    <div id="primary" class="content-area">
        <div id="content" class="site-content" role="main">
            <?php while ( have_posts() ) : the_post(); ?>
                <?php acf_form(array(
                    'post_id'       => 'new_post',
                    'new_post'      => array(
                        'post_type'     => 'db_test',
                        'post_status'       => 'publish'
                    ),
                    'submit_value'      => 'Create a new event',
                    // 'return' => '/wp-content/themes/intranetpuur/includes/new-employee.php'
                )); ?>
            <?php endwhile; ?>
        </div>
    </div>
    <?php
        $FirstName = $_POST['acf']['field_5968834df2ac1'];
        echo 'data' . $FirstName;
        echo $_SESSION['foo'];
     ?>
    <?php get_footer(); ?>
  • Seeing the 3rd piece of code with it all together helps. When the page is being loaded and showing the form it is too late, posted data is not available when a page is being displayed. You must use an acf/save_post action https://www.advancedcustomfields.com/resources/acf-save_post/.

    This is how form submissions work
    1) You press submit
    2) ACF saves that values (this is when $_POST) is available
    3) ACF redirects back to the page and the page is reloaded ($_POST is not available when the page is being reloaded)

    If $_POST was available on the page reload then hitting the browser reload button would resubmit the data.

    
    // priority 1 or 20 depending on when you want this to run
    // if you use 1 then you need to look in $_POST['acf']
    // if you use 20 then you can use get_feild() and other acf functions
    add_action('acf/save_post', 'my_acf_save_post_action', 1);
     
    function my_acf_save_post_action($post_id) {
      if (get_post_type($post_id) != 'db_test') {
        // not our post type
        return;
      }
      // code here to get acf values and do something with them
    }
    
  • So for the way I want to use this. I’ll probably be best off using priority 20. Because I want the values after the post is submitted.

    So something like this?

    <?php// priority 1 or 20 depending on when you want this to run
    // if you use 1 then you need to look in $_POST['acf']
    // if you use 20 then you can use get_feild() and other acf functions
    add_action('acf/save_post', 'my_acf_save_post_action', 20);
    
    function my_acf_save_post_action($post_id) {
      if (get_post_type($post_id) != 'db_test') {
        // not our post type
        return;
      }
      $FirstName = get_field('voornaam_db');
        echo 'data' . $FirstName;
    }?>
  • Yes, that would work.

  • Sorry, it doesn’t šŸ˜€ I have placed the function in the code just under the acf_form_head() is this possible? Or do I need to place it in the function.php file?

  • you filter function needs to go in your function.php file. The template file is only loaded when showing the page so the function will not exist when it needs to be called.

    Basically, your filter/function needs to exist before acf_form_head() is called.

  • Hi John, I hope you had a nice weekend. But I’m still working on this issue. I’ve placed the code in my function.php file. But it still doesn’t work. Here is my code. I’ve replaced the code from here and pasted the code from the link you provided…

    function my_acf_save_post( $post_id ) {
    
        // get new value
        $value = get_field('voornaam_db');
    
        // do something
        echo 'data' . $value;
    }
    
    add_action('acf/save_post', 'my_acf_save_post', 20);

    Still no value is being echo-ed. I’ve also tried to do an alert after // do somethingalert('something'); but this made the page crash after the submit.

  • If what you have is in the right place, it should not only echo the value but it should also crash the page with an error “unable to send header information headers already sent at….”

    You say that the post is being created and saved so I can’t say what the problem is. At this point I can’t see any reason that it would not be working.

  • Yeah, same here. I’ve checked everything and I think it should work. But. I’ve been working on a workaround which works for me. So let’s just close this one, maybe in the future I’ll look into this. Thanks for the support.

  • I’m experiencing the same issue.

    I don’t understand why acf_form wouldn’t accept a normal $_POST. How could it be getting all other values without $_POST?

        <?php
        $first_name = $_POST['acf']['field_5ae11c28116dc'];
    
        // Displays the User Profile fields.
        $options = array(
          'field_groups' => array(105),
          'submit_value' => 'Contact',
          'updated_message' => __("Thank you. Your submission has been received.", 'acf'),
          'return' => add_query_arg( 'updated', 'true', '' ),
          'post_id'		=> 'new_post',
          'new_post'		=> array(
            'post_type'		=> 'sfm_contact_requests',
            'post_status'		=> 'publish',
            'post_title' => $first_name,
          ),
        );
    
        acf_form($options);
  • As I explained previously, $_POST is not available when the page is being displayed, it simply has no value.

    1) The page loads and displays the form
    2) You fill out the form and submit it
    3) acf_form_head() detects that the form was submitted. Your template for this has not been loaded, and even if it was the code in it would not run because ACF is taking control at this point.
    4) acf does acf/save_post action https://www.advancedcustomfields.com/resources/acf-save_post/. You can hook into the both before and after ACF saves the data in fields
    5) When processing is done ACF redirects, this can either be to the post page or somewhere else if you use this setting in acf_form(). Because of the redirect $_POST has no value when your template for the page is run because no values are submitted in the redirect.

    If you want to do something with the data then you must do so during the acf/save_post action.

    Optionally, you could also do this during the acf/pre_save_post action that is also called for a form inserted using acf_form() https://www.advancedcustomfields.com/resources/acf-pre_save_post/, but this would generally only be done if you want to customize $post_id in some way before ACF calls acf/save_post.

Viewing 19 posts - 1 through 19 (of 19 total)

The topic ‘Can\'t get the name value to use $_POST’ is closed to new replies.