简体   繁体   中英

Auto-submit form with PHP action via JavaScript

Forgive me if this question is naive. I'm new to JavaScript and am learning my way through some setbacks with a form I'm using to display data.

An icao code is passed to the #depicao <select> menu via $_GET['icao'] in the JavaScript code at the bottom of the script. On page load, the <select> menu is populated with the $_GET['icao'] value.

After the #depicao <select> menu is populated, I'd like the form to automatically submit itself with its populated value. My train of thought is that if I include

document.getElementById("form").submit();

as the last line in the script, I can get the script to submit itself after it loads with the $_GET['icao'] value. Unfortunately, this hasn't been working, however.

The code contains <input type="submit" name="submit"> buttons. 该代码包含 <input type="submit" name="submit">按钮。 I believe that this is the culprit.

See code below.

<form id="form" action="<?php echo actionurl('/schedules/view');?>" method="post">
<div id="tabcontainer">
    <ul>
        <li><a href="#depapttab" onclick="formReset()"><span>Via departure airport</span></a></li>
        <li><a href="#arrapttab" onclick="formReset()"><span>Via arrival airport</span></a></li>
    </ul>
    <div id="depapttab">
        <select id="depicao" name="depicao">
        <option value="">Select All</option>
        <?php
        $exclude = array(13, 18, 19, 22); // Airport IDs found in phpVMS_airports not to be included in the dropdown menu
        if(!$depairports) $depairports = array();
            foreach($depairports as $airport) {
                if(!in_array($airport->id, $exclude)) { // Exclude values in the above array from the dropdown menu
                    echo '<option value="'.$airport->icao.'">'.$airport->icao.' - '.$airport->name.'</option>';
                }
            }
        ?>
        </select>
        <input type="submit" name="submit" value="Find Flights" />
    </div>
    <div id="arrapttab">
        <select id="arricao" name="arricao">
            <option value="">Select All</option>
        <?php
        $exclude = array(13, 18, 19, 22); // Airport IDs found in phpVMS_airports not to be included in the dropdown menu
        if(!$depairports) $depairports = array();
            foreach($depairports as $airport) {
                if(!in_array($airport->id, $exclude)) { // Exclude values in the above array from the dropdown menu
                    echo '<option value="'.$airport->icao.'">'.$airport->icao.' - '.$airport->name.'</option>';
                }
            }
        ?>
        </select>
        <input type="submit" name="submit" value="Find Flights" />
    </div>
</div>
<input type="hidden" name="action" value="findflight" />
</form>
<script type="text/javascript">
function formReset() {
    document.getElementById("form").reset();
}
function setSelectedIndex(s, valsearch) {
    // Loop through all the items in drop down list
    for (i = 0; i< s.options.length; i++) { 
        if (s.options[i].value == valsearch) {
            // Item is found. Set its property and exit
            s.options[i].selected = true;
            break;
        }
    }
    return;
}
setSelectedIndex(document.getElementById("depicao"),"<?php if(isset($_GET['icao'])) { echo $_GET['icao']; } else { echo 'Select All'; } ?>");
document.getElementById("form").submit();
</script>

Form Auto Submission Confusion ?:

After the #depicao menu is populated, I'd like the form to automatically submit itself with its populated value.

How I understood you was that you want the form to be submitted automatically when the select menu is populated ? I think you meant when the user chooses a selection, it should submit automatically. If this is what you mean. Please add a onchange="//submitfunction()" onto the <select> tag

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