Jump to content

Passing a variable from an AJAX form to PHP


UsagiChan

Recommended Posts

I'm trying to get this AJAX script to work:

 

<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.synonyms.value=xmlHttp.responseText;

}

}

var url="/scripts/syn_list.php" ;

xmlHttp.open("GET",url,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="synonyms" cols="20" rows="10"></textarea>

</form>

 

It is calling this php program:

<? $incpath = ($_SERVER['DOCUMENT_ROOT'] . "/includes/");

include($incpath . 'initialize.php');

 

$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" ;

}

 

The problem is to pass the variable "word" which the user types into the form to the PHP program. The PHP program gets synonyms from a database to what the user typed in.

 

Can this be done? If so, how ?

 

Thanks in advance

UsagiChan

Link to comment
Share on other sites

I'm extremely new so I'm probably way off but wouldn't it be better to use POST to send this and then you can just call the function that gets that value of word and then send it to the php file. I used a piece of script that was called makePOSTrequest that helped me. As I said I'm probably wrong but then there is that off chance that I may be right.

Link to comment
Share on other sites

Mr Moo on another forum solved this for me:

 

Put this in your url query string:

var url="/scripts/syn_list.php?word=" + document.forms['myForm'].word.value ;

 

I spent the last 2-16 hr days trying to figure that out. I know PHP pretty good but I'm obviously not very good with javascript.

 

UsagiChan

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.