I am trying to validate a form with json for the first time. I'm not sure exactly sure on all the details except what I've been reading up on today. Unfortunately there weren't many examples of what I am trying what to do. which is this, I need to check multiple input fields to check if they are in the mysql database. so I need to send them to the php page which checks them all and if it finds one which isn't in the database the foreach loop breaks and that variable value can be used in an alert like $value." is not a registered email";. I'm not exactly how to return the values to my html form page. So I'll just show you what I have. There are probably a million errors. any help you give would be much appreciated.
on the form page:
<script>
$(function(){
$("#send").click(function(e){
e.preventDefault();
$.post("ajax2.php", $("#myForm").serialize(),
function(data){
if(data.check == '2'){
alert(data.message);
}
}, "json");
});
});
</script>
<html>
<form name="myForm" action="ajax.html" method="post" enctype="multipart/form-data">
<input id="file" name="uploaded[]" type="file" multiple /><br>
title: <input name="title" id ="title"><br>
box1: <input type="text" id = "a" name="a"><br>
box2: <input type="text" id = "b" name="b"><br>
box3: <input type="text" id = "c" name="c">
<input type="submit" name="send" id="send" value="send" ">
</form>
</html>
and on ajax2.php:
<?php
$db = new mysqli('localhost', 'root', 'oreo', 'test');
foreach($_POST as $key=> $for) {
if(!empty($for) && $key != 'send' && $key != 'title') {
$usercheck = "SELECT email FROM users WHERE email = '$for'";
$usercheck = $db->query($usercheck);
if($usercheck->num_rows > 0) {$check="1"; continue;}
if($usercheck->num_rows == 0){$check="2"; break;}
}
}
if($check == "2") {
$message = $for." is not a regestered email";
echo json_encode($message);
}
$return_arr["check"] = $check;
$return_arr["message"] = $message;
echo $return_json;