-1

I have a problem with the following $_POST. As you see from the code once the radioButton has value YES and you press submit you are redirected to the page where I put "IdCantiere" = (value random) and? myInput = which would be the $_POST that does not work for me. The redirect refers to the affidatario.php page in which I make the $_GET of both variables.

executing a var_dump($_GET) the only thing that finds is idCantiere. How can I get the value of myInput?

I place my code and some screens

<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
Sfili_81
  • 2,377
  • 8
  • 27
  • 36
AlexLogic
  • 91
  • 9
  • I do not know if it's the correct answer, but, if I'm not mistaken, $ _POST must be used with 'name' not with 'id' – Leo Nov 27 '18 at 14:07
  • `$ MyInput = $ _ POST [ 'myInput "];` I think it's empty – Leo Nov 27 '18 at 14:07
  • it's not very clear..you set $myinput with post, but when you edit your input $_POST remain empty.. so you have to retrieve this value in your js file or retrieve it directly from $_POST in your affidatario.php file – Sfili_81 Nov 27 '18 at 14:08
  • 1
    [Didn't you post something like this already?](https://stackoverflow.com/q/53494554/1415724) - Seems like a form of repost. – Funk Forty Niner Nov 27 '18 at 14:11
  • yes but if you read the questions you understand that the project is the same, while the questions are different !!! @FunkFortyNiner – AlexLogic Nov 27 '18 at 14:12
  • so this is an undefined index question then – Funk Forty Niner Nov 27 '18 at 14:13
  • So @AlexLogic read the value in the affidatario.php the value $_POST["myInput"] and read how $_GET and $_POST works – Sfili_81 Nov 27 '18 at 14:15
  • Do you ever actually send the form? Your function controlla simply loads a new page by setting window.location.href – Joni Nov 27 '18 at 14:18
  • _“How can I get the value of myInput?”_ - by _sending_ it correctly in the first place. Did it not make you think, that in your debug output, the value of `idCantiere` was shown as `2053813695?myInput=` …? That’s because you messed up the query string syntax - it _starts_ with a question mark, but multiple name=value pairs are separated from each other by an ampersand (whereas you tried to use a _second_ question mark.) – misorude Nov 27 '18 at 14:19
  • I'm sorry that I made so many mistakes but I'm a beginner and I do not understand where I'm wrong ... – AlexLogic Nov 27 '18 at 14:20
  • @misorude so I should put something like this = `../affidatario.php?idCantiere=&myInput= ` – AlexLogic Nov 27 '18 at 14:24
  • Yes, of course. That’s how a query string works. – misorude Nov 27 '18 at 14:25
  • okay now the result of vardump is `array(2) { ["idCantiere"]=> string(9) "867012990" ["myInput"]=> string(0) "" }` – AlexLogic Nov 27 '18 at 14:26
  • but i don't understand why the fileds is NULL – AlexLogic Nov 27 '18 at 14:27
  • @FunkFortyNiner but why did you put me as a duplicate? what does that have to do with my question? * Confused * – AlexLogic Nov 27 '18 at 15:24

1 Answers1

0

First, use name as well as id for your input tag:

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

When the form is sent, you can read the value using $_POST["myInput"].

Second, your controlla function that decides whether to send the form always returns false. This means the form is actually never sent. Changing window.location.href takes the user to a new page, but without sending the form data to the server.

It looks like your intention is to change the URL where the form is sent. To do that, you can change the form's 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;
        }
    }

For this to work you have to change the onsubmit event handler so that it passes the form to controlla:

    <form ...... onsubmit="return controlla(this);">
Joni
  • 108,737
  • 14
  • 143
  • 193