jquery - ajax realtime refresh of values of word occurences using php -


i tried searching solutions not handle properly. also, want display using ajax. every time type word, appear below textbox counter beside it. current code this:

the output should this: input of "the quick brown fox"

brown -- 1  fox -- 2  quick -- 1  -- 1  

but current output is:

brown -- 1  fox -- 1  fox -- 2  quick -- 1  -- 1  //the fox displays twice. 

my index.php

<!doctype html> <html>     <head>         <title>working javascript</title>         <script type="text/javascript" src="js/jquery-3.2.1.min.js"> </script>     </head>      <body>         <!--textbox-->         <input type="text" name="input" id="textinput" autofocus/>         <!--where answer appear-->         <div id="content"></div>     </body>     <script type="text/javascript">         //get content of textbox         var textinput = document.getelementbyid("textinput");         //transform jquery         var jtextinput = $("#textinput");         var divselector = document.getelementbyid("content");         //the ajax function         textinput.onkeyup = function () {             console.log($("#textinput").val());             $.ajax({                 "method": "post", //to specify type of method  request in server (get or post)                 "url": "assignment2.php", // send request                 "datatype": "json", // datatype of request                 "data": {                     "text": $("#textinput").val() //data values you'll  send                 },                  success: function (res) {                     $("#content").html(res.reversedstring);                 }             });         };     </script>     </html> 

and assignment2.php

<?php /* //note: if delete , left commented code below, text  displays type in textbox. //$vari = array("reversedstring" => $_post['text']); //echo json_encode($vari); */ //////////////////////////////////////////////////// //get content of textbox $vari = array("reversedstring" => $_post['text']); $var = $vari; header('content-type: application/json;charset=utf-8');  //make array  $words = explode(' ', $var); //sort a-z sort($words); $result = array_combine($words, array_fill(0, count($words), 0)); //total number of words $len = count($words); //the array number of occurences $totals = array();  foreach ($words $word) {     $result[$word] ++;     array_push($totals, $result[$word]); } //the array words , number of occurences $finalarray = array(); ($i = 0; $i < $len; $i++) {     array_push($words[$i] . " -- " . $totals[$i] . " <br>"); } //make array sentence $regularexpression = implode(" ", $finalarray);  $last = array("reversedstring" => $regularexpression); echo json_encode($last); 

i'll answer blurry information have. assignment of mine. please , thankyou patience :)

use if statement inside each loop

you check if it's there, using in_array, before pushing.

foreach($words $word){if(!in_array($word, $liste, true)){     array_push($liste, $word); }} 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -