0

So this is for a project for uni. I'm trying to create a form with a username and password and when you click on the connect button, it's supposed to pop up a window alert but it won't work because of my function "myAlert()". I know the if is wrong but i can't figure it out, I'm using my professor's instructions.

<?php

$name=$password="";
if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    $name = test_input($_POST["name"]);
    $password = test_input($_POST["password"]); 
}

function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data;
   } 
   ?>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="container">
<div class="text">
Κωδικός Υπαλλήλου: <br> <input type="text" name="name">
 <br><br> 
Κωδικός: <br> <input type="text" name="password"> 
<br><br>
<button class="connect" onclick="myAlert()">Σύνδεση</button> 
<script> 
function myAlert() {
 if (empty($_POST["name"])) && (empty($_POST["password"])) { 
alert("Δεν συμπληρώσατε τους κωδικούς"); 
} else { 
alert("Επιτυχής σύνδεση"); } 
} 
</script> 
</div> 
</form>

How can I receive the information from the form and depending on the answer, pop up a different message? Do i have to rewrite it?

  • could you format your code so we can see what you're doing wrong please – Jaromanda X Jun 09 '23 at 11:57
  • `function myAlert() {` is **javaascript**, but you're using PHP code in it `empty($_POST["name"])` is PHP ... also, the syntax of `if` (in both JS and PHP is `if (condition) {` ... your `()` are wrong - you have `if (...) && (...) {` ... which is syntactically wrong ... `if ((condition1 && (condition2)) {` ... the inner `()` may be optional depending on what the conditions are ... it's pretty simple – Jaromanda X Jun 09 '23 at 11:58
  • FYI: That `test_input` function is utter nonsense, it _falsifies_ the input data. Remove it, and go read up on prepared statements instead (regarding the issue of preventing SQL injection.) – CBroe Jun 09 '23 at 12:00

0 Answers0