{"id":1382,"date":"2014-12-21T03:55:00","date_gmt":"2014-12-21T06:55:00","guid":{"rendered":"http:\/\/www.jgarces.info\/?p=1382"},"modified":"2014-12-21T03:56:15","modified_gmt":"2014-12-21T06:56:15","slug":"calculator-ajax-jquery","status":"publish","type":"post","link":"https:\/\/www.jgarces.info\/en\/calculator-ajax-jquery\/","title":{"rendered":"Calculator AJAX &#8211; JQuery"},"content":{"rendered":"<p>Calculator using AJAX with JQuery<\/p>\n<p>Download here: <a href=\"http:\/\/www.jgarces.info\/wp-content\/uploads\/2013\/09\/calcajax.zip\">Calculator AJAX<\/a><\/p>\n<pre lang=\"php\">\r\n<?php\r\n\r\nif(isset($_POST[\"par1\"]) &#038;&#038; isset($_POST[\"par2\"]) &#038;&#038; isset($_POST[\"op\"]))\r\n{\r\n\trequire \"funciones.php\";\r\n\r\n\t$par1 = $_POST[\"par1\"];\r\n\t$par2 = $_POST[\"par2\"];\r\n\t$op = trim($_POST[\"op\"]);\r\n\tif(!is_numeric($par1) || !is_numeric($par2))\r\n\t{\r\n\t\t$resultado = \"Ingrese N\u00fameros\";\r\n\t}\r\n\telse\r\n\t{\r\n\t\t$par1 = intval($par1, 10);\r\n\t\t$par2 = intval($par2, 10);\r\n\t\tswitch ($op)\r\n\t\t{\r\n\t\t\tcase \"+\": $resultado = sumar($par1, $par2); break;\r\n\t\t\tcase \"-\": $resultado = restar($par1, $par2); break;\r\n\t\t\tcase \"*\": $resultado = multiplicar($par1, $par2); break;\r\n\t\t\tcase \"\/\":\r\n\t\t\t\tif($par2 != 0) {\r\n\t\t\t\t\t$resultado = dividir($par1, $par2);\r\n\t\t\t\t} else {\r\n\t\t\t\t\t$resultado = \"Err. Div. Cero\";\r\n\t\t\t\t}\r\n\t\t\t\tbreak;\r\n\t\t\tdefault: $resultado = \"Seleccione Operaci\u00f3n\";\r\n\t\t}\r\n\t}\r\n\r\n\t\/*Genera respuesta en JSON*\/\r\n\techo json_encode($resultado);\r\n\r\n\texit();\r\n}\r\n\r\n?>\r\n<\/pre>\n<p><!--more--><\/p>\n<pre lang=\"php\">\r\n<html>\r\n<head>\r\n\t<title>Calculadora<\/title>\r\n\t<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=UTF-8\">\r\n\t<script src=\"jquery-1.4.2.js\"><\/script>\r\n  \t<script src=\"json2.js\"><\/script>\r\n  \t<script type=\"text\/javascript\">\r\n\t\tfunction CalcularTotal()\r\n\t\t{\r\n\t\t\tvar par1 = $(\"#par1\").val();\r\n\t\t\tvar par2 = $(\"#par2\").val();\r\n\t\t\tvar op = $(\"#op\").val();\r\n\r\n\t\t\t\/*Request the result with AJAX by POST method*\/\r\n\t\t\t\/*The parameters are: par1, par2 and op.*\/\r\n\t\t\t$.post(\"calculadora.php\", {par1: par1, par2: par2, op: op}, function(resultado){\r\n\t\t\t\t$(\"#res\").val(resultado);\r\n\t\t\t}, \"json\"); \/*This parameter (\"json\") indicates that the response from the server is in JSON*\/\r\n\t\t}\r\n  \t<\/script>\r\n<\/head>\r\n<body>\r\n  <h3 align=\"center\">CALCULADORA<\/h3>\r\n  <form action=\"calculadora.php\" method=\"POST\">\r\n    <table border=\"1\" align=\"center\" width=\"400px\" cellpadding=\"4\">\r\n    <tr>\r\n\t\t<td><b>Par\u00e1metro 1:<\/b><\/td>\r\n\t\t<td>\r\n\t\t\t<input type=\"text\" name=\"par1\" id=\"par1\" size=\"10\">\r\n\t\t<\/td>\r\n    <\/tr>\r\n    <tr>\r\n\t\t<td><b>Par\u00e1metro 2:<\/b><\/td>\r\n\t\t<td>\r\n\t\t\t<input type=\"text\" name=\"par2\" id=\"par2\" size=\"10\">\r\n\t\t<\/td>\r\n    <\/tr>\r\n    <tr>\r\n\t\t<td><b>Operador:<\/b><\/td>\r\n\t\t<td>\r\n\t\t\t<input type=\"text\" name=\"op\" id=\"op\" size=\"5\">\r\n\t\t<\/td>\r\n    <\/tr>\r\n    <tr>\r\n\t\t<td colspan=\"2\" align=\"center\">\r\n\t\t  <input type=\"button\" name=\"enviar\" value=\"Resultado\" onclick=\"CalcularTotal();\">\r\n\t\t<\/td>\r\n    <\/tr>\r\n    <tr>\r\n\t\t<td colspan=\"2\" align=\"center\">\r\n\t\t  <input type=\"text\" name=\"res\" id=\"res\" size=\"15\">\r\n\t\t<\/td>\r\n    <\/tr>\r\n    <\/table>\r\n\r\n  <\/form>\r\n<\/body>\r\n<\/html>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Calculator using AJAX with JQuery Download here: Calculator AJAX<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[37,36],"class_list":["post-1382","post","type-post","status-publish","format-standard","hentry","category-php-development","tag-ajax-en","tag-jquery-en"],"_links":{"self":[{"href":"https:\/\/www.jgarces.info\/en\/wp-json\/wp\/v2\/posts\/1382","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jgarces.info\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jgarces.info\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jgarces.info\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jgarces.info\/en\/wp-json\/wp\/v2\/comments?post=1382"}],"version-history":[{"count":2,"href":"https:\/\/www.jgarces.info\/en\/wp-json\/wp\/v2\/posts\/1382\/revisions"}],"predecessor-version":[{"id":1384,"href":"https:\/\/www.jgarces.info\/en\/wp-json\/wp\/v2\/posts\/1382\/revisions\/1384"}],"wp:attachment":[{"href":"https:\/\/www.jgarces.info\/en\/wp-json\/wp\/v2\/media?parent=1382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jgarces.info\/en\/wp-json\/wp\/v2\/categories?post=1382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jgarces.info\/en\/wp-json\/wp\/v2\/tags?post=1382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}