I have a form with some check boxes and some select fields:
<input type="checkbox" name="service" value="1">
<select id="period1">
<option value="3">3</option>
<option value="6">6</option>
</select>
<input type="checkbox" name="service" value="2">
<select id="period2">
<option value="3">3</option>
<option value="6">6</option>
</select>
I'm trying to pass the selected check box value and and a related select field value. For example: if the user checks the first check box the script should read "period1" value.
The question is: how to form the data using javascript and pass it to PHP using JSON. In PHP, I'd like to have kind of:
array => [0]
service["id"] => 1
service["period"] => 6
Thank you :)
use json_encode()
to encode in json format and json_decode()
to decode the data back in original form.
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
var_dump(json_decode($json, true));
?>
Convert form data to JavaScript object with jQuery
This link maybe help to you. And if you had json object, you can pass the server in post, like:
$.post("address", {json:jsonObj}, function(result){...});
I guess it is correct syntax, correct me if i mistake.