473,569 Members | 2,870 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 1368
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
7387
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 database. Is my understanding correct? At the moment, I pass all my data through htmlentities() before writing to my database. Is this enough? ...
0
1451
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. My problem is that javascript is executed before the mySQL insertion. I actually need the mySQL insertion first, since my javascript function
3
3627
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 number of files specified to upload. I'm trying to solve this by having one INPUT TYPE=FILE box, and, using javascript, each time someone selects...
10
19325
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 able to fill that one out just fine. The second form is multipart/form-data. Unfortunately, I haven't been able to fill that out in a way that makes...
10
2882
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 being passed to the page specified in the Action property of the form. This is on a Windows 2000 Pro PC. I copied the code to another server...
2
1669
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 data with JavaScripted HTML pages. Another posting brought up the fact that a URL can't be more than 1 024 bytes. How, then, could I exchange, say,...
7
1572
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). This server exists on an intranet and would have no 'web' function other than using possibly the existing email server. Is all that I need to do...
17
46488
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: The goal of this tutorial is to design a Data Abstraction Layer (DAL) in PHP, that will allow us to ignore the intricacies of MySQL and focus our...
70
5328
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 different issue than what I was initially having. I am working on storing data collected from a form on my website. I would like the information to be...
0
7618
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7983
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6287
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5223
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3657
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2117
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.