i'm having a problem with jquery and php, mainly php :S i have a select populated via php, that's the code:
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/script.js"></script>
<select name="gname" id="gnome">
<option>Scegli...</option>
<?php
include ('php/config.php');
//recupero organizzazioni dal database
$sql = "SELECT DISTINCT gname, count(*) as number
FROM gtdb
WHERE `country` = 4 OR `country` = 103 OR `country` = 107 OR `country` = 219 OR `country` = 210 OR `country` = 210 OR `country` = 153 OR `country` = 110 OR `country` = 200 OR `country` = 102 OR `country` = 95 OR `country` = 173 OR `country` = 228 OR `country` = 152 OR `country` = 97 OR `country` = 94
GROUP BY gname
HAVING number >= 20
ORDER BY gname";
$result = mysql_query($sql, $mysql);
while($nome = mysql_fetch_array($result)) {
?>
<option value= "<?php echo $nome['gname'];?>" ><?php echo $nome['gname']; ?></option>
<?php
};
?> </select>
when i select an option, this jQuery function should post the value of the option to a php script and append the result into a "result" div. jquery code:
$(document).ready(function() {
$('#gnome').change(function() {
var inpval=$(this).val();
$.ajax({
url: 'php/query.php',
type: 'POST',
data: {valor : inpval},
success: function(data) {
$('.result').html(data);
}
});
});
});
PHP code:
<?php
include('config.php'); //database connection(works)
$org_name=$_POST['valor'];
$query=mysql_query("SELECT * FROM gtdb WHERE gname LIKE '$org_name%'");
while($row=mysql_fetch_assoc($query)){
$row['name'];
}
?>
Actually i'm able to connect to the database, do a query and populate the select, but when i select an option nothing happens, i don't know if it's fault of the jquery code or of the php code, any help? Thanks in advance :)
EDIT: html code that reaches the browser, for user3558931
<p>gname</p>
<select name="gname" id="gnome">
<option>Scegli...</option>
<option value= "Al-Aqsa Martyrs Brigade" >Al-Aqsa Martyrs Brigade</option>
<option value= "Al-Nusrah Front" >Al-Nusrah Front</option>
<option value= "Al-Qa`ida in Iraq" >Al-Qa`ida in Iraq</option>
<option value= "Al-Qa`ida in the Arabian Peninsula (AQAP)" >Al-Qa`ida in the Arabian Peninsula (AQAP)</option>
<option value= "Baloch Liberation Army (BLA)" >Baloch Liberation Army (BLA)</option>
<option value= "Baloch Republican Army (BRA)" >Baloch Republican Army (BRA)</option>
<option value= "Free Syrian Army" >Free Syrian Army</option>
<option value= "Gunmen" >Gunmen</option>
<option value= "Hamas (Islamic Resistance Movement)" >Hamas (Islamic Resistance Movement)</option>
<option value= "Haqqani Network" >Haqqani Network</option>
<option value= "Individual" >Individual</option>
<option value= "Islamic State of Iraq (ISI)" >Islamic State of Iraq (ISI)</option>
<option value= "Lashkar-e-Islam (Pakistan)" >Lashkar-e-Islam (Pakistan)</option>
<option value= "Lashkar-e-Jhangvi" >Lashkar-e-Jhangvi</option>
<option value= "Militants" >Militants</option>
<option value= "Other" >Other</option>
<option value= "Palestinian Islamic Jihad (PIJ)" >Palestinian Islamic Jihad (PIJ)</option>
<option value= "Popular Front for the Liberation of Palestine (PFLP)" >Popular Front for the Liberation of Palestine (PFLP)</option>
<option value= "Popular Resistance Committees" >Popular Resistance Committees</option>
<option value= "Sindhu Desh Liberation Army (SDLA)" >Sindhu Desh Liberation Army (SDLA)</option>
<option value= "Southern Mobility Movement (Yemen)" >Southern Mobility Movement (Yemen)</option>
<option value= "Taliban" >Taliban</option>
<option value= "Tehrik-i-Taliban Pakistan (TTP)" >Tehrik-i-Taliban Pakistan (TTP)</option>
<option value= "Unknown" >Unknown</option>
</select>
And yes, it's about terrorism XD
Please change:
data: ({valor : inpval}),
To:
data: {valor : inpval},
And the statement obtaining the data value should be:
var inpval = this.value;
JS FIDDLE DEMO
When you change the select value please open dev tools and check the network tab and you'll see that the ajax call is made.
Also make sure you have an element with the class result is on the page.
NOTE: Your PHP script needs to be edited so it can return something, maybe echo $row['gname'];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With