There is data presented in the form of XML:

function load($option){ $xml_string =<<<XML <city> <client> <id>1</id> <name>Moscow</name> <mail>moscow@moscow.ru</mail> <phone>988998989</phone> </client> <client> <id>2</id> <name>Sankt-Peterburg</name> <mail>xabarovsk@mail.ru</mail> <phone>99989998</phone> </client> </city> XML; //simplexml_load_file $xml = simplexml_load_string($xml_string) or die("Error: Cannot create object"); foreach ($xml->children() as $city) { if (strtolower($city->name) == strtolower($option) ) return $city; } } if(isset($_POST['value'])){xbnf var_dump($_POST); $value = json_encode(load($_POST['value'])); return $value; } 

there is a select on the page to select a city

 <select id="select_city" "> <optionvalue="moscow">Москва</option> <option value="sankt-peterburg">Санкт-петербург</option> </select> 

Post (ajax) request:

 $('#select_city').change(function() { var value= $(this).val(); $.ajax({ type: "POST", url: 'test.php', data: value, dataType: 'json', contentType: 'application/json', success :function(result) { console.log(result); $('#phone').html(result[0]); } }) 

and output fields in the form

  <p id="phone"><? php echo isset($value->phone) ? $value->cite : "Москва"; ?></p> 

The $ value variable is declared as global $ value; variable value is set during document loading

Post request is sent, but returns nothing, tell me what was wrong. Happy New Year.

By editing ajax in the same way:

  $('#select_city').change(function () { var value = $(this).val(); $.ajax({ type: "POST", url: 'test.php', data: value, dataType: 'json', contentType: 'application/json', callbackParameter: 'callback', timeout: 10000, success: function (json) { alert('success') }, error: function (error) { alert('error') } }) }) 

I get the output of alern (error) (modal window)

  • Comments are not intended for extended discussion; conversation moved to chat . - PashaPash

1 answer 1

Generally earned as follows:

  $('#select_city').change(function () { var value = $(this).val(); $.ajax({ type: "POST", url: 'test.php', data: {value: value}, callbackParameter: 'callback', timeout: 10000, success: function (value) { alert() }, error: function(jqXHR, textStatus, errorThrown) { console.log(textStatus + "-" + errorThrown); console.log(jqXHR); }, }) }) 

and in the test.php file we use echo instead of return. Thanks to all. Happy New Year