如何在php中使用自动完成数据给出基于计数的条件

I have one input-text box with a submit button. By using an autocomplete method when I search for related data, if data is in database it should do the search, otherwise it should be inserted in database when I click submit. Here in my code I am getting the database result in "source" using ajax in JS.

 <?php  $search = "<script>source</script>";
   if(count($search) > 0){
        $onclickEvent = "onclick='return searchQuestionName();'";
        }else{
        $onclickEvent = "onclick='return InsertQuestionsInfo();'";
        } ?>

<div class="ui-widget">
    <input type="text" id="questionname" name="name" placeholder="Search through Questions" required=""/>
</div>
<button type="submit" id="FormSubmit" <?php echo $onclickEvent;?> class="mc-btn btn-style-1" style="width:100px;">Submit</button>

Below is my autocomplete Script:

<script>
    $( "#questionname" ).autocomplete({
      source: rootUrl+"includes/ajax/ajax_chat.php?action=searchQuestionName"
    });
</script>

From My understanding, You cannot define the onclick event before you start typing search term.

You have 2 choices,

1/ textbox id questionname, on blur you send an ajax request and find the count. then bind the click event to the submit button according to the count you get from ajax.

2/ Just submit this form to a php file and write your business flow.(find search term's count, if no count just insert the search term before proceed you business logic).