Jump to content

Recommended Posts

I'm really not so good with ajax, I just need this IF condition to work.

<script>
$(document).ready(function() {
	$.ajax({
		url: 'layouts/cities-list.php', 
		data: 'debut', 
		dataType: 'json', 
		success: function(json) {
			       $.each(json, function(index, value) { 
				$('#country').append('<option value="'+ index +);
				if (index == 144)
				{
					$('#country').append(' selected="selected"');
				}
				$('#country').append('>'+ value +'</option>');
			});
		}
	});
});
</script>

 

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/268046-if-problem-with-append-function/
Share on other sites

Yes there is an error, I forgot to do this:

.append('<option value="'+ index +'"');

but that doesnt solve it. This is what it ouputs:

<option select="" <="" value="1"></option>
>Afghanistan
<option select="" <="" value="193"></option>
>Afrique du Sud
<option select="" <="" value="2"></option>
>Albanie
<option select="" <="" value="3"></option>
>Alg?rie
<option select="" <="" value="81"></option>
>Allemagne

 

So there are still other errors. right?

 

Thanks, alright I solved it!

 

here is my new code:

<script>
$(document).ready(function() {
	var country_list;
	$.ajax({
		url: 'layouts/cities-list.php', 
		data: 'debut', 
		dataType: 'json', 
		success: function(json) {
			$.each(json, function(index, value) { 
				country_list = '<option value="'+index+'"';
				if ( index == 144 )
					country_list += ' selected="selected"';
				country_list += '>'+value+'</option>';
				$('#country').append(country_list);
			});
		}
	});
});
</script>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.