admin管理员组

文章数量:1317364

I try to make custom theme option, a switch select option can change $bannerchange value. but i cannot add/update value of $bannerchange, select option inside a form. working fine with submit button but come to onchange="this.form.submit() the select option not changing.

<?php
if(isset($_POST['wphw_submit'])){
        wphw_opt();
}
function wphw_opt(){
    $bannerchange = $_POST['bannerchange'];
    if( get_option('bannerchange') != $bannerchange) {
        $chk = update_option( 'bannerchange', $bannerchange);
    }
}?>
  <form method="post" action="">         
   
        <select name="bannerchange" onchange="this.form.submit()">
            <option value="page1"<?php if (get_option('bannerchange') == "page1") { echo " selected"; } ?>>Custom Departure</option>
            <option value="page2"<?php if (get_option('bannerchange')  == "page2") { echo " selected"; } ?>>Fixed Departure</option>
        </select>
        <input type="submit" name="wphw_submit" value="Save changes" class="button-primary" />
  </form><?php 
switch ($bannerchange) { 
    case 'page2': 
        break;
    case 'page1':
        break; 
}  

$bannerchange get always undefined. I can debug this problem with straight way? any help greatly appreciated.

I try to make custom theme option, a switch select option can change $bannerchange value. but i cannot add/update value of $bannerchange, select option inside a form. working fine with submit button but come to onchange="this.form.submit() the select option not changing.

<?php
if(isset($_POST['wphw_submit'])){
        wphw_opt();
}
function wphw_opt(){
    $bannerchange = $_POST['bannerchange'];
    if( get_option('bannerchange') != $bannerchange) {
        $chk = update_option( 'bannerchange', $bannerchange);
    }
}?>
  <form method="post" action="">         
   
        <select name="bannerchange" onchange="this.form.submit()">
            <option value="page1"<?php if (get_option('bannerchange') == "page1") { echo " selected"; } ?>>Custom Departure</option>
            <option value="page2"<?php if (get_option('bannerchange')  == "page2") { echo " selected"; } ?>>Fixed Departure</option>
        </select>
        <input type="submit" name="wphw_submit" value="Save changes" class="button-primary" />
  </form><?php 
switch ($bannerchange) { 
    case 'page2': 
        break;
    case 'page1':
        break; 
}  

$bannerchange get always undefined. I can debug this problem with straight way? any help greatly appreciated.

Share Improve this question edited Nov 1, 2020 at 17:03 Noufal Binu asked Oct 18, 2020 at 8:51 Noufal BinuNoufal Binu 2713 bronze badges 6
  • 1 Change <select name="wphw_submit" into <select name="bannerchange". – Mayeenul Islam Commented Oct 19, 2020 at 2:45
  • @MayeenulIslam not working – Noufal Binu Commented Oct 19, 2020 at 5:20
  • 1 What's showing with if(isset($_POST)) { var_dump($_POST['bannerchange']); exit(); }? – Mayeenul Islam Commented Oct 19, 2020 at 6:29
  • not working undefined bannerchage. – Noufal Binu Commented Oct 22, 2020 at 14:09
  • 1 @MayeenulIslam thanks for your comments that's helped me much to finish my code. – Noufal Binu Commented Nov 2, 2020 at 9:20
 |  Show 1 more comment

1 Answer 1

Reset to default 0

I fixed myself we can submit a form by triggering submit button click event:

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

本文标签: javascripton change form submit variable value not updating