-1

I trying to put multiple functions in same form with ajax.The form are used to prenote a new "meeting": I have a input:date and a select (for choose the operator).

FORM CODE:

<div id="info"></div>
<form>
<div id="input-form">
  <input type="date" name="data" id="dataApp" onChange="checkDate()" required>
</div>
<div id="divSquadre">
  <select name="squadra" onChange="orariApp()" id="squadra" required>
  <option value="0">Operator 1</option>
  <option value="1">Operator 2</option>
  <option value="2">Operator 3</option>
  </select>
</div>
</form>

The first function (checkDate) check the input date in DB and edit the select only with the "free-operator". The second function (orariApp), in this moment, show alert when it's called. i'm using this for "debug".

JS CODE:

function checkDate() {
      var data=document.getElementById("dataApp").value;

      var xmlhttp = new XMLHttpRequest();
      xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
          document.getElementById("divSquadre").innerHTML = this.responseText;
        }
      };
      xmlhttp.open("GET", "./ajax/checkSquadreInDataApp.php?data=" + data , true);
      xmlhttp.send();
    }

function orariApp() {

  //if run function checkDate() this function doesn't work
  alert("i'm working :)");

}

PHP checkSquadreInDataApp.php:

<?php
$data=$_REQUEST["data"];
$query = $mysqli->query('SELECT * FROM operators where data like "'.$data.'" and active is not null'); //example query
$squadra=$query->fetch_all(MYSQLI_BOTH);
echo '<select name="squadra" onChange="orariApp()" id="squadra" required>';
foreach($squadra as $el){
  echo '<option value="'.$el['id'].'">'.$el['id'].'</option>';
}
echo '</select>';

Before i change date (start function checkDate) the function "orariApp" works. When function "checkDate" edit div "divSquadra", "orariApp" doesn't work anymore. Can someone help me?

Sorry for bad english :)

nik
  • 1
  • 1
  • I am trying to understand what you want to achieve, can you briefly describe the issue in comment here so I can assist you! – Robert Azar Sammie Apr 22 '21 at 14:58
  • Hi! Im trying to get the value of selected option with "orariApp" function. If i dont change the date (without AJAX) the function work. But, if function "checkDate" send the HTTP Request and change the option of select, the function "orariApp" doesn't work anymore. – nik Apr 22 '21 at 15:01
  • @nik, Ideally it should work, anyways, can you share the value of `this.responseText` after ajax success? – Ravikumar Apr 22 '21 at 15:38
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Apr 22 '21 at 16:12
  • @Ravikumar `this.responseText` works! They change the select's option in correct way, with correct value. But after that the `onChange="orariApp()" doesn't work anymore. I can't understand the cause. – nik Apr 22 '21 at 16:38
  • @Dharman Thx but only 2 person (after login) can work on this page. They are also who popolate this table, so im not afraid about it. I change name of all elements, just to show you a similar situation. – nik Apr 22 '21 at 16:42
  • @nik, as along as `this.responseText` has valid html, it'll bind the event again and should work fine. but in your case it's failing. So, if you can provide what is returned by `this.responseText`, then it would be easy to narrow down the issue. – Ravikumar Apr 22 '21 at 16:42
  • @Ravikumar `query('SELECT * FROM operators where data like "'.$data.'" and active is not null'); //example query $squadra=$query->fetch_all(MYSQLI_BOTH); echo ''; ?>` This is the script. I can't see the responde in the sourcecode (ctrl+u). The responde depend from date. But it is the script who generate the responde code for js function. – nik Apr 22 '21 at 16:50
  • You're not afraid of your code being broken? What does it matter how many people are using this code? Broken code is a code that needs to be fixed. Even if this was completely automated there is a chance it won't work properly because of SQL injection. Fix the problem, don't ignore it. – Dharman Apr 22 '21 at 17:13

1 Answers1

0

Hi! Im trying to get the value of selected option with "orariApp" function. If i dont change the date (without AJAX) the function work. But, if function "checkDate" send the HTTP Request and change the option of select, the function "orariApp" doesn't work anymore.

Okay Nik, I understand. Sometimes contents generated and added to the DOM using JQuery has some known issues but this is what you can do in your case; add this your function

    function orariApp() {

  //if run function checkDate() this function doesn't work
  alert("i'm working :)");

}

in the responsecheckSquadreInDataApp.php file, It might work. Let me know if anything.