同一页中的PHP函数 [英] PHP function in the same page

查看:76
本文介绍了同一页中的PHP函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我想了解是否可以将所有内容都放在同一页面上,而不必为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> 

解决方案

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

I propose you my following problem: I would like to make sure that the textbox (which appears and disappears according to the radioButton choice) is used to receive a data that will be used to perform a mkdir ... Let me explain better the case that the value of the txtBox is "Hello" when I press the button (submit) must automatically create a folder (via mkdir) and must redirect to the address contained in the function javascript controll () ..

I want to understand if it is possible to have everything on the same page and not having to create other pages for PHP

this is one of my attempts .. but it does not work :(

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

解决方案

Maybe by doing redirection directly in PHP ? Be careful, my solution (untested) uses header, you have to be sure there is no data sent to client before !

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

这篇关于同一页中的PHP函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆