Client Side Coding > Javascript & Ajax

text transfer from db table

<< < (3/4) > >>

richei:
lol, i quickly hit a big roadblock.  Having no experience with ajax, i'm at a loss as to what's going on here.
my ajax capture.  I was using the tutorial from http://www.tizag.com/ajaxTutorial/ajax-mysql-database.php, which before it crapped out, wasn't exactly known for having accurate tutorials.

--- Code: ---<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
function ajaxFunction(){
var ajaxRequest;  // The variable that makes Ajax possible!

try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
//document.myform.time.value = ajaxRequest.responseText;
var ajaxDisplay = document.getElementById('n_answer');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var a_id = document.getElementById('danswer').value;
var queryString = "?a_id=" + a_id;
ajaxRequest.open("GET", "ajax_query.php" + queryString, true);
ajaxRequest.send;
}

//-->
</script>
--- End code ---

ajax_query.php

--- PHP Code: ---
<?php
include 'config.php';

$id = mysql_real_escape_string($_GET['a_id']);
$qry = mysql_query("SELECT danswer FROM venzo_contactus_answers WHERE id = $id") or die(mysql_error());
$row = mysql_fetch_assoc($qry);

$display_string = $row['danswer'];

echo $display_string;
?>

--- End code ---



the form

--- Code: ---<form action="" method="post" name="myform">
<div style="float: left; width: 90px; margin: 0 auto;">
<input type="submit" name="close" value="close message" />
</div>

<div style="float: right; width: 100px;">
<select name="danswer" id="danswer" >
<?php
$ans = mysql_query("SELECT * FROM venzo_contactus_answers") or die(mysql_error());
while($row = mysql_fetch_array($ans)) {
echo "<option value='$row[id]'>$row[name]</option>";
}
unset($row);
?>
</select>
</div>
<div style="float: right; width: 100px;">
<input type="button" id="a_select" name="select" value="Select" onclick="ajaxFunction"/>
</div>
<input type="hidden" name="id" value="<?=$id?>" />
<input type="hidden" name="req_id" value="<?=$r['req_id']?>" />
<input type="hidden" name="u_name" value="<?=$r['name']?>" />
<input type="hidden" name="a_name" value="<?=$_SESSION['adminname']?>" />

</form>
<div style="clear: both;"> </div>
<form action="" method="post" name="myforma" >
<input type="hidden" name="id" value="<?=$id?>" />
<input type="hidden" name="req_id" value="<?=$r['req_id']?>" />
<div id="d_answer"></div>
<textarea style="width: 100%;" rows="12" name="n_answer" id="n_answer"><?=nl2br(strip_slashes_recursive($r['answer']))?></textarea>
<div align="center" style="width: 100%;"><input type="submit" name="Answer" value="Answer" /></div>
</form>

--- End code ---

I dont' know if its from having 2 different forms or what, but nothing happens when i click on select.

ErnieAlex:
Well, when you call a function that has the format "ajaxfunction()"   You must call it with "ajaxfunction();" in the ONCLICK.  So, I think this is the problem.  ONCLICK="ajaxfunction();"

Let us know if that gets it working.  And, if it throws an error, post the error also...  Good luck.

richei:
yea i caught that, didn't work when i fixed it.

ErnieAlex:
Well, do us a favor...  In the form once it is up in a browser, RIGHT-CLICK on it and select VIEW-SOURCE.
Then, paste the code here.  This will show us exactly what the code looks like AFTER the PHP code.  It should show up any javascript- calling errors and might help sort this out.

Thanks...

richei:
I got it working.  I was missing the ; in the function call and i had to comment out document.myform.time.value = ajaxRequest.responseText;

thanks for the help

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version