narutofan Posted December 25, 2018 Share Posted December 25, 2018 hi, i'm trying to create a @tagging script for one of my project where i'm using at.js with tinymce editor. The problem is i get the desired username in alert output ,but can't seem to bind it to a variable that will be accepted by at.js. The response i get when i alert is Quote `[{"readyState":4,"responseText":"\n\n{\"names\":\"Shikhar Bansal(bshikhar13131313@gmail.com),\"}","responseJSON":{"names":"Shikhar Bansal(bshikhar13131313@gmail.com),"},"status":200,"statusText":"OK"}]` but i want only the Quote "Shikhar Bansal(bshikhar13131313@gmail.com)" part in the variable don't know how to get it in jquery or javascript a little help in solving this problem might help me a lot. The code i have tried so far tinymce.init({ selector: 'textarea#wall_id_1', height: 300, theme: 'modern', resize: false, force_p_newlines: false, plugins: 'print preview powerpaste searchreplace autolink directionality advcode visualblocks visualchars fullscreen image link media template codesample table charmap hr pagebreak nonbreaking anchor toc insertdatetime advlist lists textcolor wordcount tinymcespellchecker a11ychecker imagetools mediaembed linkchecker contextmenu colorpicker textpattern help', toolbar1: 'formatselect | bold italic strikethrough forecolor backcolor | link | alignleft aligncenter alignright alignjustify | numlist bullist outdent indent | removeformat', image_advtab: true, templates: [ { title: 'Test template 1', content: 'Test 1' }, { title: 'Test template 2', content: 'Test 2' } ], content_css: [ '//fonts.googleapis.com/css?family=Lato:300,300i,400,400i', '//www.tinymce.com/css/codepen.min.css' ], setup: function(editor) { editor.on('keyup', function(e) { /* var eli= $(editor.contentDocument.activeElement).prop('innerHTML'); var txt=$(eli+'p').html(); var txt1=$(eli+'span').html(); var regex=new RegExp(/@+([a-zA-z!._-]+)/g); var match= regex.exec(txt); // console.log(match); // alert(match); var names=load_Ajax(match[1]); return names; */ if (e.keyCode == 13 && $(editor.contentDocument.activeElement).atwho('isSelecting')) return false; }); return names; }, init_instance_callback: function(editor) { var name = editor.on("keyup", function(e) { var eli = $(editor.contentDocument.activeElement).prop('innerHTML'); console.log('eli=', eli); var txt = $(eli + 'p').html(); console.log('txt=', txt); var txt1 = $(eli + 'span').html(); console.log('txt1=', txt1); var regex = new RegExp(/@+([a-zA-z!._-]+)/g); var match = regex.exec(txt); console.log('match=', match); if( typeof match != 'undefined' && match != null ) { var results = []; $.ajax({ url: "jsdropdown.php", data: { "uname": match[1] }, async: false, complete: function(res) { results.push(res); // results=res; // alert(JSON.stringify(res)); return res; }, dataType: "json" }); var results1=JSON.stringify(results); alert(results1); $(editor.contentDocument.activeElement).atwho({ at: "@", data: results1 }); } }).responseJSON; } }); here is a js fiddle: http://jsfiddle.net/e8szo2yw/8/ Quote Link to comment https://forums.phpfreaks.com/topic/308074-how-to-trim-a-output-and-insert-it-to-a-variable-in-javascript/ Share on other sites More sharing options...
Barand Posted December 25, 2018 Share Posted December 25, 2018 Take what you have, json_decode it $json = '[{"readyState":4,"responseText":"\n\n{\"names\":\"Shikhar Bansal(bshikhar13131313@gmail.com),\"}","responseJSON":{"names":"Shikhar Bansal(bshikhar13131313@gmail.com),"},"status":200,"statusText":"OK"}]' ; $data = json_decode($json,1); echo '<pre>', print_r($data, 1), '</pre>'; and examine the result: Array ( [0] => Array ( [readyState] => 4 [responseText] => {"names":"Shikhar Bansal(bshikhar13131313@gmail.com),"} [responseJSON] => Array ( [names] => Shikhar Bansal(bshikhar13131313@gmail.com), ) [status] => 200 [statusText] => OK ) ) You should see that you need echo $data[0]['responseJSON']['names']; //--> Shikhar Bansal(bshikhar13131313@gmail.com) Quote Link to comment https://forums.phpfreaks.com/topic/308074-how-to-trim-a-output-and-insert-it-to-a-variable-in-javascript/#findComment-1563099 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.