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
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?

Link to comment
Share on other sites

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.