473,507 Members | 2,441 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Data From a Form to JavaScript to PHP and onto MySQL

nathj
938 Recognized Expert Contributor
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
Nov 22 '07 #1
1 1363
pbmods
5,821 Recognized Expert Expert
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.

Expand|Select|Wrap|Line Numbers
  1. //********************************************
  2. //
  3. //    @serialize
  4. //
  5. //    Outputs data for use by PHP's @unserialize.
  6. //
  7. //    Anatomy of a serialize()'ed value:
  8. //    ----------------------------------
  9. //
  10. //    String
  11. //        s:size:value;
  12. //
  13. //    Integer
  14. //        i:value;
  15. //
  16. //    Boolean
  17. //        b:value; (does not store "true" or "false", does store '1' or '0')
  18. //
  19. //    Null
  20. //        N;
  21. //
  22. //    Array
  23. //        a:size:{key definition;value definition;(repeated per element)}
  24. //
  25. //    Object
  26. //        O:strlen(object name):object name:object size:
  27. //            {s:strlen(property name):property name:property definition;
  28. //            (repeated per property)}
  29. //
  30. //    String values are always in double quotes
  31. //    Array keys are always integers or strings
  32. //        "null => 'value'" equates to 's:0:"";s:5:"value";',
  33. //        "true => 'value'" equates to 'i:1;s:5:"value";',
  34. //        "false => 'value'" equates to 'i:0;s:5:"value";',
  35. //        "array(whatever the contents) => 'value'" equates to an "illegal
  36. //            offset type" warning because you can't use an array as a key;
  37. //            however, if you use a variable containing an array as a key,
  38. //            it will equate to 's:5:"Array";s:5:"value";', and attempting
  39. //            to use an object as a key will result in the same behavior as
  40. //            using an array will.
  41. //
  42. //    All strings appear inside quotes. This applies to string values,
  43. //        object class names and array key names. For example:
  44. //
  45. //        s:3:"foo"
  46. //        O:7:"MyClass":1:{...
  47. //        a:2:{s:3:"bar";i:42;...
  48. //
  49. //    Object property names and values are delimited by semi-colons, not colons.
  50. //        For example:
  51. //
  52. //        O:7:"MyClass":2:{s:3:"foo";i:10;s:3:"bar";i:20}
  53. //
  54. //    Double/float values are represented as:
  55. //
  56. //        d:0.23241446
  57. //
  58. function serialize(content) {
  59.     var retVal = ''
  60.     var myType = getType(content);
  61.  
  62.     switch(myType) {
  63.         case 'Array':
  64.             retVal += 'a:' + content.length + ':{';
  65.             for(var i = 0; i < content.length; i++)
  66.                 retVal += 'i:' + i + ';' + serialize(content[i]);
  67.             retVal += '}';
  68.             break;
  69.         case 'Boolean':
  70.             retVal += 'b:' + (content * 1) + ';';
  71.             break;
  72.         case 'Number':
  73.             retVal += ((parseInt(content) < content) ? 'd' : 'i') + ':' + content + ';';
  74.             break;
  75.         case 'Object':
  76.             //    Because PHP objects and JS objects are like night and day,
  77.             //        we're going to treat JS objects as PHP associative
  78.             //        arrays.
  79.             retVal += 'a:' + sizeof(content) + ':{';
  80.             for(var p in content)
  81.                 retVal += 's:' + p.length + ':"' + p + '";' + serialize(content[p]);
  82.             retVal += '}';
  83.             break;
  84.         case 'String':
  85.             retVal += 's:' + content.length + ':"' + content + '";';
  86.             break;
  87.         case 'undefined':
  88.             retVal += 'N;';
  89.         default:
  90.             //    References and HTML Objects, etc. go here.
  91.             retVal += 's:' + eval('\'' + content + '\'').length + ':"' + content + '";';
  92.     }
  93.  
  94.     return retVal;
  95. }
  96.  
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:
Expand|Select|Wrap|Line Numbers
  1. //********************************************
  2. //
  3. //    @sizeof
  4. //
  5. //    Counts the number of elements (array/object) or length (string)
  6. //
  7. function sizeof(content) {
  8.     if(content == null)
  9.         return 0;
  10.  
  11.     if(typeof(content.length) != 'undefined')
  12.         return content.length;
  13.  
  14.     if(getType(content) == 'Object') {
  15.         var retVal = 0;
  16.         for(p in content)
  17.             retVal++;
  18.         return retVal;
  19.     }
  20.  
  21.     //    Otherwise, I have no idea what to do with you.
  22.     return -1;
  23. }
  24.  
Nov 26 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
7374
by: Randell D. | last post by:
Folks, I've heard of buffer overflows being used/abused by hackers and believe one method to reduce this from happening is to check the length of my form data before writing it to my MySQL...
0
1445
by: Ronan | last post by:
Hi, I have a problem with a form using the PHP PEAR HTML_QuickForm package & javascript: I want to record the content of my form into a mySQL database then execute a javascript function. ...
3
3622
by: Jason | last post by:
Hi folks, I'm trying to create a section of a website with a unique file upload utility. The problem is that in most code and components I find to pass multipart/form data, you need to know the...
10
19307
by: Gregory A Greenman | last post by:
I'm trying to write a program in vb.net to automate filling out a series of forms on a website. There are three forms I need to fill out in sequence. The first one is urlencoded. My program is...
10
2877
by: Noozer | last post by:
Below is some ASP, HTML and javascript. It is part of a page used to maintain a small database. This code did work at one time, but has since stopped. For some reason the data on my form is not...
2
1666
by: Joakim Braun | last post by:
Greetings, I'm playing around with a web-based interface to MySQL that would let you manage, create and use databases. So far I'm using PHP to talk to MySQL and query strings to exchange the...
7
1568
by: cover | last post by:
I have a form that writes to an MySQL database just fine but would like to email people to give them a heads up that an entry was made under their name (1 of 6 names on writing to the database). ...
17
46458
Motoma
by: Motoma | last post by:
This article is cross posted from my personal blog. You can find the original article, in all its splendor, at http://motomastyle.com/creating-a-mysql-data-abstraction-layer-in-php/. Introduction:...
70
5306
mideastgirl
by: mideastgirl | last post by:
I have recently been working on a website for an honors association, and have a lot of difficulty but have found help from those on this site. I would like to see if I can get some more help on a...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7111
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7319
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7485
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.