Web Hosting Talk


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : using prototype to do an autopopulate. select from a drop list
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.

 
Thread Tools Search this Thread Display Modes
  #1  
Old 10-31-2008, 12:25 AM
Satch Satch is offline
View Beta Profile
Newbie
 
Join Date: Aug 2006
Posts: 10
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.

Reply With Quote
Sponsored Links
  #2  
Old 10-31-2008, 12:10 PM
Satch Satch is offline
View Beta Profile
Newbie
 
Join Date: Aug 2006
Posts: 10
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?

Reply With Quote
  #3  
Old 10-31-2008, 12:53 PM
Adam-AEC Adam-AEC is offline
View Beta Profile
Web Hosting Master
 
Join Date: Feb 2003
Location: Canada
Posts: 828
What does ajax.php look like?
You should try to return JSON if possible.

Reply With Quote
Sponsored Links
  #4  
Old 10-31-2008, 12:58 PM
Satch Satch is offline
View Beta Profile
Newbie
 
Join Date: Aug 2006
Posts: 10
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

Reply With Quote
  #5  
Old 10-31-2008, 01:21 PM
Adam-AEC Adam-AEC is offline
View Beta Profile
Web Hosting Master
 
Join Date: Feb 2003
Location: Canada
Posts: 828
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.

Reply With Quote
  #6  
Old 10-31-2008, 02:03 PM
Satch Satch is offline
View Beta Profile
Newbie
 
Join Date: Aug 2006
Posts: 10
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 ....

Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement: