Jump to content

Sending dynamic quantity of inputs to next page


DeX

Recommended Posts

I have a quoting system that some of you may be familiar with from my previous questions, this is another piece of it I'm trying to clean up.

The user can add a door or window to the quote and can also associate options with the item. For example, they can add the door, then supply the type of opening mechanism (chain, motor....), then how many windows are in the door, then which storey the door will be installed on. Then they can add another door if they wish.

 

Currently I'm using a jQuery popup like a lightbox (Bootstrap modal dialog) to ask the user all of the proper questions about the door, then when they click the button to save the door, I'm serializing all of the inputs from that lightbox and assigning them to a hidden variable farther down the page. No matter how many doors they add, it all gets serialized into a JSON array into that hidden variable and that variable is submitted to the next page to be saved to the database when the user saves the quote.

 

I know of no other way of transferring all of this data to the next page other than collecting, serializing and submitting as a JSON array. Am I doing it correctly? Thanks.

Link to comment
Share on other sites

JSON might be useful if the inputs vary a lot - like there isn't a consistent structure every time you prompt for particular details regarding an item. Make sure to validate it on the next page so that someone can't tamper with the data before submitting the form.

 

The normal answer is to use arrays, which could also help here but probably isn't the only thing you need.

<input type="hidden" name="whatever[]" value="1">
<input type="hidden" name="whatever[]" value="2">
<input type="hidden" name="whatever[]" value="3">
and $_POST["whatever"] is an array.
Link to comment
Share on other sites

JSON might be useful if the inputs vary a lot - like there isn't a consistent structure every time you prompt for particular details regarding an item. Make sure to validate it on the next page so that someone can't tamper with the data before submitting the form.

 

The normal answer is to use arrays, which could also help here but probably isn't the only thing you need.

<input type="hidden" name="whatever[]" value="1">
<input type="hidden" name="whatever[]" value="2">
<input type="hidden" name="whatever[]" value="3">
and $_POST["whatever"] is an array.

 

When adding doors, it is always a set number of inputs and they're always the same. The same options are available for every door. Then there are different input options for windows but they're always the same for each window. Then the same for a few other items with a total of about 10 different items like this.

Link to comment
Share on other sites

Right, they're the same for particular types of items, but not for all items. That means the data structure will have to vary somehow, and you can accomplish that with a JSON string or by creating an arbitrary array.

To refine the example I gave earlier,

<input type="hidden" name="items[0]" value="door">
<input type="hidden" name="options[0][opening]" value="chain">
<input type="hidden" name="options[0][numwindows]" value="2">
<input type="hidden" name="options[0][storey]" value="1">
0 is the first item added to the order. It is a "door". The door comes with multiple options, which arrive in your code as an array in $_POST.

$option_keys = array(
	"door" => array("opening", "numwindows", "storey")
);

$items = array();
foreach ($_POST["items"] as $n => $type) {
	if (!isset($option_keys[$type])) {
		// error: invalid item type
		continue;
	}

	$item = array("type" => $type, "options" => array());
	if (isset($_POST["options"][$n])) {
		// extract options from the form and validate against $option_keys
		$item["options"] = array_intersect_key($_POST["options"][$n], array_flip($option_keys[$type]));
		if (count($item["options"]) != count($option_keys[$type])) {
			// error: not all options set in form
			continue;
		}
	}
	$items[$n] = $item;
}

// $items = array(
//	0 => array(
//		type => "door",
//		options => array(
//			opening => "chain",
//			numwindows => 2,
//			storey => 1
//		)
//	)
// )
The JSON option isn't that much different.

<input type="hidden" name="items[0][type]" value="door">
<input type="hidden" name="items[0][options]" value="{"opening":"chain","numwindows":2,"storey":1}">
$option_keys = array(
	"door" => array("opening", "numwindows", "storey")
);

$items = array();
foreach ($_POST["items"] as $n => $item) {
	if (!isset($item["type"], $option_keys[$item["type"]])) {
		// error: invalid item type
		continue;
	}
	if (!isset($item["options"])) {
		$item["options"] = array();
	}

	// extract options from the form and validate against $option_keys
	$options = array_intersect_key($item["options"], array_flip($option_keys[$item["type"]]));
	if (count($options) != count($option_keys[$item["type"]])) {
		// error: not all options set in form
		continue;
	}
	$item["options"] = $options;

	$items[$n] = $item;
}

// $items = array(
//	0 => array(
//		type => "door",
//		options => array(
//			opening => "chain",
//			numwindows => 2,
//			storey => 1
//		)
//	)
// )
And there are multiple variations possible for both of those, depending on what's easiest to work with both in the Javascript and the PHP.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.