【问题标题】:PHP function in the same page同一页面中的 PHP 函数
【发布时间】:2019-04-28 21:23:53
【问题描述】:

我向您提出以下问题:我想确保文本框(根据 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>

【问题讨论】:

  • 请清理您的代码,并只使用英文,以便我们更好地理解您的逻辑流程。
  • 清理并改为英文 :) @Ahmad
  • 您可以创建一个模板 PHP 页面,该页面将被复制到您创建的每个新目录中。
  • 什么对不起? :O

标签: javascript php html post web-applications


【解决方案1】:

也许通过直接在 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>

【讨论】:

  • 对不起,我没听懂……你能说清楚点吗?
  • 在您的代码中,我没有看到 test() 函数的调用
  • 我把它放在表单上 action="test()"
  • 哦,我明白了,action="test()" 正在尝试查找页面“test()”。您必须在 PHP 中调用该函数,而不是 HTML/JS(您不能使用 HTML 等客户端语言直接调用 PHP 函数)
  • 该死的我不知道..你有什么解决办法吗?
猜你喜欢
  • 1970-01-01
  • 2021-06-19
  • 2016-10-05
  • 2016-05-18
  • 1970-01-01
  • 2013-12-19
  • 2013-05-21
  • 1970-01-01
  • 2011-06-23
相关资源
最近更新 更多