2

I want my position to show as a string. Can someone give me clue on how to do this? Im just new to laravel and javascript. enter image description here

public function myformAjax(Request $request)
{
 $position =Tbl_press_release_recipient::select('position')->where('country',Request::input('country'))->distinct()->get();
 return json_encode($position);
}

my ajax:

 $(document).ready(function() {
 $('#country').on('change', function() {
 var country = this.value;
 if(country){
 $.ajax({
 url: '/member/page/press_release_email/choose_recipient_press_release/ajax',
 type: "GET",
 data:{country:country},
 dataType: "json",
 success:function(data) {
 $('select[name="position"]').empty();
 $.each(data, function(key, value) {
 $('select[name="position"]').append('<option value="'+ JSON.stringify(key) +'">'+ JSON.stringify(value) +'</option>');
 });
 }
 });
 }else{
 alert("as23d");
 }
 });
 });
asked Oct 19, 2017 at 8:13

2 Answers 2

1

You're stringifying the value object whereas you need to access the position property of the value object:

$('select[name="position"]').append(
 $('<option>', { value : value.position, text : value.position })
);

This also creates an option element object to set the text and value, instead of concatenating the HTML which could lead to XSS vulnerabilities.

answered Oct 19, 2017 at 8:23
Sign up to request clarification or add additional context in comments.

Comments

0

If in ajax response you get json like {key: value}, check this:

...,
success: function(repsonse) {
 $.each(repsonse, function(i, elem) {
 $('select[name="position"]').append('<option value="'+ i +'">'+ elem +'</option>');
 });
}

jsfiddle: https://jsfiddle.net/tomol1111/7sw53q45/

answered Oct 19, 2017 at 8:33

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.