How to use JQuery autocomplete selected item?

How to use JQuery autocomplete selected item?

I assign selected autocomplete value to variable var selected and posting it to the same php page, after the page is reloaded, am checking if the variable is posted like this: if(isset($_POST["selected"])) then echo it, but nothing happens.

I need to use this variable in query, after page is reloaded.The question is how to use correctly selected autocomplete item?

<script> $(function() { var data=<?php echo json_encode($namesArray);?>; var selected; $( "#searchTags" ).catcomplete({ delay: 0, source:data, select: function(event, ui) { selected=ui.item.label; jQuery.ajax({ url:"index.php", data:{selected}, type:"POST", success:function(){ window.location="index.php"; } }); }}); });