开发者

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: When the user has filled out all of the fields, and hits a submit button, how can I send all of the fields over to PHP, and how can PHP intercept that and make it into usable data that I can validate and then throw into my db?

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜