• Resolved xerviami

    (@xerviami)


    Dear all,,

    I well understand that Ninja Forms support does not address code issue.

    I post here in case of the Ninja Forms community could help me. ??

    I am trying to create and modify forms with PHP.

    Creating an empty form with this code works well (the new form appears in the admin panel):

    https://developer.ninjaforms.com/codex/forms/

    <?php
    
    /*
     * Create a Form
     */
     
    $form = Ninja_Forms()->form()->get();
    
    $form->update_setting( 'title', 'Contact Me' );
    
    $form->save();

    Nevertheless, I am trying to add a field in an existing form with this code and I am not able to make it work… :

    https://developer.ninjaforms.com/codex/fields/

    /*
     * Create a Field
     */
     
    $field = Ninja_Forms()->form( 12 )->field()->get();
    
    $field->update_settings( 'type', 'textbox' )->save();

    Can anyone tell me what I am doing wrong ?

    Thank you

    Xerviami

Viewing 2 replies - 1 through 2 (of 2 total)
  • Shaylee

    (@shayleehansen30)

    appreciate you reaching out to the Ninja Forms community for assistance with modifying forms using PHP. Let’s take a look at the code you provided.

    In the first code snippet, you successfully create a new form and update its title using the Ninja Forms API. That’s working well for you, which is great to hear.

    Now, let’s focus on adding a field to an existing form. The code snippet you provided should work as expected. However, there might be a couple of things to check:

    1. Verify the form ID: In the second code snippet, you’re using Ninja_Forms()->form(12) to retrieve the form with ID 12. Make sure that the form ID you’re referencing is correct and matches the form you want to modify.
    2. Confirm the field settings: Ensure that you’ve defined the necessary field settings correctly. The update_settings() method should include all the required settings for the field type you’re adding. Double-check the field type and any additional settings that might be required.

    If you’ve verified the form ID and ensured the field settings are correct, but the code still doesn’t seem to be working, I recommend sending your question to our Customer Success team, ninjaforms.com/contact, for more direct assistance.

    Thread Starter xerviami

    (@xerviami)

    Dear Shaylee,

    First of all, thank you for your answer.

    Yes, 12 is the correct form identifier.

    I changed it to test on dirfferent forms but nothing changed the result. In all cases, I used a print_r(Ninja_Forms()->form( 12 )->get()); to verify that the identifier is the correct one (and changed the id accordingly).

    For the setting concern, please keep in mind that the code I use here is the exact copy of the code available in the codex of Ninja forms (except for the id, which I changed). Therefore I do not see how I can have forgotten something.

    https://developer.ninjaforms.com/codex/fields/

    OK I will open a case to your Customer Success team.

    Thank you !

    Xerviami

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Modify form with PHP’ is closed to new replies.