$ _POST PHP和HTML问题

时间:2018-11-27 14:01:33

标签: javascript php html post get

我对以下$_POST有疑问。从代码中可以看到,当radioButton的值为 YES 并按Submit时,您将被重定向到我放置"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 Var_DUMP

1 个答案:

答案 0 :(得分:0)

首先,在输入标签中使用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);">