-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);
}
Felippe Duarte
  • 14,901
  • 2
  • 25
  • 29
  • Apparently you don't know how to use capitalization or punctuation either. – Difster Nov 28 '18 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. – gforce301 Nov 28 '18 at 19:26
  • PHP runs server-side. Javascript runs client-side. PHP cannot run Javascript functions. – Patrick Q Nov 28 '18 at 19:26
  • @PatrickQ if you search you can understand you can do this watch my last else statement in controllet method – Mehdi Aghighi Nov 28 '18 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. – Patrick Q Nov 28 '18 at 19:35
  • `if you search you can understand you can do this`. You have no idea what you're talking about. If you **can** do this then why isn't your code working? Answer: **Because you can't do this** – gforce301 Nov 28 '18 at 19:37
  • @PatrickQ watch this https://stackoverflow.com/questions/3057317/run-a-javascript-function-from-a-php-if-statement – Mehdi Aghighi Nov 28 '18 at 19:37
  • i am telling that we can use javascript function in php if statement but i don't know why mine is not working @gforce301 – Mehdi Aghighi Nov 28 '18 at 19:38
  • If actually think the misunderstanding is that by assigning a response to `innerHTML` you expect script execution if that response HTML contains script tags. This is not true. – trincot Nov 28 '18 at 19:38
  • @gforce301 watch the link above – Mehdi Aghighi Nov 28 '18 at 19:38
  • @MehdiAghighi You're just proving my point. This is from the answer in that question "Therefore you cannot call a JavaScript function from PHP". I'm done here. – Patrick Q Nov 28 '18 at 19:38
  • @trincot id did that too i set the response to HTML contains script tags – Mehdi Aghighi Nov 28 '18 at 19:39
  • I am also done here. I've been a web developer for a very long time and I'm telling you that **you can't use a javascript function in a php if statement.** Period. End of story. You have no idea what you're talking about. – gforce301 Nov 28 '18 at 19:40
  • So, JS scripts will not execute if you assign them to `innerHTML`. No execution of `StartJob()`. It is by design. – trincot Nov 28 '18 at 19:40
  • @PatrickQ i told ya if you check that link you can use javascript functions in php if statement – Mehdi Aghighi Nov 28 '18 at 19:40
  • One solution could be that you don't do `echo ` at all. Just do nothing in that case ... no output. Then in JS do: `if (xmlhttpform.responseText.trim().length === 0) StartJob();`. – trincot Nov 28 '18 at 19:45
  • @trincot Thank You Man That works !! :) – Mehdi Aghighi Nov 28 '18 at 19:49
  • OK, I have put it as an answer. – trincot Nov 28 '18 at 20:18

1 Answers1

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.

trincot
  • 317,000
  • 35
  • 244
  • 286