-3

I have Select option which has Customer names. I want to fill phone and email fetched from database onto these 2 input fields. I am using javacript to achieve, followed @mr J answer mentioned HERE. I tried to alert(user); I get selected value but nothing else happens.

addlead.php

 <div class="form-group row">
              <label for="inputEmail3" class="col-sm-4 col-form-label">Phone</label>
                <div class="col-sm-10">
                  <input type="text" class="form-control form-control-sm" id="phone" name="phone" placeholder="Enter Phone no.">
                </div>
            </div>

<div class="form-group row">
             <label for="inputEmail3" class="col-sm-4 col-form-label">Email</label>
                <div class="col-sm-10">
                  <input type="email" class="form-control form-control-sm" id="email" name="email" placeholder="Enter Email" onkeyup="checkemail();" required="">
                </div>
            </div>
<script type="text/javascript">
$(document).ready(function(){
$('#customer_name').on('change',function(){
    var user = $(this).val();
    alert(user);
    $.ajax({
        url : "search2.php",
        dataType: 'json',
        type: 'POST',
        async : false,
        data : { user : user},
        success : function(data) {
            userData = json.parse(data);
            $('#phone').val(userData.phone);
            $('#email').val(userData.email);
        }
    }); 
    });
    });
    </script>

search2.php

 $con = mysqli_connect("localhost", "user_crm", "pass", "kaem_crm");
if (mysqli_connect_errno()){
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$user = $_REQUEST['user'];    
$sql = mysqli_query($con, "SELECT phone,email FROM customer WHERE Cust_name = '".$user."' ");
$row = mysqli_fetch_array($sql);

json_encode($row);die;
Arnold Daniels
  • 16,516
  • 4
  • 53
  • 82
mark
  • 623
  • 3
  • 21
  • 54
  • 1
    Note that your code is susceptible to SQL injection. Please read https://stackoverflow.com/questions/332365/how-does-the-sql-injection-from-the-bobby-tables-xkcd-comic-work – Arnold Daniels Apr 06 '20 at 11:57
  • Do you see any errors in the console when you open your browser's developer toolbar? Did you check the network tab in the toolbar to see what your PHP script is returning? Avoid `alert` as means of debugging, you can get more information and in a cleaner and more usable way if you do a `console.log` instead. – El_Vanja Apr 06 '20 at 12:20
  • Also, drop that `async : false`, you don't need it, you want async for this case. And you don't need `userData = json.parse(data);` when you specify `dataType: 'json'`. – El_Vanja Apr 06 '20 at 12:27
  • if I remove `userData = json.parse(data);` then how would I get value which I am by`$('#phone').val(userData.phone);` – mark Apr 06 '20 at 12:33
  • Because `dataType: 'json'` option that you set in the AJAX request tells it that it will receive JSON as response and then it automatically turns it into an object. – El_Vanja Apr 06 '20 at 12:38
  • so I removed `datatype: 'json'` however kept `userData = json.parse(data);` , then it should be good? – mark Apr 06 '20 at 12:49
  • Yes, that's also a possible way to do it. – El_Vanja Apr 06 '20 at 12:52
  • @mark You have this `onkeyup="checkemail();"` in your HTML. Where and what does that function do? – Funk Forty Niner Apr 06 '20 at 12:53
  • thats for checking email if it exists in database, I have different function for it – mark Apr 06 '20 at 13:00

1 Answers1

-2

You are not returning the result, try this

<?php
$con = mysqli_connect("localhost", "user_crm", "pass", "kaem_crm");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$user = $_REQUEST['user'];    
$sql = mysqli_query($con, "SELECT phone,email FROM customer WHERE Cust_name = 
'".mysqli_escape_string($con, $user)."' ");
$row = mysqli_fetch_array($sql);

header('Content-Type: application/json'); //set the content type for browser
echo json_encode($row); //return result to browser
mysqli_close($con); //close mysql connection

On the form page

  <div class="form-group row">
          <label for="inputEmail3" class="col-sm-4 col-form-label">Name</label>
            <div class="col-sm-10">
              <select class="form-control form-control-sm" id="customer_name" name="name" placeholder="Select Name.">
             <option value="some value">some value</option>
           </select>
            </div>
        </div>
  <div class="form-group row">
  <label for="inputEmail3" class="col-sm-4 col-form-label">Phone</label>
            <div class="col-sm-10">
              <input type="text" class="form-control form-control-sm" id="phone" name="phone" placeholder="Enter Phone no.">
            </div>
        </div>

<div class="form-group row">
         <label for="inputEmail3" class="col-sm-4 col-form-label">Email</label>
            <div class="col-sm-10">
              <input type="email" class="form-control form-control-sm" id="email" name="email" placeholder="Enter Email" onkeyup="checkemail();" required="">
            </div>
        </div>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script type="text/javascript">
 $(document).ready(function(){
 $('#customer_name').on('change',function(){
 var user = $(this).val();
 $.ajax({
    url : "search2.php",
    dataType: 'json',
    type: 'POST',
    async : true,
    data : { user : user},
    success : function(data) {    
        $('#phone').val(data.phone);
        $('#email').val(data.email);
    }
}); 
});
});
</script>
andychukse
  • 520
  • 9
  • 19
  • Copying a bunch of unchanged code obscures the change you made. I'd suggest you remove all the code before return to make your change clearer. – El_Vanja Apr 06 '20 at 12:25
  • You don't need that header, the AJAX is already expecting JSON because `dataType: 'json'` is set. – El_Vanja Apr 06 '20 at 12:30
  • @mark Did you check if the query is returning any results? Also is the search2.php file on same level with your addlead.php file? – andychukse Apr 06 '20 at 12:37
  • yes both files are same level. How do I check if query return any result. Table has the values – mark Apr 06 '20 at 12:46
  • @mark Here's a thorough [tutorial](https://stackoverflow.com/a/22662582/4205384) on debugging db related problems. – El_Vanja Apr 06 '20 at 12:52
  • @andychukse you made customer_name `input type="text"` ? however i tried but no luck – mark Apr 06 '20 at 13:10
  • @mark I have updated the form and I mistakenly type search.php instead of search2.php. Try again, but make sure you update the select options with name that exists on your DB. Also crosscheck the database connection. – andychukse Apr 06 '20 at 13:15
  • @andychukse I know this might sound novice but still, how do check if mysql query returns values? – mark Apr 06 '20 at 13:24
  • @mark Here is a simple way to check. On the search2.php file edit $user = $_REQUEST['user']; to $user = 'some value'; replace some value with a name in the database. Then run the search2.php file in your browser. – andychukse Apr 06 '20 at 13:29
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/211075/discussion-between-andychukse-and-mark). – andychukse Apr 06 '20 at 13:37