Razvan George
Razvan George

Reputation: 117

How to send a JSON object to a php from HTMl form

I am tryiong to integrate a XML API solution for Hotel Booking, and i want to use JSON to send onject to php so the system can return me the responses. The form is :

<form action="https://www.bookingassist.ro/test/html/hotel-list-view.php" method="post">
<label>Destinatie</label>
<input type="text" value="Oras" name="City" id="autocomplete" placeholder="Oras sau regiune" />
<label>Check In</label>
<input type="text" name="In" value="AAAA/LL/ZZ" class="input-text full-width" placeholder="yyyy/mm/dd" />
label>Check Out</label>
<input type="text" name="Out" value="AAAA/LL/ZZ" class="input-text full-width" placeholder="yyyy/mm/dd" />
<label>Camere</label>">
 <select name="RoomsNR" class="full-width">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<label>Adulti</label>
<select name="Adults" class="full-width">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
<label>Copii</label>
<select name="Kids" class="full-width">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
 <option value="3">3</option>
<option value="4">4</option>
 </select>
<button type="submit">CAUTA</button>
</form>

For the city, in and out i sent them using post.

And i need to receive the information in php as the below: -And example with 2 rooms selected and 4 adults

  // First Room
  $rooms[] = array(array("paxType" => "Adult"));

  // Second Room
  $rooms[] = array(array("paxType" => "Adult"), array("paxType" => "Adult"));

I have tried to send the values using post and create some php rules by its not working as it should. Several people adiced me to do this with JSON, but i have no ideea how to do this. Of helps the Instruction for this API system can be found at : www.hotelspro.com/xf_4.0/HotelsPro_XML_booking_system_4_1.doc

Upvotes: 0

Views: 650

Answers (1)

hakre
hakre

Reputation: 198237

Several people adiced me to do this with JSON

Well, might be they did but you should keep things much more separated here if you're looking to solve this with more ease.

What you do with the HTML form and how you submit the data (either via the classic submit button resulting in a HTTP POST request to the server or by using some javascript code that takes the form data and creates a HTTP POST request to the server) is not so much an issue on how to interact with the remote API.

Take this sentence of yours for example in which you describe your problem:

I have tried to send the values using post and create some php rules by its not working as it should.

Given that there are two major parts in the application (most likely there are three, next to input (1) and processing (2) there is also output (3)), you have to locate the where the problem occurs first.

For example: The input UI/routines might not yet be 100% perfect (hat is in input (1)), but creating the wrong intermediate data formats in processing (2) does already count as an error on it's own. It's not that a possible wrong input from (1) is the problem, but that processing (2) is not able to detect that.

So now looking in input (1) to solve a problem in processing (2) is not often working well, because too often you're looking at the wrong place.

So first mock the input (just set the variables as you need them) and check if interacting with the remote API works as expected.

This should also ensure that you don't get distracted only because somebody shipped just another new term to you which only creates more question marks.

Upvotes: 1

Related Questions