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
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. -
//********************************************
-
//
-
// @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;value definition;(repeated per element)}
-
//
-
// Object
-
// O:strlen(object name):object name:object size:
-
// {s:strlen(property 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:"value";',
-
// "true => 'value'" equates to 'i:1;s:5:"value";',
-
// "false => 'value'" equates to 'i:0;s:5:"value";',
-
// "array(whatever 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:10;s:3:"bar";i:20}
-
//
-
// Double/float values are represented as:
-
//
-
// d:0.23241446
-
//
-
function serialize(content) {
-
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(content[i]);
-
retVal += '}';
-
break;
-
case 'Boolean':
-
retVal += 'b:' + (content * 1) + ';';
-
break;
-
case 'Number':
-
retVal += ((parseInt(content) < 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(content[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;
-
}
-
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: -
//********************************************
-
//
-
// @sizeof
-
//
-
// Counts the number of elements (array/object) or length (string)
-
//
-
function sizeof(content) {
-
if(content == null)
-
return 0;
-
-
if(typeof(content.length) != 'undefined')
-
return content.length;
-
-
if(getType(content) == 'Object') {
-
var retVal = 0;
-
for(p in content)
-
retVal++;
-
return retVal;
-
}
-
-
// Otherwise, I have no idea what to do with you.
-
return -1;
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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.
...
|
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...
|
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...
|
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...
| |
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...
|
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). ...
|
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:...
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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,...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |