Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
193
rated 0 times [  195] [ 2]  / answers: 1 / hits: 22064  / 11 Years ago, wed, november 13, 2013, 12:00:00

I'm new to AJAX and trying to learn basic calls through AJAX and jQuery.
I have a simple drop list of countries, where I want to select a particular country and send the value of it to server, where it would process which country was selected and give particular output. For now, it could just echo simple output in php file. There is some kind of problem with this code. I would appreciate if anyone could help me with it. thanks



 <html>
<head>
<script src=http://code.jquery.com/jquery-1.9.1.js></script>
<script type=text/javascript>
function load()
{
$.ajax({
type:GET,
url: test.php,
data: { country: $(#country).val()}
}).done(function(msg){
$(#right #myDiv).html(msg);
});
}

</script>
</head>
<body>

<div id=main>
<div id=left>
Select Country: <select id=country name=country>
<option value=germany>Germany</option>
<option value=england>England</option>
</select>
<input type=button value=Run Query onClick=load()></input>
</div>

<div id=right>
<div id=myDiv></div>
</div>
</div>





test.php



 <?php
$name=$_GET['country'];
if($name==England)
{
echo Works;
}
else
{
echo doesnt Work;
}
?>
?>

More From » php

 Answers
48

The only issue I see is that in your PHP you have



if($name==England)


But in your html you have



<option value=england>England</option>


It is sending over england. england != England.



Also, I would just do



$(#country).val()


instead of



$(#country option:selected).val()

[#74299] Tuesday, November 12, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
elijahm

Total Points: 674
Total Questions: 124
Total Answers: 79

Location: Northern Mariana Islands
Member since Fri, Jan 15, 2021
3 Years ago
;