Data From a Form to JavaScript to PHP and onto MySQL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nathj
    Recognized Expert Contributor
    • May 2007
    • 937

    #1

    Data From a Form to JavaScript to PHP and onto MySQL

    Hi Everyone,

    I am working on a project that has only two pages, a login page and a mainpage. The system I am building is a data handling system, it has now public face.

    The issue I am stuggling on is passing data around. I have a main input form for this and the first step takes some set up data. This is then passed through JavaScript to the next step in the Wizard using AJAX. This ony has a few items in it so this could be done on the query string. The trouble is that the second step contains a lot of data in it, and so the query string is not possible.

    When the user the hits the first step they can specify how many sets of data they want so after the first set of data they are presented with another blank form. The plan I have is to send the form element ID's to JavaScript and extract the values, generate some JSON and then send that to the PHP page updates the database and presents a blank form again.

    I hope that is a clear explanation of what I'm trying to do. The trouble I have is how to generate the JSON and decode it poperly in the PHP.

    Any help on this would be great as I am totally stuck and getting very frutrated.

    Cheers
    nathj
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Nathj.

    PHP has some nifty JSON functions.

    In the past, I wrote a serialize() function in JavaScript that may be a useful alternative. I am not really sure which one is better; JavaScript prefers JSON, but PHP prefers serialization. So it goes.

    [code=javascript]
    //*************** *************** **************
    //
    // @serialize
    //
    // Outputs data for use by PHP's @unserialize.
    //
    // Anatomy of a serialize()'ed value:
    // ----------------------------------
    //
    // String
    // s:size:value;
    //
    // Integer
    // i:value;
    //
    // Boolean
    // b:value; (does not store "true" or "false", does store '1' or '0')
    //
    // Null
    // N;
    //
    // Array
    // a:size:{key definition;valu e definition;(rep eated per element)}
    //
    // Object
    // O:strlen(object name):object name:object size:
    // {s:strlen(prope rty name):property name:property definition;
    // (repeated per property)}
    //
    // String values are always in double quotes
    // Array keys are always integers or strings
    // "null => 'value'" equates to 's:0:"";s:5:"va lue";',
    // "true => 'value'" equates to 'i:1;s:5:"value ";',
    // "false => 'value'" equates to 'i:0;s:5:"value ";',
    // "array(what ever the contents) => 'value'" equates to an "illegal
    // offset type" warning because you can't use an array as a key;
    // however, if you use a variable containing an array as a key,
    // it will equate to 's:5:"Array";s: 5:"value";', and attempting
    // to use an object as a key will result in the same behavior as
    // using an array will.
    //
    // All strings appear inside quotes. This applies to string values,
    // object class names and array key names. For example:
    //
    // s:3:"foo"
    // O:7:"MyClass":1 :{...
    // a:2:{s:3:"bar"; i:42;...
    //
    // Object property names and values are delimited by semi-colons, not colons.
    // For example:
    //
    // O:7:"MyClass":2 :{s:3:"foo";i:1 0;s:3:"bar";i:2 0}
    //
    // Double/float values are represented as:
    //
    // d:0.23241446
    //
    function serialize(conte nt) {
    var retVal = ''
    var myType = getType(content );

    switch(myType) {
    case 'Array':
    retVal += 'a:' + content.length + ':{';
    for(var i = 0; i < content.length; i++)
    retVal += 'i:' + i + ';' + serialize(conte nt[i]);
    retVal += '}';
    break;
    case 'Boolean':
    retVal += 'b:' + (content * 1) + ';';
    break;
    case 'Number':
    retVal += ((parseInt(cont ent) < content) ? 'd' : 'i') + ':' + content + ';';
    break;
    case 'Object':
    // Because PHP objects and JS objects are like night and day,
    // we're going to treat JS objects as PHP associative
    // arrays.
    retVal += 'a:' + sizeof(content) + ':{';
    for(var p in content)
    retVal += 's:' + p.length + ':"' + p + '";' + serialize(conte nt[p]);
    retVal += '}';
    break;
    case 'String':
    retVal += 's:' + content.length + ':"' + content + '";';
    break;
    case 'undefined':
    retVal += 'N;';
    default:
    // References and HTML Objects, etc. go here.
    retVal += 's:' + eval('\'' + content + '\'').length + ':"' + content + '";';
    }

    return retVal;
    }
    [/code]

    This function relies on getType(), defined here.

    It also relies on a sizeof() function which runs through an object and counts its properties.

    Something like this:
    [code=javascript]
    //*************** *************** **************
    //
    // @sizeof
    //
    // Counts the number of elements (array/object) or length (string)
    //
    function sizeof(content) {
    if(content == null)
    return 0;

    if(typeof(conte nt.length) != 'undefined')
    return content.length;

    if(getType(cont ent) == 'Object') {
    var retVal = 0;
    for(p in content)
    retVal++;
    return retVal;
    }

    // Otherwise, I have no idea what to do with you.
    return -1;
    }
    [/code]
    Last edited by pbmods; Nov 26 '07, 04:25 PM.

    Comment

    Working...