簡體   English   中英

通過JavaScript通過PHP操作自動提交表單

[英]Auto-submit form with PHP action via JavaScript

如果這個問題很天真,請原諒我。 我是JavaScript的新手,並且正在使用某種形式來顯示數據來克服一些挫折。

通過腳本底部JavaScript代碼中的$_GET['icao'] ,將icao代碼傳遞到#depicao <select>菜單。 在頁面加載時, <select>菜單將填充$_GET['icao']值。

its populated value. 在后#depicao <select>填充菜單,我想的形式其填充值自動提交本身。 我的思路是,如果我包括

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

in the script, I can get the script to submit itself it loads with the $_GET['icao'] value. 作為腳本的 ,我可以讓腳本加載$_GET['icao']提交自己。 不幸的是,這沒有奏效。

注意:該代碼包含多個 <input type="submit" name="submit">按鈕。 我相信這是罪魁禍首。

請參見下面的代碼。

<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>

表單自動提交混淆?:

填充#depicao菜單后,我希望表單自動以其填充的值提交自己

我對您的理解是,您希望在填充選擇菜單時自動提交表單? 我認為您的意思是當用戶選擇一個選項時,它應該自動提交。 如果這是您的意思。 請在<select>標記上添加onchange="//submitfunction()"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM