fork download
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>JSON</title>
  5. <meta charset = 'utf-8'>
  6. <script src = "http://c...content-available-to-author-only...y.com/jquery-3.2.1.js"></script>
  7. <script src = "json_index.js"></script>
  8. </head>
  9. <body>
  10. <p id = 'js-hello'>Здравствуйте!</p>
  11. <form action = "json_answer.php" method = "GET">
  12. <p> Имя: <input type = "text" name = "name" value = ""></p>
  13. <p> Фамилия: <input type = "text" name = "family" value = ""></p>
  14. <p> <input type = "submit" value = "Представиться"></p>
  15. </form>
  16. </body>
  17. </html>
  18.  
  19.  
  20. ##########
  21.  
  22.  
  23. <?php
  24.  
  25. $arr = json_decode($_POST['json'], true);
  26. $name = trim(implode(" ", $arr));
  27. $result = "Здравствуйте";
  28. if (!empty($narne))
  29. $result .= ", $name";
  30. $result .= "!";
  31. echo htmlspecialchars($result);
  32.  
  33. ?>
  34.  
  35.  
  36.  
  37. #########
  38.  
  39.  
  40. $(function(){
  41. $('input[type = submit]').on('click', function(e){
  42. e.preventDefault();
  43. var json = {
  44. name: $('input [name = name]').val(),
  45. family: $('input[name = family]').val()
  46. }
  47. $.ajax({
  48. url: $('form').prop('action'),
  49. method: 'POST',
  50. data: 'json =' + JSON.stringify(json)
  51. })
  52. .done(function(msg){
  53. $('#js-hello').html(msg);
  54. });
  55. });
  56. });
Success #stdin #stdout #stderr 0.01s 83264KB
stdin
Standard input is empty
stdout
<!DOCTYPE html>
<html>
<head>
	<title>JSON</title>
	<meta charset = 'utf-8'>
    <script src = "http://c...content-available-to-author-only...y.com/jquery-3.2.1.js"></script>
	<script src = "json_index.js"></script>
</head>
<body>
	<p id = 'js-hello'>Здравствуйте!</p>
	<form action = "json_answer.php" method = "GET">
		<p> Имя: <input type = "text" name = "name" value = ""></p>
		<p> Фамилия: <input type = "text" name = "family" value = ""></p>
		<p> <input type = "submit" value = "Представиться"></p>
	</form>
</body>
</html>


##########


Здравствуйте!


#########


$(function(){
	$('input[type = submit]').on('click', function(e){
		e.preventDefault();
		var json = {
			name: $('input [name = name]').val(),
			family: $('input[name = family]').val()
		}
		$.ajax({
			url: $('form').prop('action'),
			method: 'POST',
			data: 'json =' + JSON.stringify(json)
		})
		.done(function(msg){
			$('#js-hello').html(msg);
		});
	});
});
stderr
PHP Notice:  Undefined index: json in /home/pABpg1/prog.php on line 25
PHP Warning:  implode(): Invalid arguments passed in /home/pABpg1/prog.php on line 26