Jump to content

AJAX/PHP and Drop Down Menu's


CMellor

Recommended Posts

Hello,

 

I have been trying to develop a bit of code to allow me to update a database field when an onChange occures on a drop down box. For some odd reason, it doesn't work at all, also I might add I am using Prototype.

 

Here's my code:

 

<script language="Javascript" type="text/javascript">
// Update roster brand
function changeBrand(value, id) {
new Ajax.Updater('div', 'js/brand.php?show=' + value + '&id=' + id);
}
</script>

 

<?php
// MySQL connect
include('../../include/functions.php');
// connect
connect();

if(!isset($_GET['show'])) {
mysql_query("UPDATE ibf_members SET
	mgroup = '".$_GET['show']."'
WHERE id = '".$_GET['id']."'");
}

// close MySQL
mysql_close();
?>

 

echo('
		<form action="" method="post" name="form">
		<select name="brand" onchange="changeBrand(document.form.brand.options[document.form.brand.selectedIndex].value, '.$row['id'].'); return false">
		');
		// query
		$group_query = mysql_query("SELECT * FROM ibf_groups, ibf_members WHERE ibf_groups.g_id = ibf_members.id AND g_id IN(3, 4, 6, 7)");
		// fetch data
		while($groups = mysql_fetch_array($group_query)) {
		$selected = $row['mgroup'] == $groups['g_id'] ?  'selected' : '';
		echo('
		<option value="'.$groups['g_id'].'" '.$selected.'>'.$groups['g_title'].'</option>
		');
		}
		echo('
		</select>
		</form>
		<div id="div"></div>
');

 

Does anything look wrong?

Link to comment
https://forums.phpfreaks.com/topic/42263-ajaxphp-and-drop-down-menus/
Share on other sites

2 things:

 

Try changing it to onChange (capital C)

Or try using a different event.

 

You're also not checking the result of the SQL for a valid result (nor are you cleaning the input).  That's never good.

 

Are you getting any JS errors otherwise?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.