javascript - 同一页面中的 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>
解决方案
也许通过直接在 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>
推荐阅读
- javascript - Electron webview html字符串视频调整大小
- gpflow - GPflow AdamOptimizer 问题
- python - 从 HSV 获取亮度值 - Python
- ios - 无法转换“UIImage”类型的值?到预期的参数类型'[Any]'
- html - 如果类存在,请使用这些样式(SASS)
- c# - 何时在具有默认实现的接口上使用抽象类?
- r - 在 R(终端)和 Rstudio 中使用 lm() 会给出具有相同输入数据的不同输出
- django - 如何在模型上添加到期日期功能?
- javascript - 如何为数组中的每个单击元素动态更改信息模式 innerHTML
- php - 如何将可重复的数据记录到数据库中