index.php
<form action="../inc/q_camp.php" method="POST" class="modalForm">
<h3>Campaign</h3><br>
<label>Name</label><br>
<input type="text" name="campName" required><br>
<label>Customer</label><br>
<select name="client" required>
<option value="1">Abc</option>
</select><br>
<label>Start in</label><br>
<input type="date" name="campStart" id="cStrt" required><br>
<label>End in</label><br>
<input type="date" name="campStop" required><br>
<input type="button" name="toStore" value="Next">
</form>
ajax.js
/*This function submit ".modalForm"*/
function submitForm1(){
return $.ajax({
type: "POST",
url: "../inc/q_camp.php",
data: $(".modalForm").serialize()
});
}
/*When the button with name "toStore" is pressed, call submitForm1*/
$("input[name=toStore]").click(function(){
submitForm1();
});
q_camp.php
/*Submit the campaign*/
if(!empty($_POST['campName']) || !empty($_POST['client']) ||
!empty($_POST['campStart']) || !empty($_POST['campStop']))
{
$sql = "INSERT INTO campaigns(cmp_name, customer_id, cmp_start, cmp_stop) VALUES (:cName,:cId,:cStr,:cStp)";
$query = $db->prepare($sql);
$query->bindParam('cName', $_POST['campName']);
$query->bindParam('cId', $_POST['client']);
$query->bindParam('cStr', $_POST['campStart']);
$query->bindParam('cStp', $_POST['campStop']);
$query->execute();
}else print_r("Failed");
The ajax function insert in the database even if the php if statement is not satisfied... If I let the "campName" input empty, I get "Failed" but the row is inserted in the database like "cmp_id 1, cmp_name NOTHING, customer_id 1, cmp_start 00-00-0000, cmp_stop 00-00-0000".
In the database, all field are setted to NOT NULL.
I want to understand how AJAX can access the SQL Query even if the if statement from PHP isn't satisfied, how a row can be inserted even if the column is setted to null and what I'm doing wrong.