1

The code that I have below works. I just need to populate the input fields to update the values once the ajax call is completed. However, I don't know the ajax success function to callback the php variable ($first, $last, etc.) to populate those input fields. Thanks in advance!

HTML:

<div class="field-wrap">
  <input id="first" class="form-control" name="first" value="<?php echo $first ?>"/>
  <input id="last" class="form-control" name="last" value="<?php echo $last ?>"/>
  <input id="title" class="form-control" name="title" value="<?php echo $title ?>"/>
  <input id="user_input" class="form-control" name="user_input" type="text"/>
</div>

Script:

<script type="text/javascript">
  $(document).ready(function(){
    $('#user_input').on('change',function(){
      var input_user = $(this).val();
      if(input_user){
        $.ajax({
          type:'POST',
          url:'ajax.php',
          data:{user_input:user_input},
          success: function(){
          ???WHAT DO I NEED TO PUT HERE TO POPULATE THE INPUT FIELDS WITH DATA
          }
        }
      }
    });
  });
</script>

PHP:

$user_input = $_POST['user_input'];
$query_sql = $DB_CON_A->prepare("SELECT * FROM table_account WHERE account=:user_input");
$query_sql->bindValue(':user_input', $user_input, PDO::PARAM_STR);
$query_sql->execute();
$user = $query_sql->fetch(PDO::FETCH_ASSOC);
$first = $user['first_name'];
$last = $user['last_name'];
$title = $user['title'];

2 Answers 2

2

In your php, you will probably just want to echo a json object/array:

die(json_encode($user));

In your ajax success, just parse the encoded json string:

success: function(response){
    var data = JSON.parse(response);
    $('#first').val(data.first_name);
    $('#last').val(data.last_name);
    $('#title').val(data.title);
}

I would probably change your input field names to match the returned columns from the database (like you have for title), that way you can use $.each() and easily auto-poulate any amount of matching input names without having to specifically use an idattribute on your form fields. You could use $('input[name='+key+']').val(value); in a loop IF the column names match the form names.

Also, make sure you are sending back html-safe strings. If user has saved <script>doSomething(nasty)</script> or similar, you could be putting malicious code into your html. I would maybe use htmlspecialchars($value,ENT_QUOTES) for your values on the return. Depends what is in your database for first_name, last_name, and title.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. Will try. Should I use echo(json_encode($user)) or die(json_encode($user)) ?
I would use die() so there is no output after that. It may malform the json back to the ajax. It's safer to die().
Works like a charm. Thank you!!
Thanks for the tip. Will make adjustment to the code accordingly. BTW, if I want to pass the variables/values to angularjs, how should I do it?
Unfortunately I have no experience with angularjs, so I can't help you there!! Sorry.
0

Firstly, your ajax.php should return true or false if return true, it will pass to ajax.success, otherwise, it will pass to ajax.error

In your ajax.success, you should doing something that response(Submission Successful) maybe alert, maybe redirect to other page. Here is a example:

success: function(){
   alert('Submission Successful!! ');
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.