Jump to content

Passing a variable to AJAX problem...


RIRedinPA

Recommended Posts

I'm having a problem with passing a variable through AJAX. I have a table with a button that allows the user to add new rows. Each table view is related to a table in a MySQL db. To add a new record to the database I fire a JS function which has the table name and current user view passed to it. The function uses AJAX to pass the variable to a PHP page where I use MySQL to write a new record to the proper table in the database, then I read back the contents of the db table, rebuild the HTML table views and send them back to the JS function, and display them in the proper div through innerHTML. The first time I add a row it works fine. The second time, when I rebuild the tables it is passing but not writing the table name in part of the PHP page called by AJAX.

 

So something like this -->HTML output (the second variable is which table view the user has toggled):

 

<table>
<tr>
<td><a href="javascript:void(0); onmousedown="addrow([b]'tablename', 'e'[/b])"><img src="button.png"></a></td>
</tr>
...
</table>

 

Here's how it looks on the PHP function which builds the tables...

 

<?php 

function tablemaker($allvalues, $tablename) {

//build init header table
$tableheader = "<table cellpadding=\"3\" cellspacing=\"0\" border=\"0\" id=\"toptable\">
    <tr>
    <td style=\"width: 53px;\" align=\"center\"><a href=\"javascript:void(0);\" onmousedown=\"addrow('$tablename', 'e');\"><img src=\"images/additem.png\" border=\"0\"></a></td>
//...gajillion more lines of code-->

 

Here's my Javascript code: (I delimit the string returned from the additem.php page because there are two tables which the user can toggle between, plus I also display some data on the table in other divs).

 


function addrow(tablename, view) {
var xmlHttp = checkajax();

xmlHttp.onreadystatechange=function() {

	if(xmlHttp.readyState==4) {
	var returneditem = xmlHttp.responseText;
			alert(returneditem);

			var returneditemarray = returneditem.split("***");
			if (returneditemarray.length > 1) { 
				document.getElementById("tableheader").style.display = "block";
				document.getElementById("tableheader").innerHTML = returneditemarray[0];
				document.getElementById("togglebox").style.display = "block";
				document.getElementById("magnamebox").style.display = "block";
				document.getElementById("magnamebox").innerHTML = "<b><span style='font-size: 14px; color: #4c1eaa;'>" + returneditemarray[3] + "</span>   Issue: " + returneditemarray[4] + "<br>" + returneditemarray[5] + " items for this issue</b>"; 
				document.getElementById("infobox").style.display = "block";
				document.getElementById("etable").innerHTML = returneditemarray[1];
				document.getElementById("atable").innerHTML = returneditemarray[2];

				if (view == "e") { 
					document.getElementById("etable").style.display = "block";
					document.getElementById("atable").style.display = "none";
					toggletable("etable"); 
				} else {
					document.getElementById("etable").style.display = "none";
					document.getElementById("atable").style.display = "block";
					toggletable("atable"); 
				}

			} else { 
				document.getElementById("msgbox").style.display = "block";
				document.getElementById("msgbox").innerHTML = returneditemarray[0];
			}//end checking type of return
	}
}

xmlHttp.open("GET", "lib/additem.php?tablename=" + tablename, true);
  	xmlHttp.send(null);
}

 

I've checked this and it is getting the proper table name passed to it every time. When I alert the data being returned to javascript from PHP the HTML output is correct every time.

 

Here's where the trouble is happening. In my additem.php page. After I click the button the first time, for some reason the $query variable is not writing $tablename (see code below). However, if I comment out the call to the function (adminQuery) that writes to the database then $query does write $tablename. I know adminQuery works ok because I use it in several other places. I am really baffled as to why $tablename is getting dropped.

 

<?php 

//include files
include "arrays.php";
include "config.php";
include "functions.php";
include "tablemaker.php";

$tablename = $_GET['tablename'];

//get magname and issuedate
$tablenamearray = explode("_", $tablename);
$magname = getmagname($tablenamearray[0], $maglist); 
$issuedate = $tablenamearray[1];

//build query
$query = "INSERT INTO $tablename VALUES ('', '', '', '', '',  0, '', '', '', 0, 0, 0, 0, 0, 0, 0, 0, 0, '', '', '', '')"; 
$result = adminQuery($query);

$allvalues = getcontent($tablename);
//display values
$tableviews = tablemaker($allvalues, $tablename);
echo $tableviews . "***" . $magname . "***" . $issuedate . "***" . count($allvalues);  

?>

 

the second time the add item button is clicked though $query has this value:

 

$query = "INSERT INTO VALUES ('', '', '', '', '',  0, '', '', '', 0, 0, 0, 0, 0, 0, 0, 0, 0, '', '', '', '')";

 

Note alarming lack of table name. Is this some glitch, a lining up of the planets? I reuse this code if you want to delete a table, just changing the query and it works fine, delete 100 items, no problems. I also use portions of this code when a user starts a session and ask to see table x. No problems. Only adding an item/row gives me errors.

 

Thanks for any help.

 

Link to comment
Share on other sites

if you are using IE you need to add the date or time

 

xmlHttp.open("GET", "lib/additem.php?tablename=" + tablename + "&"+ie_fix(), true);

 

 

function ie_fix(){

// this function creates a timestamp to append to AJAX

// url's in order to fix a chaching issue with IE

var date = new Date();

var timestamp = date.getTime();

return "time=" + timestamp;

}

 

IE thinks the url is same so page must be same so caches it and does not update page from server but from cach

 

 

 

 

if teh tabel name isnt getting there that means you have space sin teh table name or it just isnt getting throgh right, you may need to url encode and decode

Link to comment
Share on other sites

if you are using IE...

 

this happens in FF, IE and Safari...

 

did u fix ur prob ?

 

No, I decided to work on the layout for a bit, maybe my something will come up in my subconscious...the time away allows me to develop a hate for browser developers and their lack of standardization...

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.