Web Hosting Talk







View Full Version : using prototype to do an autopopulate. select from a drop list


Satch
10-31-2008, 12:25 AM
using prototype to do an autopopulate. select from a drop list

* screwed up the title sorry, getting late *
so i'm using prototype to do an autopopulate. select from a drop list and then a textbox is supposed to get filled. it works fine if it's going into a div but it doesn't work for a text box for some reason. the code is a little flaky but it does work for a div
file 1
Code:
<html>
<head>
<title>Changing textbox value based on dropdown list using Ajax and PHP</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script src="ajax.js"></script>
<script src="prototype.js"></script>
<script>
function sendRequest() {
new Ajax.Request("ajax.php",
{
method: 'post',
postBody: 'id='+ $F('id'),
onComplete: showResponse
});
}
function showResponse(req){
$('fadeBlock').innerHTML= req.responseText;
//document.form1.cur_code.value = 'asdfad';
document.form1.cur_code.value = eval(req.responseText);
//document.getElementById('cur_code').value=req.responseText;
//$F('cur_code').value = 'asdf';
}
</script>





</head>

<body style="font: 12px Verdana, Arial, Helvetica, sans-serif;">
<div id="fadeBlock"></div>

<?php
$sql = "SELECT * FROM product WHERE productid =11749";
$result = mysql_query($sql);
?>

<form style="text-align:center" method="post" action="" name="form1" id="form1">
<p style="color:#000099 ">When you change the dropdown list, the respective currency code of the country will be displayed in the textbox which is fetched from PHP using Ajax. </p>
<p>Country : <select name="id" onChange="sendRequest()" id="id">
<option value="">Select Country</option>
<?php
while($row = mysql_fetch_array($result)) {
echo "<option value=\"$row[productid]\">$row[prodname]</option>\n";
}
?>

</select><br/><br/>
Currency : <input type="text" name="cur_code" id="cur_code" ></p>
</form>
</body>
</html>
ajax file getting called
Code:
<?php
include_once('functions.php');
$sql = "SELECT * FROM product WHERE id =11749";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
echo $row[productid];

?>
insights are appreciated





Last edited by Satch : 10-31-2008 at 12:30 AM.

Satch
10-31-2008, 12:10 PM
actually document.form1.cur_code.value = req.responseText seems to work in a way. it put a lot of space at the beginning of the response so i didn't notice that it was returning something in the text box. it's returning this
Code:
<!-- //javascript functions, then php functions --> <script> function preparehtml(whichdiv) { //alert("preparing"); //alert("preparing" + whichdiv); document.all("htmlfromdiv").value=document.all(whichdiv).innerHTML; document.all("prepared").value=whichdiv; } function preparetextarea(whichdiv) { //alert("preparing"); alert("preparing" + whichdiv); document.all("htmlfromdiv").value=document.all(whichdiv).value; document.all("prepared").value=whichdiv; } </script> <SCRIPT LANGUAGE='JAVASCRIPT' TYPE='TEXT/JAVASCRIPT'> <!-- /**************************************************** AUTHOR: WWW.CGISCRIPT.NET, LLC URL: http://www.cgiscript.net Use the code for FREE but leave this message intact. Download your FREE CGI/Perl Scripts today! ( http://www.cgiscript.net/scripts.htm ) ****************************************************/ var win=null; //MO 2004/02/16 Modified function to take additional variables for scrollbars function NewWindow(mypage,myname,w,h,pos,infocus,sbars){ if(pos=="random"){myleft=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;mytop=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;} if(pos=="center"){myleft=(screen.width)?(screen.width-w)/2:100;mytop=(screen.height)?(screen.height-h)/2:100;} else if((pos!='center' mytop=20} settings="width=" + w + ",height=" + h + ",top=" + mytop + ",left=" + myleft + ",scrollbars=" + sbars + ",location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no,titlebar=no";win=window.open(mypage,myname,settings); win.focus();} //MO function NewWindowdebug(mypage,myname,w,h,pos,infocus){ if(pos=="random"){myleft=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;mytop=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;} if(pos=="center"){myleft=(screen.width)?(screen.width-w)/2:100;mytop=(screen.height)?(screen.height-h)/2:100;} else if((pos!='center' mytop=20} settings="width=" + w + ",height=" + h + ",top=" + mytop + ",left=" + myleft + ",scrollbars=yes,location=no,directories=no,status=yes,menubar=no,toolbar=yes,resizable=yes,titlebar=yes";win=window.open(mypage,myname,settings); win.focus();} //MO 2004/02/16 Modified function to take additional variables for window size function CMS_PopUp(msgid, typeid) { if (typeid==null) { NewWindow('/site/cms_popup.php?msgid='+msgid,'cms_popupwindow','390','220','null','front','no'); } else if (typeid==1) { NewWindow('/site/cms_popup.php?msgid='+msgid,'cms_popupwindow','400','400','null','front','no'); } else if (typeid==2) { NewWindow('/site/cms_popup.php?msgid='+msgid,'cms_popupwindow','500','500','null','front','yes'); } } //MO // --> </script> 11749 111
all i need is what's at the end - 11749 111. is there anyway just to return that?

Adam-AEC
10-31-2008, 12:53 PM
What does ajax.php look like?
You should try to return JSON if possible.

Satch
10-31-2008, 12:58 PM
Quote:



Originally Posted by Adam-AEC


What does ajax.php look like?
You should try to return JSON if possible.


it isn't final but its' good enough for testing purposes
Code:
<?php
include_once('functions.php');
$sql = "SELECT * FROM product WHERE id =11749";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
echo $row[productid];

?>
i've never really used JSON before so i'd need some assistance with that. what i have does return to a div cleanly enough but not a text box for whatever reason

Adam-AEC
10-31-2008, 01:21 PM
Well I think you should be able to use the code you have, but you need to figure out where the extra text is being injected into your responseText.
Download firebug, fire off a manual call to your ajax.php file, and see what it returns. You just want the text returned, nothing else.

Satch
10-31-2008, 02:03 PM
Quote:



Originally Posted by Adam-AEC


Well I think you should be able to use the code you have, but you need to figure out where the extra text is being injected into your responseText.
Download firebug, fire off a manual call to your ajax.php file, and see what it returns. You just want the text returned, nothing else.


i removed the include and mysql stuff and just hard coded some values. it worked but i need to have the include and mysql stuff as it needs to be dynamic. not sure what to do here ....