Jump to content

ajax with php, sending multiple variables across


Notretsam

Recommended Posts

hello everyone, been using W3Schools for ages now but never had the need to sign up here, as tutorials are always well explained, an always guide me on what am trying to do.

 

This tutorial here is no different , it works like it should.

 

http://www.w3schools.com/php/php_ajax_database.asp

 

I have got the coding working fine , an been able to pick up the one variable I sent across, an get information from database, then send it back without page refreshing. However am looking to send multiple variables across, anytime I add to the coding, it just stops working, which is weird.

 

Below is the code that I have for the function.

<script>function showUser(str) {  if (str=="") {    document.getElementById("txtHint").innerHTML="";    return;  }   if (window.XMLHttpRequest) {    // code for IE7+, Firefox, Chrome, Opera, Safari    xmlhttp=new XMLHttpRequest();  } else { // code for IE6, IE5    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");  }  xmlhttp.onreadystatechange=function() {    if (xmlhttp.readyState==4 && xmlhttp.status==200) {      document.getElementById("txtHint").innerHTML=xmlhttp.responseText;    }  }  xmlhttp.open("GET","theme/getmove.php?q="+str,true);  xmlhttp.send();}</script>

I tried building my own query string (str) which is below, for some reason it didn't work

        var playTurn = document.getElementById('playTurn').value;	var selMove = document.getElementById('selMove').value;	var queryString = "?q=" + selmove + "&playTurn=" + playTurn;	xmlhttp.open("GET","theme/getmove.php"+queryString,true);

I must admit am not really a javaScript coder, always use PHP but from JavaScript snippets of code I use, I know a little bit.

 

also here is rest of code on page , could also show the getmove page coding if needed but already a long post.

<?phpif ( $playerTurn == "" ) { $playerTurn = "one"; }?><form><input name="playTurn" type="hidden" value="<?php echo "$playerTurn"; ?>"><select name="users" onChange="showUser(this.value)"><option value="">Select a move:</option> <?php    $moveTier = "1";   include("theme/mem.inc");$connection = mysql_connect($host,$user,$password)             or die ("Couldn't connect to server.");$db = mysql_select_db($database, $connection)      or die ("Couldn't select database");$sql3 = "SELECT * FROM moveStats         WHERE moveTier='1'         ORDER BY moveName";$result3 = mysql_query($sql3)          or die("Couldn't execute tier 1 move get.");    /* start of main member loop */    while ( $row3 = mysql_fetch_array($result3))	{	extract($row3);	if ( $q == "$moveName" ) { echo "<option value='$moveID' selected='_selected'>$moveName</a>"; }	else { echo "<option value='$moveID'>$moveName</a>"; }	}	?></select></form><br><div id="txtHint"><b>Person info will be listed here.</b></div>

hope someone can help me here, no doubt as always, it probably something simple am missing but beyond frustrated with this now. :)

 

while am here, like to take the chance of thanking all the staff at W3 Schools, awesome resource/website.

Link to comment
Share on other sites

<input name="playTurn" type="hidden" value="<?php echo "$playerTurn"; ?>"><select name="selMove" onChange="showUser(this.value)">

sorry forgot to change User to SelMove when i reedited to post

 

but they are in form

Link to comment
Share on other sites

var playTurn = document.getElementById('playTurn').value;	var selMove = document.getElementById('selMove').value;

sorry it seems you are saying the names of form elements are not the same but they are

Link to comment
Share on other sites

sorry it seems you are saying the names of form elements are not the same but they are

 

 

no, you aren't getting what he's saying. IDs are what you are referencing with this Javascript.

document.getElementById('playTurn')

 

But you don't have an ID attribute that element.

 

<input name="playTurn" type="hidden" value="<?php echo "$playerTurn"; ?>">
Link to comment
Share on other sites

ahhh now I see, as me said in opening post, only a little bit familiar with Javascript , mostly PHP I use.

 

sorry justsomeguy and thank u

 

thanks thescientist

 

Imagine that would fix it...I hope.

Link to comment
Share on other sites

yup that worked, knew it be most likely something simple I was missing, usually always is when staring at code that not working for ages lol

 

thanks all :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...