简体   繁体   中英

New to JSON, looking for a simple example with PHP/jquery

I am creating a registration form using PHP and jquery with ajax. Each field is validated via jquery/ajax to a PHP backend. Once the form is all filled out, I would like to have all of the registration data sent via ajax to the PHP backend to be submitted to MySQL.

Someone recommended using JSON, which I am unfamiliar with. OOP is still largely alien to me, and I'm familiar with procedural design.

Here are some code snips for example sake. My registration form has four values that I'd want to submit (email, fullname, charactername, password) (though please don't be distracted from my question, which I outline below)

a bit of jquery:

$('#fullname_field').blur(function(){
        //alert('test');
        var fullname = $('#fullname_field').val();

            $.ajax({
            url: "register_backend.php",
            type: 'POST',
            data: 'fullname=' + fullname,

                success: function(result) {
                //alert("a success");
                //alert(result);
                $('#fullname_status_return').empty();
                $('#fullname_status_return').append(result);

                }
            });

And the corresponding code on the PHP backend:

if (filter_has_var(INPUT_POST, "fullname")) {
    $fullname = mysql_real_escape_string(filter_input(INPUT_POST,'fullname'));
    $testvalue = $fullname;
    $shorttest = $fullname;
    $denychars = stripslashes($fullname);
}
if (strlen($shorttest) < '5')
{
 $echo  "This field is too short.";

} else   {
////////// end short test
////////// if string is not too short, it runs the rest of the code, otherwise this is     all it runs


$result = mysql_query("SELECT * FROM deniednames WHERE deniedname = '$testvalue'",$db);
if (!$result)
die(mysql_error());

$rowcheck = mysql_num_rows($result);
if ($rowcheck > '0') {
    echo "This name is forbidden, please choose another.";
}
}

My question is this:

Breaking that down further, I'm uncertain how to use JSON to capture those values, or conversely, to inject them into JSON, then to provide that JSON to the PHP backend, and then to get the PHP code to accept the JSON input.

I've poured over JSON intros and info on the web, but I'm just not getting it, and would be very grateful for some enlightenment.

(here it is, live: http://www.dixieandtheninjas.net/dynasties/register.php )

JSON is just a way to denote an object. In JS you can create an object literal by just declaring it with { and }, like

var user = {name: 'jeremy'}

You can 'send' that to PHP with the $.ajax function of jQuery (or any like it) and in PHP you can receive that as a normal $_POST or $_GET array.

You can use jQuery to fetch the values from the input's and put them into a data array. Find more on jQuery and ajax here: http://api.jquery.com/jQuery.ajax/ . If you want to send a response in JSON from PHP, you can call json_encode on an array and echo that back.

Does that help?

I would recommend using a jQuery forms plugin. I have used this one: http://malsup.com/jquery/form/ .

Submitting a form is then a simple call such as:

$('#uploadForm').ajaxForm({
  function () {
    alert ('Form submitted') ;
  }
});

It supports JSON as well, and several callbacks and configuration options. However, I think in your case you can get away with using standard form encoding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM