title: Stop the execution of php code and send the signal to JavaScript
body: <p>My PHP code sends queries on Amazon.com to retrieve information about books.
  When it receives the information, there are two possibilities for execution of the following program. Possibility to define what should be used, it must look at the total number of book profit for research.</p>
  
  <p>What I do at the moment, I am sending a first request and retrieves the total number of results. According to the number of results, I assign a new value to the variable <code>$queryUrl</code>. If the number of results is greater than 1200, the programs to execute as it should.</p>
  
  <p>If the number of results is less than 1200, the program should finish execute the loop to iterate through the entire pages of results and the rest of the php code but only a single time.</p>
  
  <p>At the moment, if there is less than 1200 results. The program goes through all pages of results, but places to stop at the end of PHP code. It executes all the code several times depending on the parameter of the queries is <code>$searchMonthUrlParam</code> inherit the variable <code>recupMonth</code> JavaScript.</p>
  
  <p>For now, I have that</p>
  
  <p>PHP :</p>
  
  <pre><code>//Retrieve variable value passed in POST from JavaScript
  $pageNum = (isset($_POST["pageNum"]) &amp;&amp; $_POST["pageNum"]) ? intval($_POST["pageNum"]) : 1;
          $writeMode = (isset($_POST["writeMode"]) &amp;&amp; $_POST["writeMode"]) ? $_POST["writeMode"] : "w";
          $searchType = (isset($_POST["searchType"]) &amp;&amp; $_POST["searchType"]) ? intval($_POST["searchType"]) : 0;
          $month = (isset($_POST["month"]) &amp;&amp; $_POST["month"]) ? intval($_POST["month"]) : date("n");
          $year = (isset($_POST["year"]) &amp;&amp; $_POST["year"]) ? intval($_POST["year"]) : date("Y") ;
          $keyword = (isset($_POST["keyword"]) &amp;&amp; strlen($_POST["keyword"])) ? $_POST["keyword"] : "";
          $startMonth = (isset($_POST["startMonth"]) &amp;&amp; strlen($_POST["startMonth"])) ? $_POST["startMonth"] : NULL;
          $startYear = (isset($_POST["startYear"]) &amp;&amp; strlen($_POST["startYear"])) ? $_POST["startYear"] : NULL;
          $endMonth = (isset($_POST["endMonth"]) &amp;&amp; strlen($_POST["endMonth"])) ? $_POST["endMonth"] : NULL;
          $endYear = (isset($_POST["endYear"]) &amp;&amp; strlen($_POST["endYear"])) ? $_POST["endYear"] : NULL;
          if($keyword) {
              if($writeMode === "w") {
                  file_put_contents(CSV_FILE, "");
              }
  
              $searchMonthUrlParam = "&amp;field-datemod=".$month;
              $searchYearUrlParam = "&amp;field-dateyear=".$year;
  
              $searchTypeUrlParam = "";
              switch($searchType) {
                  case SEARCH_TYPE_TITLE:
                      $searchTypeUrlParam = "&amp;field-title=";
                      break;
                  case SEARCH_TYPE_KEYWORDS:
                      $searchTypeUrlParam = "&amp;field-keywords=";
                      break;
                  case SEARCH_TYPE_AUTHOR:
                      $searchTypeUrlParam = "&amp;field-author=";
                      $searchTypeUrlParam = "&amp;field-publisher=";
                       break;
                  case SEARCH_TYPE_PUBLISHER:
                     break;
              }
      //send request to Amazon    
      $queryUrl    = AMAZON_TOTAL_BOOKS_COUNT . $searchMonthUrlParam . $searchYearUrlParam . $searchTypeUrlParam . urlencode($keyword) . '&amp;page=' . $pageNum;
                      $queryResult = file_get_contents($queryUrl);
                      //Search number total results
                      if (preg_match('/of\s+([0-9,]+)\s+Results/', $queryResult, $matches)) {
                          $totalResults = (int) str_replace(',', '', $matches[1]);
                      } else {
                          throw new \RuntimeException('Total number of results not found');
                      }
                      //this condition work
                      if ($totalResults &gt; MAX_RESULT_ALL_PAGES) {
                          $queryUrl = AMAZON_SEARCH_URL . $searchMonthUrlParam . $searchYearUrlParam . $searchTypeUrlParam . urlencode($keyword) . '&amp;page=' . $pageNum;
                      }
  
                      //with this condition I don't know how to proceed
                      else {
  
                          $queryUrl = AMAZON_TOTAL_BOOKS_COUNT.$searchMonthUrlParam.$searchYearUrlParam.$searchTypeUrlParam.urlencode($keyword)."&amp;page=".$pageNum;
  
                  }
  $htmlResultPage = file_get_html($queryUrl);
  $htmlQueryResult = $htmlResultPage-&gt;find("div[class=result]");
  exit;
  </code></pre>
  
  <p>JavaScript :</p>
  
  <pre><code>if(processedResultCount === 0) {
                          pageNum = 1;
                          recupMonth--;
                          if(recupMonth === 0 &amp;&amp; recupYear &gt; endYear) {
                              recupMonth = 12;
                              recupYear--;
                          }
                          else if(parseInt(recupMonth, 10) === parseInt(endMonth, 10) &amp;&amp; parseInt(recupYear, 10) === parseInt(endYear, 10)) {
                              alert("Processing finished");
                              if(totalResultCount != 0) {
                                  contentElt.innerHTML = "Total processed results: " + totalResultCount + '&lt;br/&gt;&lt;br/&gt;&amp;gt; &lt;a href="amazon_keyword_stats.csv" title="Download CSV result file"&gt;Download CSV result file&lt;/a&gt;';
                              }
                              return;
                          }
                      }
                  getAmazonResult(pageNum, writeMode);
                  return;
              }
           }
       }
  xmlHttp.open("POST", "ctrl/getAmazonResult.php", true);
      xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
      xmlHttp.send("pageNum=" + pageNum + "&amp;writeMode=" + writeMode + "&amp;searchType=" + searchType + "&amp;month=" + recupMonth + "&amp;year=" + recupYear + "&amp;keyword=" + keyword + "&amp;startMonth=" + startMonth + "&amp;startYear=" + startYear + "&amp;endMonth=" + endMonth + "&amp;endYear=" + endYear);
  </code></pre>
  
  <p>Someone would have a solution on how to power stoper php code execution if it goes in the else but he is finished to execute once in full?</p>