0

I have been trying to find methods to pass variables from client to server What is making it difficult for me is:

  1. I have an unknown number of variables( The program that I am working on pass different number of variables/arrays to server side depending on what the user is doing )
  2. The user could be using any character including & and = therefore if I passed the variables as multiple/one GET variable/s the user could pass = and & which will affect the GET variables

I am looking for methods ( a solution ) to pass variables, preferably arrays, to server side ( like PHP )

Appreciate your time and help :)

Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
user3072635
  • 13
  • 1
  • 3
  • For #2, that's what [URL encoding](http://en.wikipedia.org/wiki/Percent-encoding) is for. `=` becomes `%3D` and `&` becomes `%26`. – JCOC611 Jan 02 '15 at 00:57
  • What role do [PHP session variables](http://php.net/manual/en/reserved.variables.session.php) play in your design thinking (if any)? – hardmath Jan 02 '15 at 01:00

2 Answers2

0

To pass arrays to the server, you would do something like this (note the [] in the name, this signifies an array):

<input type="checkbox" name="myCheckBox[]" value="One" />
<input type="checkbox" name="myCheckBox[]" value="Two" />
<input type="checkbox" name="myCheckBox[]" value="Three" />
<input type="checkbox" name="myCheckBox[]" value="Four" />

Then in php you would do something like this:

foreach($_POST['myCheckBox'] as $value){
    echo "<p>$value</p>";
}

You would then get output that looks like this (assuming all but Two was selected):

One
Three
Four

So assuming you are not using javascript to pass the data, the browser should automatically encode the data. If you are using javascript you can pass the data to the javascript function escape() and it will escape/encode the data to be passed to the server.

Also for jquery you can use the serialize() operation:

$.ajax(
     url: "/to/file.php",
     data: $("#my_form").serialize(), // <form id="my_form">
     success: function(){
         // Do some stuff
     }
);

Here is another way to pass data:

<input type="text" name="names[0]['name']['first']" />
<input type="text" name="names[0]['name']['last']" />
<input type="radio" name="names[0]['gender']" value="male" />
<input type="radio" name="names[0]['gender']" value="female" />

<input type="text" name="names[1]['name']['first']" />
<input type="text" name="names[1]['name']['first']" />
<input type="radio" name="names[1]['gender']" value="male" />
<input type="radio" name="names[1]['gender']" value="female" />

<input type="text" name="names[2]['name']['first']" />
<input type="text" name="names[2]['name']['first']" />
<input type="radio" name="names[2]['gender']" value="male" />
<input type="radio" name="names[2]['gender']" value="female" />

<input type="text" name="names[3]['name']['first']" />
<input type="text" name="names[3]['name']['first']" />
<input type="radio" name="names[3]['gender']" value="male" />
<input type="radio" name="names[3]['gender']" value="female" />

The in the php you would do this for example:

foreach($_POST['names'] as $name){
    echo "<p>" . $name["name"]["first"] . " is a " . $name["gender"] . "</p>";
}
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
0

There are multiple soultions possible for your problems, for

1.) I would suggest passing the variables in JSON-encrypted format. Of course you need to generate an array including all variables you wish to send, and after that, encode it using the javascript function JSON.stringify(yourArray). After that, you can use AJAX to send the data to the server.

If you want to have a form-only (thus no javascript) solution, consider using the HTML 5 naming annotation for multiple input types, e.g. name="yourForm[]".

2.) The characters you specified are automatically stored in JSON annotation. No need to care about them.

On the server, you will just need to use json_decode($yourArray, true); to convert the string back to an array. Note the true flag used for outputting arrays instead of objects.

Tacticus
  • 561
  • 11
  • 24