1

Variables are set and everything is displayed correctly in start.php.I can choose between both displayed variables in the form.

Problem is: The variable $projektcheck or $projektcheckk are not passed to nxtfile.php when selected and the form is submitted. When I check it with the print($_POST['prjkttype']); the variable is undefined and nothing gets printed. It instanly switches to my echo"Data is wrong";

Why does it not get passed to the other file although it is set and selected?

start.php:

<?PHP 
    session_start();
    $projektcheck = $_SESSION['projektname'];
    $projektcheckk = $_SESSION['projektnamee'];                
?>
 [other codestuff]
<script>
function validateForm() {
    var fields = ['prjkttype']

    var i, l = fields.length;
    var fieldname;
    for (i = 0; i < l; i++) {
        fieldname = fields[i];
    }   
    f1.method="POST";
    f1.submit();
}
</script>


[other codestuff]

<form action="nxtfile.php" method="POST" name="f1">
    <select class="selectstyle" name="prjkttype" form="submit">
<?PHP 
    if (isset($projektcheck)) {
        echo"<option value='1'>$projektcheck </option>";    
    } else {
    }  

    if (isset($projektcheckk)) {
        echo"<option value='2'>$projektcheckk </option>";   
    } else {
    }  
?>
    </select>

    <input type=button name=s1 value="Insert" onClick="validateForm()">

</form>
[other codestuff]

nxtfile.php:

<?PHP
session_start();
include ("../connection.php");
$prjkttype=$_POST['prjkttype'];


print($_POST['prjkttype']); 

$checkindb="SELECT * FROM projekte WHERE (projektname = '$prjkttype' )";
$dbcheckk=mysqli_query($con ,$checkindb);
$dbcheckedd=mysqli_fetch_array($dbcheckk);

if($dbcheckedd > 0){
    header ("Location: xy.php");
} else {
    echo "Data is wrong.";
}
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
CodeNewb
  • 35
  • 4
  • 1
    Some sensible code indentation would be a good idea. It helps us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](http://www.php-fig.org/psr/psr-2/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Aug 23 '17 at 14:03
  • 1
    _small note:_ If you dont have anything to do in an `ELSE` then dont code an `ELSE` – RiggsFolly Aug 23 '17 at 14:05
  • Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Aug 23 '17 at 14:06
  • Its a good idea to code an `exit;` after a `header('Location .......');` as the `header` command does not terminate the script when executed, but all output following it will of course get lost – RiggsFolly Aug 23 '17 at 14:07
  • thanks a lot for these helpful informations! I will take a look at it! – CodeNewb Aug 23 '17 at 14:10
  • Have you checked the browsers debugger. I am pretty sure that javascript wont do what you think it will – RiggsFolly Aug 23 '17 at 14:13
  • @RiggsFolly Yep, it doesn't display any error. – CodeNewb Aug 23 '17 at 14:18
  • Be careful. javascrit errors can be a little Shy unless you know what you are looking for – RiggsFolly Aug 23 '17 at 14:19
  • @RiggsFolly even if I delete everything of the script except the `f1.submit();` it still doesn't work. It's kind of mysterious. – CodeNewb Aug 23 '17 at 14:23

1 Answers1

1

You need to set the values to your variables like below. It will pass the value not the variable. This will work:

 if (isset($projektcheck)) {
        echo"<option value='$projektcheck'>$projektcheck </option>";    
    } 

    if (isset($projektcheckk)) {
        echo"<option value='$projektcheckk'>$projektcheckk </option>";   
    } 
pr0cz
  • 509
  • 7
  • 22