I have found an auto suggest script that is working. I am trying to add some codes in order for html select form to pass its value to ajax.js then from there it will pass the value to script_page.php something like these,
PHP Code:
$str2= strtolower($_GET['content2']);
so I can create at script_page.php a condition on what sql query to do.
But to no avail, it does not pass the value. I get warning of undefined index content2. Below is the 2 main files.
Ajax.js
Code:
subject_id = '';
function handleHttpResponse() {
if (http.readyState == 4) {
if (subject_id != '') {
document.getElementById(subject_id).innerHTML = http.responseText;
}
}
}
function getHTTPObject() {
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
}
return xmlhttp;
}
var http = getHTTPObject(); // We create the HTTP Object
function getScriptPage(div_id,content_id)
{
subject_id = div_id;
content = document.getElementById(content_id).value;
http.open("GET", "script_page.php?content=" + escape(content), true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
if(content.length>0)
box('1');
else
box('0');
}
function highlight(action,id)
{
if(action)
document.getElementById('word'+id).bgColor = "#C2B8F5";
else
document.getElementById('word'+id).bgColor = "#F8F8F8";
}
function display(word)
{
document.getElementById('text_content').value = word;
document.getElementById('box').style.display = 'none';
document.getElementById('text_content').focus();
}
function box(act)
{
if(act=='0')
{
document.getElementById('box').style.display = 'none';
}
else
document.getElementById('box').style.display = 'block';
}
Rather than trying to do the "get" request, have you tried simply using alert() to test the value of "content2"?
If so, does that alert contain the appropriate value? If the value is not correct, what does it show instead?
I can't tell from the code snippets you have posted but it looks like you are sending the variable "content2" with the javascript but only testing for the variable "content" in the PHP. If the javascript is producing the correct value for content2, then we need to look at the PHP instead as it is probably just a matter of having the wrong variable title.
Take a look into that and let me know what you find.
This is a noob question, I am not familiar with javascript, what is the proper code to test content2 with alert? I know what is alert, but I don't know how to code it to test content2.
Thanks for the response,I appreciate the help. But I finally achieve my desired result. I am not sure it seems with Ajax, once you click another form, the stored value of a variable will be lost?
Assuming the index content is being set by a click on another page.
Thanks for the response,I appreciate the help. But I finally achieve my desired result. I am not sure it seems with Ajax, once you click another form, the stored value of a variable will be lost?
Assuming the index content is being set by a click on another page.
When you click another form that set the index for content2, the data stored on $str will be lost?
Yes, an ajax page call only passes that particular set of data to the server and the server isn't storing old variable values. It's not being "reset" but actually hasn't been set at all in that page load because you haven't specified it in that page load.
If you want to keep data between ajax requests you will need to use the "session" feature of PHP to save $_GET['content'] into $_SESSION['content'] and to save $_GET['content2'] into $_SESSION['content2']. Then those variables will be available to you for as long as you like if you access them through the $_SESSION variable.