0

I've followed all SO (and non-SO) links I could find about my issue, but, none helped me. Can anyone point me to what the error here could be? This HTML form (contains bootstrap dropdown and jQuery) won't POST to my PHP file. As I said, I've been debugging for hours on hours now, but can't seem to resolve this.

HTML:

<!DOCTYPE html>
<meta charset="UTF-8">
<body>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"><script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>


<div class="jumbotron">
<div class="container-fluid">
    <h3 id="nmf">Δημιουργία μαθήματος!</h3>

    <form name="newsubj" method="POST" action="create.php" id="newsubj" role="form" class="form-horizontal">
          <label for="fname">Όνομα μαθήματος</label>
          <div class="form-group">
          <input type="text" id="fname" name="fname" class="form-control"></div>
            <div class="form-group">
          <input type="hidden" id="thetiko" class="form-control" name="thetiko"></div>
            <div class="form-group">
            <div class="dropdown">
        <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Θα έλεγες πως το μάθημα σου είναι...
        <span class="caret"></span></button>
            <ul class="dropdown-menu" name="drpdng">
                <li><a onclick="$('#thetiko').val('0');">Θεωρητικό</a></li>
                <li><a onclick="$('#thetiko').val('1');">Θετικό</a></li>
                        </ul>
                    </div> 
                  <input type="submit" class="btn btn-success" value="Δημιουργία!"> 
                  </div>
                </form>
            </div>
          </div>

      <style>

        .btn
        {
            margin-top: 15px;
        }

        body 
        {
            padding: 0;
            text-align: center;
            background-color: #eeeeee;
        }

        #main_container 
        {
            text-align: left;
        }

        #nmf
        {
            margin-bottom: 60px;
        }

      </style>
      </body>


PHP:

<?php

$host = ""; // SQL credentials
$user = "";
$pass = "";
$dbn = "";

echo var_dump($_POST); // returns arr(0)
echo var_dump($_REQUEST); // returns arr(0)

function containsGreek_($strf)
{
    $gr = array(';', 'ς', 'ε', 'ρ', 'τ', 'υ', 'θ', 'ι', 'ο', 'π', 'α', 'σ', 'δ', 'φ', 'γ', 'η', 'ξ', 'κ', 'λ', 'ζ', 'χ', 'ψ', 'ω', 'β', 'ν', 'μ');
    $array = str_split(strtolower($strf));

    foreach ($array as $char)
    {
            if(in_array($char, $gr)) {
                return true;
            }
    }

    return false;
}


function redirect($url)
{
    if (headers_sent()){
      die('<script>window.location=\''.$url.'\';</script>');
    }else{
      header('Location: ' . $url);
      die();
      } 
};


$conn = new mysqli($host, $user, $pass, $dbn);

if ($conn->connect_error)
{
    echo "<h1>Κάτι πάει στραβά με την βάση δεδομένων μας!</h1>";
    die("No connection to MySQL");
}

if($_COOKIE['suffle_teachauthcookie'])
{
    $auth = new mysqli(".com", "", "", ""); // more sql credentials

    $cook = $conn -> real_escape_string($_COOKIE['suffle_teachauthcookie']);
    $name = explode(':', $conn -> real_escape_string($cook), 2);
    $name_ = $name[0];
    $pwwd = $name[1];


  $sql_ = "SELECT * FROM `userdb` WHERE name ='" . sha1($name_) . "' AND pwwd =". "'".$pwwd."'"; 
  $result = $auth->query($sql_);
  $rowl = mysqli_num_rows($result);

  if($rowl > 0)
  {
    $thetiko = $conn -> real_escape_string($_REQUEST["thetiko"]); 
    $subjname = $conn -> real_escape_string($_REQUEST["fname"]);


    $req = 'SELECT * FROM `subjects` WHERE name=\''.$subjname.'\'';
    $result_ = $conn->query($req);
    $rows_ = mysqli_num_rows($result_);

    if(!containsGreek_($subjname))
    {
    if($rows_ < 1)
    {
        $query__ = 'INSERT INTO `subj` (name, id, thetiko) VALUES (\'' . $subjname . '\', \'' . $name_ .'\', ' . $thetiko . ')';

        $conn -> query($query__);
        //redirect('my_subjects.php');
    } else {
        echo 'Αυτό το μάθημα υπάρχει ήδη!';
        echo '<button href="create_new.html" class="btn btn-primary"><-- Πίσω</button>';
        die();
    }       

    } else {
        die('<h3> Παρακαλώ οχι UTF-8 χαρακτήρες! </h3> <button href="create_new.html" class="btn btn-primary"><-- Πίσω</button>');

       }
          }
             }


 else {
    redirect('teach-login.html');
    die();
}


?>

Output:

array(0) { } array(0) { }
Notice: Undefined index: thetiko in /storage/ssd1/971/13158971/public_html/create.php on line 62

Notice: Undefined index: fname in /storage/ssd1/971/13158971/public_html/create.php on line 63
matthias_h
  • 11,356
  • 9
  • 22
  • 40
  • 1
    This seems not to be an issue of the script. I tested your code and got the correct outputs for `var_dump()`. Maybe the redirect at the end is the problem. What does the file `teach-login.html` do? If it checks for a valid login and redirects back to the script, your $_POST data is lost. It is already lost, when redirecting to `teach-login.html`. – mixable Apr 07 '20 at 14:48
  • teach-login is indeed a login page. It redirects the user there if the login cookie is not set. –  Apr 07 '20 at 14:57

0 Answers0