I have Form where a user inputs the data, presses submit and the data is saved in MySQL(php). The Form can only be submitted if all the fields are not empty(javascript).
The problem is this: if I use the code without the php(to store the data in mysql), the javascript for checking empty inputs works. If I add my php code, the javascript wont work. I can submit forms even if they are all empty and I don't want that.
The form:
<form action="index.php" method="post" role="form" onsubmit="return validateForm()">
<div class="form-group">
<label for="name">Vardas</label>
<input type="text" class="form-control" name="name" placeholder="Vardas" >
</div>
<div class="form-group">
<label for="lastName">Pavardė</label>
<input type="text" class="form-control" name="lastName" placeholder="Pavardė" >
</div>
<div class="form-group">
<label for="ak">Asmens kodas</label>
<input type="text" class="form-control" name="ak" placeholder="Asmens kodas" >
</div>
<div class="form-group">
<label for="adress">Adresas</label>
<input type="text" class="form-control" name="adress" placeholder="Adresas" >
</div>
<div class="form-group">
<label for="number">Telefono nr.</label>
<input type="text" class="form-control" name="number" placeholder="Telefono nr." >
</div>
<div class="form-group">
<label for="email">El. pašto adresas</label>
<input type="email" class="form-control" name="email" placeholder="El. paštas" >
</div>
<div class="form-group">
<label for="preke">Prekės pasirinkimas</label>
<select class="form-control" name="preke" >
<?php
while($preke = mysqli_fetch_array($r)){ //Creates a loop to loop through results
echo "<option>" . $preke['Pavadinimas'] . "</option>"; //$row['index'] the index here is a field name
}?>
The JavaScript:
<script>
function validateForm() {
var a = document.forms["myForm"]["name"].value;
if (a == null || a == "") {
alert("Visi laukai turi būti užpildyti");
return false;
}
var b = document.forms["myForm"]["lastName"].value;
if (b == null || b == "") {
alert("Visi laukai turi būti užpildyti");
return false;
}
var c = document.forms["myForm"]["ak"].value;
if (c == null || c == "") {
alert("Visi laukai turi būti užpildyti");
return false;
}
var d = document.forms["myForm"]["adress"].value;
if (d == null || d == "") {
alert("Visi laukai turi būti užpildyti");
return false;
}
var e = document.forms["myForm"]["number"].value;
if (e == null || e == "") {
alert("Visi laukai turi būti užpildyti");
return false;
}
var f = document.forms["myForm"]["email"].value;
if (f == null || f == "") {
alert("Visi laukai turi būti užpildyti");
return false;
}
var g = document.forms["myForm"]["preke"].value;
if (g == null || g == "") {
alert("Visi laukai turi būti užpildyti");
return false;
}
}
</script>
The PHP:
if(isset($_POST['Submitted']) == 1) {
$query = "INSERT INTO uzsakymai (Vardas, Pavarde, Ak, Adresas, Telefonas, pastas, preke) VALUES ('$_POST[name]', '$_POST[lastName]','$_POST[ak]','$_POST[adress]','$_POST[number]','$_POST[email]','$_POST[preke]')";
$result = mysqli_query($dbc, $query);
if($result) {
echo '<p>Forma issaugota</p>';
} else {
echo '<p>Page could not be added because: '.mysqli_error($dbc);
echo '<p>'.$query.'</p>';
}
}
?>
Aucun commentaire:
Enregistrer un commentaire