Ajax Talk - Asynchronous javascript and xml discussion board
 FAQ   Search    Usergroups      Register   Profile   Check your messages   Log in 
Signup now to enjoy a range of free membership advantages !!
Ajax Talk - Moderators Wanted
Ajax + PHP

 
Post new topic   Reply to topic    Ajaxtalk.com Forum Index -> AJAX Help
View previous topic :: View next topic  
Author Message
UsagiChan
New one
New one


Joined: 04 Oct 2007
Posts: 2

PostPosted: Thu Oct 04, 2007 4:04 am    Post subject: Ajax + PHP Reply with quote

I am making a script which queries a database for synonyms to a word. T have this so far:


<html>
<body>

<script type="text/javascript">
function ajaxFunction()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.myForm.time.value=xmlHttp.responseText;
}
}
xmlHttp.open("GET","/scripts/syn_list.php/"<? echo $word ?>,true);
xmlHttp.send(null);
}
</script>

<form name="myForm">
Word: <input type="text" name="word" />
<input type="button" value="Go" onClick="ajaxFunction();"><br>
Synonyms:<br>
<textarea name="time" cols="40" rows="40"></textarea>
</form>

</body>
</html>


The PHP program being called (/scripts/syn_list.php) is:

$dbuser = 'myuser';
$dbhost = 'localhost';
$dbpass = 'mypassword';
$dbname = 'mydatabase';
$link = mysql_connect($dbhost,$dbuser,$dbpass)

$url_array1 = explode("?",$REQUEST_URI); // get rid of session info
if (!empty($url_array1[1])) {
$noindex = 'T';
}
$url_array=explode("/",$url_array1[0]);

$word = $url_array[3];

$query="SELECT * FROM syn_words WHERE word = '$word' " ;
$result = mysql_db_query($dbname, $query, $link) or die (email_error(__FILE__,__LINE__,mysql_errno(),mysql_error())) ;
$myrow = mysql_fetch_array($result);
$word_id = $myrow['word_id'] ;

$query="SELECT * FROM syn_synonyms WHERE word_id = '$word_id' ORDER BY synonym " ;
$result = mysql_db_query($dbname, $query, $link) or die (email_error(__FILE__,__LINE__,mysql_errno(),mysql_error())) ;
$n=0;
do {
if ($n > 0) {
echo $myrow['synonym'] . "\n" ;
}
$n = $n + 1 ;
} while ($myrow = mysql_fetch_array($result)) ;

if($n == "1") {
echo "No Synonyms" ;
}

My problem is this:

I need to pass "word" in the form to the PHP program. The line in red is the problem and it simply doesn't work. I've tried several variations of that.

Anyone know how to do this?

Thanks in advance,
UsagiChan
Back to top
View user's profile Send private message
evan_carothers
Light Frequent Poster
Light Frequent Poster


Joined: 11 Oct 2007
Posts: 9
Location: River Falls, WI

PostPosted: Thu Oct 11, 2007 11:07 pm    Post subject: Reply with quote

I'm not sure why you're trying to inject $word into the function for xmlhttp.open, but if you're trying to pull the value from the current word input your function will look like this:
Code:

var word = document.myForm.word.value();
 xmlHttp.open("GET","/scripts/syn_list.php?word="+word,true);
 

Then insead of using your extremely overcomplicated method to remove the session information from the url in the script, just do a simple
Code:

$word = $_GET['word'];
Back to top
View user's profile Send private message Send e-mail
UsagiChan
New one
New one


Joined: 04 Oct 2007
Posts: 2

PostPosted: Fri Oct 12, 2007 7:23 am    Post subject: Reply with quote

Sorry that I didn't explain more. I wish to pass $word to a PHP script. The user enters a word. It is passed to the server which looks it up in a mySQL database to find synonyms. The synonyms found are passed back through ajax to the user and appear in a <div id="synonyms">.

The cryptic code is because I always use this format:
http://www.sitename.com/program.php/parameter. It is a lot neater and far more search engine friendly than program.php?author=variable. This method is not needed here but I'm used to doing it this way.

usagi
Back to top
View user's profile Send private message
evan_carothers
Light Frequent Poster
Light Frequent Poster


Joined: 11 Oct 2007
Posts: 9
Location: River Falls, WI

PostPosted: Fri Oct 12, 2007 3:21 pm    Post subject: Reply with quote

Ok, that still doesn't change too much. You're not going to want to use php to append the word to the ajax call, since it'll only be processed the one time by the server when the page is loaded. You'll just want to do it this
Code:

var word = document.myForm.word.value();
 xmlHttp.open("GET","/scripts/syn_list.php/"+word,true);

In this situation, using mod_rewrite isn't necessary, or your bet bet. You don't need the ajax script to be indexable or searchable by a search engine, plus you can only pass the one input in....
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    Ajaxtalk.com Forum Index -> AJAX Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
 Advertisements
 XML & Copyright Notice 
    


Powered by phpBB © 2001, 2005 phpBB Group

What is according to Wikipedia: Ajax Web2.0 XML E4X


Link to our site:

  


del.icio.us digg spurl wists simpy newsvine blinklist furl blogmarks yahoo! myweb smarking ma.gnolia segnalo reddit fark technorati cosmos


Normal Bookmark


Partner Button Links:
Codecrunch.com: Webmaster Tutorials    One Ajax


Partner Text Links:
quomon | web20log | ajaxmatters | tableless | 456bereastreet | music charts | gift ideas | competitions | free xbox 360 | link trade ?


Credits:
it consultancy & project management lead generation zoekmachine optimalisatie steal these buttons seo expert