Enescan
Enescan

Reputation: 5

Posting the input value with tag in the background

I want it to be posted as an id in the field that users see, but when posting.

https://prnt.sc/z5Hw61LuKoGy -> the area the user sees

https://prnt.sc/plR-s1eb4OGE -> Id data sent with value tag in background

When I post like this, I see it as 0 in my database. https://prnt.sc/XjPHKrthej2M

Why not 4?

Can you help me?

I am using jQuery UI Autocomplete.

MY JQUERY CODE

$("#urun_olustur .col-10 input").autocomplete({
    source: function(request,response){
        $.ajax({
            url: 'ajax.php',
            type: 'post',
            dataType: 'json',
            data: {
                search: request.term
            },
            success: function(data){
                response(data);
            }
        });
    },
    select: function(event,ui){
        $(this).val(ui.item.label);
        $(this).attr("value",ui.item.value);
        return false;
    }
});

MY AJAX.PHP CODE

if (isset($_POST["search"])) {
$search = $_POST["search"];
$query = $db->query("SELECT * FROM test WHERE test_name LIKE '%".$search."%'");
$response = array();
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
    $response[] = array(
        "value" => $row["id"],
        "label" => $row["test_name"]
    );
}
echo json_encode($response);
exit;
}

Upvotes: 0

Views: 75

Answers (1)

adydcoder15
adydcoder15

Reputation: 64

You can get like this I have add following snippet please check.

I have take the change event you can use any other required event as per your convenience

$("#ac_text_id").on('autocompletechange change', function() {
  $('#ac_text_op').html('You selected: ' + this.value);
}).change();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="text" class="form-control ui-autocomplete-input" name="ac_text" id="ac_text_id" autocomplete="off" value="4">

<div id="ac_text_op"></div>

Upvotes: 1

Related Questions