如何用isset php拦截javascript写的提交按钮[已关闭]

3pvhb19x  于 2023-03-16  发布在  Java
关注(0)|答案(1)|浏览(93)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题?**添加详细信息并通过editing this post阐明问题。

昨天关门了。
Improve this question
首先感谢任何用户的网站花时间来回答我的问题。
基本上,正如标题所示,我的问题的目标是使用经典的phpisset拦截用javascript编写的表单提交
代码:

<form action="" id="regForm" name="NOME_FORM"   method="post" role="search" action="<?php echo esc_url( home_url( '/grow-it' )  ); ?>">
            <h1>Register:</h1>
            <!-- One "tab" for each step in the form: -->
            <?php foreach ( $grow as $risultato )
            {
                $username = $risultato->username;
                $insta = "https://www.instagram.com/".$username; ?>
                <div class="tab">

                    <a target="_blank" href="<?php echo $insta; ?>" type="button" id="doStuffBtn" onclick="doStuff()">
                        Followe <?php echo $username; ?> per andare avanti
                    </a>
                </div>
                <?php
            }?>
    </div>

            <div class="col-md-12">
                <div style="overflow:auto;">
                    <div style="float:right;">
                        <button type="button" id="prevBtn" onclick="nextPrev(-1)">Previous</button>

                        <button type="button" name="nextBtn" id="nextBtn" onclick="nextPrev(1)">Next</button>
                    </div>
                </div>
                <!-- Circles which indicates the steps of the form: -->
                <div style="text-align:center;margin-top:40px;">
                    <?php foreach ( $grow as $risultato ) {?>
                        <span class="step"></span>
                    <?php } ?>
                </div>
            </div>s
    </form>

约旦:

var currentTab = 0; // Current tab is set to be the first tab (0)
showTab(currentTab); // Display the current tab

var nextAvailable = false;

function showTab(n) {
    // This function will display the specified tab of the form...
    var x = document.getElementsByClassName("tab");
    x[n].style.display = "block";
    //... and fix the Previous/Next buttons:
    if (n == 0) {
        document.getElementById("prevBtn").style.display = "none";
    } else {
        document.getElementById("prevBtn").style.display = "inline";
    }
    if (n == (x.length - 1)) {
        document.getElementById("nextBtn").innerHTML = "Submit";
    } else {
        document.getElementById("nextBtn").innerHTML = "Next";
    }
    //... and run a function that will display the correct step indicator:
    fixStepIndicator(n)
}

function doStuff(n) {
    nextAvailable = true;
}

function nextPrev(n) {
    if (nextAvailable || n < 0) {
        nextAvailable = false;

        // This function will figure out which tab to display
        var x = document.getElementsByClassName("tab");
        // Exit the function if any field in the current tab is invalid:
        if (n == 1 && !validateForm()) return false;
        // Hide the current tab:
        x[currentTab].style.display = "none";
        // Increase or decrease the current tab by 1:
        currentTab = currentTab + n;
        // if you have reached the end of the form...
        if (currentTab >= x.length) {
            // ... the form gets submitted:
            document.getElementById("regForm").submit();
            return false;
        }
        // Otherwise, display the correct tab:
        showTab(currentTab);
    }
}

function validateForm() {
    // This function deals with validation of the form fields
    var x, y, i, valid = true;
    x = document.getElementsByClassName("tab");
    y = x[currentTab].getElementsByTagName("input");
    // A loop that checks every input field in the current tab:
    for (i = 0; i < y.length; i++) {
        // If a field is empty...
        if (y[i].value == "") {
            // add an "invalid" class to the field:
            y[i].className += " invalid";
            // and set the current valid status to false
            valid = false;
        }
    }
    // If the valid status is true, mark the step as finished and valid:
    if (valid) {
        document.getElementsByClassName("step")[currentTab].className += " finish";
    }
    return valid; // return the valid status
}

function fixStepIndicator(n) {
    // This function removes the "active" class of all steps...
    var i, x = document.getElementsByClassName("step");
    for (i = 0; i < x.length; i++) {
        x[i].className = x[i].className.replace(" active", "");
    }
    //... and adds the "active" class on the current step:
    x[n].className += " active";
}

尝试拦截:

if(isset($_POST["nextBtn"]) && $_POST["nextBtn"] == "submit")
    {
       ?>
        <script type="text/javascript">
            window.location.href = "www.google.com"
        </script>
        <?php
    }

我该怎么解决呢?先谢了
如何在php中调用表单提交

im9ewurl

im9ewurl1#

提交表单的不是按钮,而是JavaScript代码,因此按钮不会在表单提交中包含值。
然而,你可以做的是在表单提交之前使用JavaScript代码添加一个表单值,然后服务器端代码可以检查这个值。

// add a new form element:
let el = document.createElement("input");
el.type = "hidden";
el.name = "clickedNext";
el.value = "submit";
document.getElementById("regForm").appendChild(el);

这将在表单中添加一个元素:

<input type="hidden" name="clickedNext" value="submit">

只需在JavaScript代码中提交表单的任何操作 * 之前 * 包含该逻辑。
然后在服务器端代码中检查 that 元素:

if(isset($_POST["clickedNext"]) && $_POST["clickedNext"] == "submit")

相关问题