首页 > 解决方案 > 同一页面中的 PHP 函数

问题描述

我向您提出以下问题:我想确保文本框(根据 radioButton 选择出现和消失)用于接收将用于执行 mkdir 的数据......让我更好地解释一下这个案例当我按下按钮(提交)时,txtBox 的值为“Hello”必须自动创建一个文件夹(通过 mkdir)并且必须重定向到函数 javascript controll () 中包含的地址。

我想了解是否可以将所有内容都放在同一页面上而不必为 PHP 创建其他页面

这是我的尝试之一..但它不起作用:(

<html>

<body>

<fieldset>
    <strong>Did you want to insert another ?</strong>
    <form action="test()" method="POST" name="prova" onsubmit="return controlla();">
        YES<input type="radio" name="scelta" value="yes" />
		<input type="text" id="myInput" style="display: none;"><br>
		NO<input type="radio" name="scelta" value="no" /><br />

        <button type="submit">Submit</button>
    </form>
</fieldset>
<script language="javascript">
    function controlla() {
        console.log("oie");
        x = document.prova;
        if (x.scelta.value == "yes") {
            window.location.href = '../affidatario.php?idCantiere=<?php echo $idCantiere?>'
            return false;
        }
        if (x.scelta.value == "no") {
            alert("NO");
            window.location.href = '../inserimentoCantiere.php'
            return false;
        }
    }
    document.querySelectorAll('input[name="scelta"').forEach(function(a) {
        a.addEventListener("change", function() {
            let textBox = document.getElementById("myInput");
            if (textBox) textBox.style.display = this.value === "yes" ? "block" : "none";
        })
    });
</script>
<?php 

function test(){
  $var = $_POST["myInput"];

if(mkdir("prova/".$date."_".$var.'/Mezzi di Trasporto'))
{
echo "DirectoryCreated";
}
echo "<script type='text/javascript'>alert('$var');</script>";
}

?>

</body>

</html>

标签: javascriptphphtmlpostweb-applications

解决方案


也许通过直接在 PHP 中进行重定向?请注意,我的解决方案(未经测试)使用标头,您必须确保之前没有数据发送到客户端!

<html>

<body>

<fieldset>
    <strong>Did you want to insert another ?</strong>
    <form action="#" method="POST" name="prova">
        YES<input type="radio" name="scelta" value="yes" />
        <input type="text" id="myInput" style="display: none;"><br>
        NO<input type="radio" name="scelta" value="no" /><br />

        <button type="submit">Submit</button>
    </form>
</fieldset>
<script language="javascript">

    document.querySelectorAll('input[name="scelta"').forEach(function(a) {
        a.addEventListener("change", function() {
            let textBox = document.getElementById("myInput");
            if (textBox) textBox.style.display = this.value === "yes" ? "block" : "none";
        })
    });
</script>
<?php 

function test(){
  $var = $_POST["myInput"];

if(mkdir("prova/".$date."_".$var.'/Mezzi di Trasporto'))
{

//Header redirection
header('Location: ../affidatario.php?idCantiere='+ $idCantiere);
} else {
header('Location: ../inserimentoCantiere.php');
}

//Call the function if there is an input :
if(isset($_POST["myInput"])){
test();
}
?>

</body>

</html>

推荐阅读