0

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;
2
  • 2
    a little tip: good to see you use mysqli, but you should use prepared statements, as you are vulnerable for mysql injectons now. Commented Jan 28, 2013 at 21:13
  • oh yeah, I will make sure the end product has that. Thanks for the tip. Commented Jan 28, 2013 at 21:16

1 Answer 1

1

You need to echo them back as a json string:

https://www.php.net/manual/en/function.json-encode.php

Then the ajax success handler/callback needs to decode that string into js objects...

How to decode a JSON string?

Talks about how to decode JSON. Odd and somewhat sad that Javascript Object Notation doesn't seem to have a good native decoder in the language for which it is named. (If there is, please correct me!)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.