【问题标题】:$_POST problems with PHP & HTML [duplicate]PHP 和 HTML 的 $_POST 问题 [重复]
【发布时间】:2018-11-27 14:01:33
【问题描述】:

我对以下$_POST 有疑问。正如您从代码中看到的那样,一旦 radioButton 具有值 YES 并且您按下提交,您将被重定向到我放置 "IdCantiere" = (value random) 的页面并且? myInput = 这将是不适用于我的 $_POST。 重定向指的是 affidatario.php 页面,我在其中创建了两个变量的$_GET

执行var_dump($_GET) 唯一找到的是idCantiere。如何获取 myInput 的值?

我放置了我的代码和一些屏幕

<html>

<body>

<fieldset>
    <strong>Vuoi inserire un affidatario?</strong>
    <form action="../affidatario.php?idCantiere=<?php echo $idCantiere?>" method="POST" name="prova" onsubmit="return controlla();">
        SI<input type="radio" name="scelta" value="si" />
		 <label name="myLabel" id="myLabel" style="display: none;">Ragione Sociale Affidataria</label><input type="text" id="myInput" style="display: none;"><br>
		NO<input type="radio" name="scelta" value="no" /><br />

        <button type="submit">INVIA</button>
    </form>
</fieldset>
<?php 

$myInput=$_POST["myInput"];


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

</body>

</html>

<?php 

	$idCantiere = $_GET["idCantiere"];
	date_default_timezone_set('Europe/Rome');
    $date = date('Y-m-d');
	chdir("../../../../../Archivio/Cantieri");
	opendir(".");
	 $myInput = $_GET["myInput"];

	if(mkdir("../../../../Archivio/Cantieri/".$date."_".$myInput))
	{
	echo "Cartella account creata con successo! :D";
	}
	echo "<script type='text/javascript'>alert(myInput);</script>";

var_dump($_GET);
	session_start();
    if(!isset($_SESSION["username"])){
    	header('location: ../../../index.php');
    }
    else
    {
	
?>
var_dump

【问题讨论】:

  • 我不知道是不是正确答案,但是,如果我没记错的话,$_POST 必须与 'name' 一起使用,而不是与 'id' 一起使用
  • $ MyInput = $ _ POST [ 'myInput "];我觉得是空的
  • 不是很清楚..你用post设置了$myinput,但是当你编辑你的输入时$_POST保持为空..所以你必须在你的js文件中检索这个值或者直接从$中检索它_POST 在您的 affidatario.php 文件中
  • Didn't you post something like this already? - 似乎是一种转发形式。
  • 是的,但是如果您阅读了问题,您就会明白项目是相同的,而问题是不同的!!! @FunkFortyNiner

标签: javascript php html post get


【解决方案1】:

首先,使用name 以及 id 作为输入标签:

<input type="text" id="myInput" name="myInput" ...

发送表单时,您可以使用$_POST["myInput"]读取值。

其次,决定是否发送表单的controlla 函数总是返回false。这意味着表单实际上从未发送过。更改 window.location.href 会将用户带到一个新页面,但不会将表单数据发送到服务器。

您的意图似乎是更改发送表单的 URL。为此,您可以更改表单的action

    function controlla(form) {
        console.log("oie");
        x = document.prova;
        if (x.scelta.value == "si") {
            return true;
        }
        if (x.scelta.value == "no") {
            alert("Hai risposto no");
            form.action = '../inserimentoCantiere.php'
            return true;
        }
    }

为此,您必须更改 onsubmit 事件处理程序,以便它将表单传递给controlla

    <form ...... onsubmit="return controlla(this);">

【讨论】:

  • 您好 Joni 感谢您的回复 .. 我添加了名称,但尽管如此我不工作 $ _POST :(
  • 是的,您的 controlla 功能也有问题。我暂时删除此答案,但移动应用程序没有该功能
  • 我该如何修改 controlla() 函数?
  • 查看更新,
猜你喜欢
  • 2011-11-24
  • 1970-01-01
  • 2016-05-18
  • 2015-04-11
  • 1970-01-01
  • 2016-06-28
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多