-4

I am using this js code for an ajax request and getting the data, this code works well & show the data in the textbox. But for some reasons, I want to get the value of js variable id1 into php code(index.php). I tried but not able to get the value of id1 into php code, plz help.

HTML CODE:(index.php)

<body>
  <input type="text" name="urn_no" ID="urn_no" value=""/>
  <input type="number" name="mob_1" ID='mob_1' value="" onkeydown="getData(this.value);" />
 </body>

JS CODE:(code in index.php)

function getData(val) {
  $.ajax({
    type: "POST",
    url: "test.php",
    dataType: "json",
    data:'dt='+val,
    success: function(data){
      var len = data.length;
      if(len > 0){
        var id1 = data[0]['id1'];
        // Set value to textboxes
        document.getElementById('urn_no').value = id1;
      }
    }
  });
}

PHP CODE(test.php)

<?php
if (! empty($_POST["dt"])) {
  $sql="SELECT * FROM call_data WHERE U_no = '".$_POST["dt"]."'";
  $result = mysqli_query($con,$sql);
  if(!$result || mysqli_num_rows($result)<=0) {

  } 
  $users_arr = array();
  while( $row = mysqli_fetch_array($result) ) {
    $r1=$row['URN_Number'];
    $users_arr[] = array("id1" => $r1);
  }
  echo json_encode($users_arr);
  exit;
}
?>
Shalini
  • 123
  • 1
  • 10
  • 3
    Debugging 101: In your success function, add `console.log(data)` (or use POSTMAN to check your PHP output) – ChrisG Jul 18 '18 at 11:03
  • i am getting the correct value in textbox by document.getElement – Shalini Jul 18 '18 at 11:12
  • So is this solved then? – ChrisG Jul 18 '18 at 11:16
  • yes is solved if i want to get the value in textbox, but i want to get the value in php code too. I am not able to get the value in php code – Shalini Jul 18 '18 at 11:27
  • The `id1` is sent back by PHP in the first place (it's `$r1`), so why do you want to get it back from JS to PHP? I have no idea what you're trying to do, or why. – ChrisG Jul 18 '18 at 11:35
  • It comes into the test.php page but I need it in the index.php page. The page "test.php" is called by ajax request. and I don't know how to get the value of $r1(test.php) into the PHP code written in index.php – Shalini Jul 18 '18 at 11:41

1 Answers1

-1

Here's a way to keep loading relevant data while the user is typing:

<?php

$mob_1 = filter_input(INPUT_GET, 'mob_1');

// if we're responding to an AJAX call, send back HTML fragment
if ($mob_1) {
    $sql = "SELECT URN_Number FROM call_data WHERE U_no = $mob_1 LIMIT 1";
    $result = mysqli_query($con, $sql);
    if (!$result || mysqli_num_rows($result) <= 0) {
        echo "Nothing found";
        exit();
    }
    $URN = mysqli_fetch_array($result)['URN_Number'];
    echo "<p>Here's some content related to URN $URN</p>";
    exit();
}

// if not, send back main document
?><!doctype html>
<html>
    <head>
        <title>URN Lookup</title>
        <meta charset="utf-8">
    </head>
    <body>
        <input type="number" id="mob_1" />
        <div id="result"></div>
        <script>
            $('#mob_1').on('keyup', function () {
                // load HTML fragment
                $('#result').load("index.php?mob_1=" + $(this).val());
            });
        </script>
    </body>
</html>
ChrisG
  • 8,206
  • 4
  • 20
  • 34
  • but i don't want the id1 on button or link click. It should be automatically send by test.php code in url – Shalini Jul 18 '18 at 12:09
  • In that case just do `document.location.href = "index.php?id1=" + id1;` – ChrisG Jul 18 '18 at 12:36
  • I can change my answer accordingly if you describe what you actually want to happen from a user's perspective. As far as I understand, you want to type some number into a textfield and automatically display data below? Or something like that? – ChrisG Jul 18 '18 at 12:50
  • I want to get the value typed in mob_1 and based on this value i want to serach U_no and fill the value in textbox and also want to get the value in php code. Becuase in php code i trigger a new query based on the selection of U_no. – Shalini Jul 19 '18 at 04:24