It's a lot more work when using ajax: https://stackoverflow.com/questions/61030806/keep-old-select-value-while-validation-failed-in-ajax-in-laravel
I generally show errors in a div:
error: function (data, ajaxOptions, thrownError) {
var status = data.status;
if (data.status === 422) {
$.each(data.responseJSON.errors, function (key, value) {
$('#msg').append('<div>' + value + '</div>');
});
}
if (status === 403 || status === 500) {
$('#msg').text("Not Auth");
}
}
You would probably be better off YES do the put or post with ajax, but have the select regular form, not ajax - just my opinion. That way old data is easier.
I did a gist for checkbox array, it's similar for select:
https://gist.github.com/jimgwhit/3c1026871ca4246d6ae9f71012ba8e4d
I plan on doing one for select when I get time.
Here is the whole put method:
$(function () {
$("#postjqzz").click(function (event)
{
event.preventDefault();
alert("here");
var $post = {};
$post.petid = $('#petid').val();
$post.species = $('#species').val();
$post.ocheck = ($("#ocheck").prop("checked") == true ? '1' : '0');
$post._token = document.getElementsByName("_token")[0].value
$post._method = document.getElementsByName("_method")[0].value
alert($post.species);
$.ajax({
url: '<?= DIR . "pet/petupdate" ?>',
type: 'PUT',
data: $post,
cache: false,
success: function (data) {
var message = "data updated";
alertWithoutNotice(message);
$('#msg').append('<div>' + data.success + '</div>');
},
error: function (data, ajaxOptions, thrownError) {
var status = data.status;
if (data.status === 422) {
$.each(data.responseJSON.errors, function (key, value) {
$('#msg').append('<div>' + value + '</div>');
});
}
if (status === 403 || status === 500) {
$('#msg').text("Not Auth");
}
}
});
});
function alertWithoutNotice(message) {
setTimeout(function () {
alert(message);
}, 1000);
}
});