-3

i am using laravel for my back-end i have a javascript code for crawling google that is a function named StartJob()

i have a crawl controller that has a get method i wanna do this when i enter a keyword that searches if that is not in database crawl that but if that is in database gets that and show it this is my html code

<div id="numofkeywords"></div>
<form method="post" id="form">
    <textarea id="input" name="input"></textarea>
    <input type="button" id="startjob" onclick="GetCrawl();" value="Start Job">
    <textarea id="filter-positive" rows="4" onkeyup="FilterIfNotWorking()" placeholder="Positive Filter"></textarea>
    <textarea id="filter-negative" rows="4" onkeyup="FilterIfNotWorking()" placeholder="Negative Filter"></textarea>
</form>
<div id="message">

</div>

and this is my controller mehthod that called in ajax

public function getCrawl(Request $request) {
    $keyword = $request->input('input');
    //echo $keyword;
    if (count(DB::table('crawler')->where('name', 'like', "%{$keyword}%")->get()->toArray()) > 0) {
        $results = DB::table('crawler')->where('name', 'like', "%{$keyword}%")->get()->toArray();
        $count = count($results);
        // var_dump($results);
        foreach ($results as $result) {
            echo $result->data . "<br />";
        }
    } elseif (count(DB::table('crawler')->where('data', 'like', "%{$keyword}%")->get()->toArray()) > 0) {
        $results = DB::table('crawler')->where('data', 'like', "%{$keyword}%")->get()->toArray();
        $count = count($results);
        // var_dump($results);
        foreach ($results as $result) {
            echo $result->data . "<br />";
        }
    } else {
        echo '<script>';
        echo 'StartJob()';
        echo '</script>';
    }

}

but in the last else that should run the StartJob() function but that doesn't how can i fix this ? is it cause i am using ajax ??

and this is my ajax call

function GetCrawl() {
var form = document.getElementById("form");

var data = new FormData(form);

if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttpform = new XMLHttpRequest();
} else { // code for IE6, IE5
  xmlhttpform = new ActiveXObject("Microsoft.XMLHTTP");
}
var message = 'message';
xmlhttpform.onreadystatechange = function() {
  if (xmlhttpform.readyState < 4) {
    document.getElementById("message").value = "در حال پردازش ...";

  }
  if (xmlhttpform.readyState == 4 && xmlhttpform.status == 200) {

    document.getElementById("message").innerHTML = xmlhttpform.responseText;

  }
}
xmlhttpform.open("POST", "/laravel-master/crawler/public/api/getcrawl", true);
xmlhttpform.send(data);
}
18
  • Apparently you don't know how to use capitalization or punctuation either. Commented Nov 28, 2018 at 19:24
  • is it cause i am using ajax ?? Nope. It's because you have no idea how any of the technologies you are trying to use actually work or interact. In the code you posted I don't see any ajax call at all. Commented Nov 28, 2018 at 19:26
  • PHP runs server-side. Javascript runs client-side. PHP cannot run Javascript functions. Commented Nov 28, 2018 at 19:26
  • @PatrickQ if you search you can understand you can do this watch my last else statement in controllet method Commented Nov 28, 2018 at 19:32
  • @MehdiAghighi All PHP can do is send a response to your client (aka browser). It does not execute any Javascript code. You need to write client-side (Javascript) code to handle the response and act accordingly. Commented Nov 28, 2018 at 19:35

1 Answer 1

0

You seem to expect that when you assign the Ajax response to innerHTML that if that response has a script tag, that the browser will then execute that script. This is not true. It will be added to the HTML content, but it will not be executed.

How you can make it to execute the desired JS function:

Instead of sending back a script tag to the client, send an empty response, and then let the JS part check on that condition (an empty response) and execute StartJob() explicitly.

So in PHP, remove completely this else block:

  else {
    echo '<script>';
    echo 'StartJob()';
    echo '</script>';
}

The idea is to just do ... nothing in that case.

In JavaScript:

if (xmlhttpform.readyState == 4 && xmlhttpform.status == 200) {
    document.getElementById("message").innerHTML = xmlhttpform.responseText;
    if (xmlhttpform.responseText.trim().length === 0) StartJob();
}

Note about browser support:

Looking at the long way you seem to go to even support archaic browsers as IE6, you will need to use something else than .trim() as that has not always been supported. Then use .replace(/^\s+/, '') which is a left-trim and enough for this case.

Sign up to request clarification or add additional context in comments.

1 Comment

i can also use != ""

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.