0

I am VERY new to jQuery, and I have been trying to get information from my SQL database whenever the user chooses a new option in a datalist form. This is the code:

<form>
<input list="chemicals" name="chemicalsearch">
  <datalist id="chemicals">

 <?php while ($info3=mysql_fetch_array($info2)) {
      echo "<option value='$info3[name]'>$info3[formel]</option>";
        }

?>
</datalist>
</form>

<script type="text/javascript">

$('#chemicals').on('change', function() {
    var record_id = $(this).val(); 
    var data = {
        'id': record_id
    };

    $.ajax({
        type: "GET",
        url: '/Chemistry%20Calculator/getchemical.php', 
        data: data,
        success: function(response) {
            document.getElementById('formel').innerHTML = data.formel;
        },
        error: function(jqXHR, textStatus, errorThrown) {
        }
    });
});
</script>

And then the PHP file:

<?php

include_once 'connect.php';

$info="SELECT * from chemicals where name='$id'";
$info2=mysql_query($info) or die("Wrong link. This page does not exist.");
$info3=mysql_fetch_array($info2);

$name = $info3['name'];
$formel = $info3['formel'];
$massa = $info3['molmassa'];

$array = array($name, $formel, $massa);

$data = json_encode($array);

echo $data;

?>

Please be patient with me here, as I have never used jQuery before. And yes, I am aware of the fact that I'm using the old MySQL syntax, I will change that as soon as I get this working. Any help would be greatly appreciated :)

Kriss0612
  • 57
  • 1
  • 10
  • 1
    use: success: function(response) { document.getElementById('formel').innerHTML = response; } – tech2017 Jun 23 '17 at 19:47
  • You have to fetch `$id` from the session. Now you don't have anything for that part. – Norbert Jun 23 '17 at 19:47
  • 1
    ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jun 23 '17 at 19:47
  • 1
    [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***. Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Jun 23 '17 at 19:47
  • Please explain, how do I fetch $id from the session? Is it a PHP Get, or something else? – Kriss0612 Jun 25 '17 at 18:15

2 Answers2

1

You are not writing what exactly is the problem, either from the side of php or javaScript. But anyway, correct your syntax to php regarding the variables. Like below:

<?php while ($info3=mysql_fetch_array($info2)) {
  echo '<option value="'.$info3[name].'">'.$info3[formel].'</option>';
}?>

and later on your query...$info='SELECT * from chemicals where name="'.$id.'"'

Vasiliki M.
  • 372
  • 3
  • 12
0

The success method is a function to be called if the request succeeds. The function gets passed three arguments:

  1. The data returned from the server, formatted according to the dataType parameter or the dataFilter callback function, if specified;
  2. A string describing the status;
  3. The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object.

In your case:

      success: function(data, textStatus, jqXHR) {
         //The "data" returned from server is assigned to HTML element here
         document.getElementById('formel').innerHTML = data;
      }

A side note: usage of mysql_* functions is deprecated, use PHP PDO instead.

EDIT: Do not forget to include jQuery library in your file, before trying to execute the code $(“#chemicals")..... The error of $ being undefined means exactly that.

Bud Damyanov
  • 30,171
  • 6
  • 44
  • 52
  • The error Im getting in the console right now is "$ is not defined" at $('#chemicals').on('change', function() { I have already implemented what you wrote. – Kriss0612 Jun 23 '17 at 20:42