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 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. -
//********************************************
-
//
-
// @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 database.
Is my understanding correct?
At the moment, I pass all my data through htmlentities() before writing to
my database. Is this enough? ...
|
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
|
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...
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
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. ...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |