如何从HTML表单发送JSON请求并在PHP中接收 [英] How to send JSON request from HTML form and receive in PHP
问题描述
我使用具有多个字段的HTML表单. 但是我需要将选择字段(下拉列表)中的值转换为php aray. 我的表单包含以下字段:
I use a HTML form with several fields. But i need to have the value from the select fields ( dropdown ) translated into a php aray. My form contains below fields:
City - Text
IN - Text/Date
OUT - Text/Date
Rooms - Select ( 1->5 )
Adults - Select ( 1->5 )
Childs - Select ( 0->5 )
我需要接收的php数组是
The php array that i need to receive is
Example If Rooms is - 1 and Adults - 2 the response must be:
$rooms[] = array(array("paxType" => "Adult"), array("paxType" => "Adult"));
Example If Rooms is - 2 and Adults - 3 the response must be:
$rooms[] = array(array("paxType" => "Adult"), array("paxType" => "Adult"));
$rooms[] = array(array("paxType" => "Adult"));
我正在使用以下代码
$Adults = intval($_POST["Adults"]);
$rooms = array();
for ($x = 0; $x < $Adults; $x++) {
array_push($rooms,array("paxType" => "Adult"));
}
但是我只能得到1个房间和1个成人的答复.
but i am receive onbly the response with 1 room and 1 adult.
推荐答案
即使使用简单的HTML表单提交(也可以通过JSON XHR请求完成),也很容易做到这一点
It is easy to do this, even with plain HTML Form Submit ( can be done with JSON XHR request too)
我假设您的SELECT
HTML表单元素名称为"adultCount
"
I assume that your SELECT
HTML form element name is "adultCount
"
$adultCount = intval($_GET["adultCount"]);
$rooms = array();
for ($x = 0; $x < $adultCount; $x++) {
array_push($rooms,array("paxType" => "Adult"));
}
仅供参考,即使我们可以将SOAP用于网页,也不会使用SOAP.
FYI, We don't use SOAP for web pages, even though it can be done ( it is unnecessary).
这篇关于如何从HTML表单发送JSON请求并在PHP中接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!