简体   繁体   中英

Form submit is not working for dropdown change in wordpress admin

I was working on submitting the form after change in dropdown in wordpress admin. I tried adding javascript to dropdown like <select onchange="this.form.submit()"></select> , this also didn't work for me. Even i tried using JQuery but didn't work.

functions.php

//**************function to add the javascript
function add_my_script() {
    wp_enqueue_script(
    'dropdown', scripts and de-register, etc.
    get_template_directory_uri() . '/js/dropdown.js', 
    array('jquery'),'20120206' 
    );
}
add_action( 'wp_enqueue_scripts', 'add_my_script' );

//**************function to create the form
function Reviews_settings_page()
{ 
?>
  <div class="wrap">        
    <form method="POST" name="review_edit" action="<?php echo $_SERVER['PHP_SELF'].'?page=Reviews-panel';?>">
       <?php
          settings_fields("section");
          do_settings_sections("Reviews-options");
          submit_button(); 
       ?>   
    </form>
  </div>
<?php
}

//*********function to create the dropdown
function display_Reviews_Brand_listing()
{
echo "<select name='dropdown' id='dropdown'>
    <option value=''>--select--</option>
    <option value='101'>EVEREST</option>
    <option value='102'>REVOLT</option>
    <option value='103'>HAWANA</option>
    <option value='104'>ELIZE</option>
</select>";
}

//**************adding functionality to wordpress admin
function display_Reviews_panel_fields()
{
add_settings_section("section", "Reviews", null, "Reviews-options");
add_settings_field("Reviews_url", "Select the Brand for Review", "display_Reviews_Brand_listing", "Reviews-options", "section");

register_setting("section", "Reviews_url");
}
add_action("admin_init", "display_Reviews_panel_fields");

dropdown.js

jQuery( document ).ready( function( $ ) {
    $('#dropdown').on('change', function() {
    var $form = $(this).closest('form');
    $form.find('input[type=submit]').click();
  });
} );

Any help will be appreciated. Thanks for your time and efforts for this problem.

I tried to clone your error. Keep the form action empty instead of $_SERVER['PHP_SELF'] . your dropdown.js is added successfully by add_action()

for me also onchange='this.form.submit();' didn't work.

I tried different snippets to submit the form and finally below javascript worked for me

dropdown.js

function myFunction() {
        document.querySelectorAll("input[type=submit]")[0].click();
    }

functions.php

//update your form tag
<form method="POST" name="review_edit" id="review_edit" action="">

//update your <select>
<select name='dropdown' id='dropdown' onchange='myFunction();'>

Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM