Simple Love Calculator

Submitted by: 
Language: 
Visitors have accessed this post 5030 times.

If your looking for a Simple Web Application for entertainment I have here a Simple Love Calculator using PHP and Javascript. This application has a two inputs of a name for the male and female. Every letter or name that the user encode the application will automatically calculate the percentage of love with that two names that the user encode. The calculation of the percentage is driven by the javascript to show how much percentage that the two name will be each other or not. See the example code below.

Sample Code

Calculator.php - this code is for the calculation of love and convert to percentage.

  1. <?php
  2. $lcname = strtolower(preg_replace("/ /", "", strip_tags(trim($_POST['per1'] . $_POST['per2']))));
  3. $alp = count_chars($lcname);
  4.  
  5. for ($i=97; $i <= 122; $i++)
  6. {
  7. if ($alp[$i] != false)
  8. {
  9. $answer = strlen($alp[$i]);
  10.  
  11. if ($answer < 2)
  12. {
  13. $calc[]=$alp[$i];
  14. }
  15. else
  16. {
  17. for ($a=0; $a < $answer; $a++)
  18. {
  19. $calc[]=substr($alp[$i], $a, 1);
  20. }
  21. }
  22. }
  23. }
  24.  
  25. while (($letter=count($calc)) > 2)
  26. {
  27. $lettermitte = ceil($letter / 2);
  28. for ($i=0; $i < $lettermitte; $i++)
  29. {
  30. $sum = array_shift($calc) + array_shift($calc);
  31. $answer =strlen($sum);
  32.  
  33. if ($answer < 2)
  34. {
  35. $addcalc[]=$sum;
  36. }
  37. else
  38. {
  39. for ($a=0; $a < $answer; $a++)
  40. {
  41. $addcalc[]=substr($sum, $a, 1);
  42. }
  43. }
  44. }
  45. $answers=count($addcalc);
  46. for ($b=0; $b < $answers; $b++)
  47. {
  48. $calc[]=$addcalc[$b];
  49. }
  50. array_splice($addcalc, 0);
  51. }
  52. echo $calc[0] . $calc[1];
  53. ?>

result

Index.php - This is for the HTML form and for the rest of javascript code to catch or fetch the data through the calculation.php

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Simple Love Calculator</title>
  5. <link rel="stylesheet" type="text/css" media="screen" href="css/style.css" />
  6. <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" />
  7. </head>
  8. <body>
  9. <script language = "javascript" type = "text/javascript">
  10. var request = false;
  11. try
  12. {
  13. request = new XMLHttpRequest();
  14. }
  15. catch (trymicrosoft)
  16. {
  17. try
  18. {
  19. request = new ActiveXObject("Msxml2.XMLHTTP");
  20. }
  21. catch (othermicrosoft)
  22. {
  23. try
  24. {
  25. request = new ActiveXObject("Microsoft.XMLHTTP");
  26. }
  27. catch (failed)
  28. {
  29. request = false;
  30. }
  31. }
  32. }
  33. if (!request)
  34. alert("Error initializing XMLHttpRequest!");
  35. function updateDiv(person1, person2)
  36. {
  37. var url = "calculate.php";
  38. var params = "per1=" + person1 + "&per2=" + person2;
  39. request.open("POST", url, true);
  40. request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  41. request.setRequestHeader("Content-length", params.length);
  42. request.setRequestHeader("Connection", "close");
  43. request.onreadystatechange = function()
  44. {
  45. if (request.readyState == 4 && request.status == 200)
  46. {
  47. var response = request.responseText;
  48. document.getElementById('targetDiv').innerHTML = response + "% ";
  49. }
  50. }
  51. request.send(params);
  52.  
  53. }
  54. </script>
  55. <div id = "main" align = "center">
  56. <form name = "test" action = "#" class="form">
  57. <h1><b>Love Calculator</b></h1><hr/>
  58. <table border = "0">
  59. <tbody>
  60. <tr>
  61. <td><span class="style1"><img src="./img/male.png" width="50" height="50" style="margin-top: 15px;"/></span>
  62. <input size = "30" class="form-control" name = "per1" placeholder = "Enter Full Name" type="text" /><br/>
  63. <span class="style1"><img src="./img/female.png" width="50" height="50" /></span>
  64. <input size = "30" class="form-control" name = "per2" placeholder = "Enter Full Name" type="text" />
  65. </td>
  66. <td style = "background-image: url('img/heart_hands.gif');background-repeat:no-repeat; width: 220px; height: 128px;">
  67. <div id = "targetDiv" valign="middle" align = "center">0%</div>
  68. </td>
  69. </tr>
  70. </tbody>
  71. </table><br/>
  72. <input onclick = "updateDiv(per1.value, per2.value)" value = "Calculate" class="btn btn-default" type = "button">
  73. </form><br/>
  74. </div>
  75. </body>
  76. </html>

Hope that you learn in my tutorial. Don't forget to LIKE & SHARE this website. Enjoy Coding.


Comments

Sir/Mam,It worked fine for me...thanks a lot.
But do you have any video which will explain me these codes.?

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.