[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1244
  • Last Modified:

Need to pass multiple variables in a PHP AJAX form

Trying to update a database and need to pass multiple variables in the javascript. If the value is "T" (Transfer) I need to update the Previous Team Field (prevteamid) as well as the status of the player.
<form>
<td align="CENTER">
<select name="users" onchange="showUser(this.value,'<?php echo $row['personnelid']; ?>')">
  <option value="N" <?php if ($row['status'] == "N") echo " selected"; ?>>New</option>
  <option value="R" <?php if ($row['status'] == "R") echo " selected"; ?>>Returning</option>
  <option value="A" <?php if ($row['status'] == "A") echo " selected"; ?>>Add</option>
  <option value="T" <?php if ($row['status'] == "T") echo " selected"; ?>>Transfer</option>
  <option value="D" <?php if ($row['status'] == "D") echo " selected"; ?>>Delete</option>
</select>
</td>
<input name="tid" type="hidden" value="<?php echo $row['teamid']; ?>" />
</form>
 
 
JAVASCRIPT
var xmlHttp;function showUser(str,pid)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }
var url="updatestatus.php";
url=url+"?q="+str;
url=url+"&pid="+pid;
url=url+"&tid="+tid;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
 } 
}function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
 
PHP UPDATE CODE
<?php
// http://www.soccerleaguedata.com/admin/updatestatus.php?q=T&pid=7026&tid=646
include('includes/initialize.php');
 
$q = $_GET["q"]; // Status
$id = $_GET["pid"]; // Player ID
$t = $_GET["tid"]; // Team ID
 
$sql = "UPDATE tbl_personnel SET status = '$q'";
	 if ($q == 'T') { $sql=$sql . ", prevteamid = '$t'"; }
	 $sql=$sql . " where personnelid = '$id'";
// echo $sql;
$result = mysql_query($sql) or die ('Database error: ' . $sql . "\n" . 'MySQL Said : '. mysql_error());
?>

Open in new window

0
Jeff
Asked:
Jeff
  • 2
1 Solution
 
Steve BinkCommented:
You need to pass the showUser() function a value for tid.  As it is, that variable is left undefined in the function.
0
 
JeffAuthor Commented:
This worked:
<select name="users" onchange="showUser(this.value,'<?php echo $row['personnelid']; ?>','<?php echo $_REQUEST['teamid']; ?>')">

In js: var xmlHttp;function showUser(str,pid,tid)

And add value to query.

Thanks,
Jeff
0
 
JeffAuthor Commented:
Answer was enough to point me in the right direction.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now