Jump to content


Updating SELECT forms using AJAX


5 replies to this topic

#1 .CJ

    Young Padawan

  • Members
  • Pip
  • 114 posts
  • Gender:Male
  • Location:Leeds, UK

Posted 09 March 2007 - 06:04 PM

Hi all,

For hours now I've been trying to make a select form update the database when a 'onchange' occurs. I'm using Prototype instead of custom AJAX, as I prefer it, also I'm using PHP in the back end.

Can anyone be a kind soul and let me know any any code or tutorials that tell me how to update a database using an 'onchange' command and using PHP in the back end.

Thanks all, hope to hear from you soon.

Chris.

#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 09 March 2007 - 09:02 PM

It'd probably be easier if you show us what you have so far and tell us whats going on, then have us help you fix it. I doubt you'll find a single tutorial that will do this for you, a mix maybe, but not one individual tutorial.

#3 .CJ

    Young Padawan

  • Members
  • Pip
  • 114 posts
  • Gender:Male
  • Location:Leeds, UK

Posted 09 March 2007 - 09:28 PM

Ok.

HTML/PHP
echo('
		  <select name="brand" onchange="changeBrand('.$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>
');

JS
// Update roster brand
function changeBrand(id) {
	new Ajax.Updater('brandChange', 'js/brand.php?id=' + id + '&mgroup=' + document.form.brand.options[document.form.brand.selectedIndex].value);
}

PHP
<?php
// functions
include("../include/functions.php");
// MySQL connection
connect();

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

// close MySQL
mysql_close();
?>

I'd like to point out just that I'm not really a newbie, nor am I a guru and have used Prototype and got it working bang on, but using it with SELECT form's has baffled me.

#4 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 09 March 2007 - 10:08 PM

What part of it isn't working?

You can get the value of the select element just like any other form element with Prototype.
$F('brand')

I would assign the select menu an ID also, as JavaScript tends to use to use that more than the name attribute.

Other than that, I don't get what you would want to update with a select element. What exactly do you think you can update in the element?

Your method just doesn't make sense to me, lol.

Just use a regular Ajax.Request method, and it'll be just fine.

Edited by Demonslay, 09 March 2007 - 10:10 PM.


#5 .CJ

    Young Padawan

  • Members
  • Pip
  • 114 posts
  • Gender:Male
  • Location:Leeds, UK

Posted 09 March 2007 - 11:01 PM

Yeah, really, 'tis rather gibberish, I guess I find it hard to explain. I did try Ajax.Request, wasn't sure how to code the PHP though, I guess not too different from what I use above.

#6 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 11 March 2007 - 12:17 PM

Ajax.Request is the very core of anything Prototype uses for Ajax actions, so it's the exact same thing pretty much. Just read the Documentation for more information. :angrylooking:





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users