0

I would like to:

  1. send data from the index.php file using ajax to php

  2. then based on it find the appropriate value in the database

  3. and send the returned value to index.php and put it in "longitude" input.

I really have no idea how to do it.

For example: I write "lala" in input "bar", then click "submit" button and then find "longitude" in database where Nombre = "lala" and finally it appears in "longitude" input.

index.php:

<form id="foo">
<label for="bar">A bar</label>
<input id="bar" name="bar" type="text" value="" />
<input type="submit" value="Send" />
</form>
<input id="longitude" name="longitude" type="text" value="" />

index.js (send value) and next take data (longitude) and put to longitude:

$(function () {

    $('form').on('submit', function (e) {

      e.preventDefault();

      $.ajax({
        type: 'post',
        url: 'http://localhost/inne/phonegap_test/agregar.php',
        data: $('form').serialize(),
        success: function () {
          alert('form was submitted');
        }
      });

    });

  });

agregar.php:

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "inzynierka_test2";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$val1 = $_POST['bar'];

$sql2 = "SELECT `longitude` FROM `lugar` WHERE `Nombre`= $val1 ORDER BY `ID` DESC LIMIT 1";
$result2 = $conn->query($sql2);

if ($result2->num_rows > 0) {
$row2 = $result2->fetch_assoc();
$cal2 = $row2["longitude"];
}
else{
    echo "Nie znalazło miasta";
}


echo json_encode(array( "longitude" => $cal2 ));
Amit Gupta
  • 2,771
  • 2
  • 17
  • 31
workhard
  • 3
  • 1
  • 2
  • 1
    Your code is vulnerable to [**SQL injection**](https://en.wikipedia.org/wiki/SQL_injection) attacks. You should use prepared statements with bound parameters, via either the [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php) drivers. [**This post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) has some good examples. – Alex Howansky Dec 05 '17 at 21:55
  • Yes, of course I will do it. Bt the most important is for me to take matching value from database. I am doing only for me and I am working with localhost. – workhard Dec 05 '17 at 21:59
  • 1
    "I really have no ida how to do it". That is not an acceptable problem statement. With what _specifically_ are you having problems? You posted code, but didn't explain what you think is wrong with it. You said what you want to happen, but not what's _actually_ happening. What debugging have you done? Are you checking your Javascript console for errors? Are you checking your Network tab for the response from your request? – Patrick Q Dec 05 '17 at 22:02
  • @Lawrence Cherone Do you know what I should write instead of $val1 to take this value? – workhard Dec 05 '17 at 22:05
  • if there is no matching value found `echo json_encode(array( "longitude" => $cal2 ));` will not return anything, in fact will likely throw an error. The ajax callback function needs to process the response rather than just popup an alert – Professor Abronsius Dec 05 '17 at 22:30

1 Answers1

0

Send the returned value to index.php and put it in "longitude" input.

if all run well, try this

$(function () {

$('form').on('submit', function (e) {
  e.preventDefault();
  $.ajax({
    type: 'post',
    url: 'http://localhost/inne/phonegap_test/agregar.php',
    data: $('form').serialize(),

    //dont forget set return back 
    success: function ( e) {

      //parse the data from json
      var data = JSON.parse(e);

      //set into longitude input
      $('#longitude').val(data.longitude);

      alert('form was submitted');
    }
  });
});

});

and your php code

if ($result2->num_rows > 0) {
   $row2 = $result2->fetch_assoc();
   $cal2 = $row2["longitude"];
}
else{
   $cal2 = "Nie znalazło miasta";
 } 
return json_encode(array( "longitude" => $cal2 ));
R . dwj
  • 108
  • 6