add data back to text field

Clash Royale CLAN TAG#URR8PPPadd data back to text field
I'm trying to populate a <input /> from the database using AJAX, but can only get the data testing in a <textarea> or <span>.
<input />
<textarea>
<span>
My AJAX code
$('#part_number').on('change', function() {
var part_number = $(this).val();
$.ajax({
url: '<?PHP echo admin_url('admin-ajax.php'); ?>',
type: 'post',
data: { action: 'description', part_number: part_number },
success: function(data) {
$('#description').val( data );
}
});
});
My input field
<input id="description" value="" />
<input id="description" value="" />
functions.php
add_action('wp_ajax_description' , 'description'); //ajax parameter action
add_action('wp_ajax_nopriv_description','description');
function description(){
$part_number = $_POST['part_number']; //id
global $wpdb;
$stock = $wpdb->get_results("SELECT * FROM _cif_inventory_table WHERE part_number = '$part_number'");
foreach ( $stock as $item ) {
echo $item->Description;
}
die();
}
I couldn't find anything here or google
EDIT This is in a loop so there are 20 lines, but the only one that works is the first one, what did I do wrong?
<?PHP
for ($i = 1; $i <= 20; $i++){
?>
<div id="lines" class="row">
<div class="col-sm-1" style="text-align:left;border:1px solid #808080"><span id="row_number"><?PHP if ( $i < 10 ){ echo "0".$i;}else{echo $i;} ?></span></div>
<div class="col-sm-2" style="text-align:left;border:1px solid #808080">
<select id="part_number" style="border:0px;">
<option value="None Selected"></option>
<?PHP
$partNumbers = $wpdb->get_results("SELECT * FROM _cif_inventory_table;");
foreach ($partNumbers as $partNumber) {
echo '<option value="'.$partNumber->part_number.'">'.$partNumber->part_number.'</option>';
}
?>
</select>
</div>
<div class="col-sm-1" style="text-align:left;border:1px solid #808080"><input size="5" maxlength="5" id="quantity" value="" /></div>
<div class="col-sm-6" style="text-align:left;border:1px solid #808080"><input size="75" id="description" value="" /></div>
<div class="col-sm-1" style="text-align:left;border:1px solid #808080"><input id="unitPrice" value="" /></div>
<div class="col-sm-1" style="text-align:left;border:1px solid #808080"><input id="amount" value="" /></div>
</div><!-- end .row -->
<?PHP
}
?>
This question came from our site for WordPress developers and administrators.
part_number
"can only get the data testing in a textarea or a span?"
html
input
part_number isn't really a number it is a string. Can you give examples of REST I'm not quite sure what you mean
– bckelley
1 hour ago
You use
.val() for input, not .html() ... e.g. like this $('#description').val( data );– LGSon
1 hour ago
.val()
input
.html()
$('#description').val( data );
@LGSon I feel dumb! Thank You! this was actually my first attempt at AJAX! You made my day
– bckelley
1 hour ago
@LGSon Actually maybe you can help with something else I have this input in a for loop and I can only get the first row to actually do anything?
– bckelley
1 hour ago
1 Answer
1
You want to fill in the value attribute of the input, so you need to use .val():
.val()
$('#description').val( data );
$('#description').val( data );
oops yea forgot to edit that
– bckelley
18 mins ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
have you considered using a REST API endpoint instead? Note that you have an SQL injection attack in your code, since you never check that
part_numberis actually a number, and not an SQL statement. Additionally, what do you mean by"can only get the data testing in a textarea or a span?"I presume you're already aware thathtmlsets the inner HTML of elements, butinputelements don't have inner HTML– Tom J Nowell
1 hour ago