Ajax - Regnemaskine.
Form requested som JSON-fil. Simpelt response som Alert.
Kontroller om fil modtaget:
if (isset($_POST['felt01'],$_POST['felt02'], $_POST['felt03']))
{
print_r($_POST);
}
|
REGNEMASKINE
FORM
<form action="add.php" method="post">
<input type="text" name="first">+
<input type="text" name="second">
<input type="submit" value="Send">
</form>
|
global js
$('#add').on('submit', function() {
var that = $(this),
contents = that.serialize();
$ajax({
url: 'add.php',
dataType: 'json',
type: 'post',
data: contents,
success: fundtion(data) {
if(data.success) {
alert('The result is ' + data.result);
}
}
});
return false;
});
|
PHP
<?php
header('Content-type: text/javascript');
$json = array(
'success' => false,
'result' => 0
);
if(isset($_POST['first'], $_POST['second'])) {
$first = (int)$_POST['first'];
$first = (int)$_POST['second'];
$json['success'] = true;
$json['result'] = $first + $second;
}
echo json_encode($json);
?>
|
https://www.youtube.com/watch?v=GrycH6F-ksY
|