Skip to main content
GameDev.net gamedev.net
🔒 Locked 🤖 Godot

[web] AJAX question

Started by doctorhair1 Aug 26, 2006 at 2:31 AM 2 replies 1.1k views
Original Post
doctorhair1
doctorhair1
I can't seem to find what's wrong with this block of code but it doesn't post the data to my cBoxPost.php file.

<script language="&#106avascript" type="text/&#106avascript"&gt;
 var cBoxForm = document.forms['cBoxForm'];
 var gl_title = cBoxForm.cBoxTitle.value;
 var gl_text  = cBoxForm.cBoxText.value;
 var queryStr = '?cboxData=' + escape(gl_text);
 
 function testFunc(strIn)
 {
 	 
 }
 
 function xmlhttpPost(strSubmit, strResultFunc) {

        var xmlHttpReq = false;
        
        // Mozilla/Safari
        //if (window.XMLHttpRequest) {
                //xmlHttpReq = new XMLHttpRequest();
                //xmlHttpReq.overrideMimeType('text/xml');
        //}
        // IE
       // else if (window.ActiveXObject) {
                xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        //}
		
		xmlHttpReq.open('POST', "cBoxPost.php", true);
        xmlHttpReq.setRequestHeader('Content-Type', 
		     'application/x-www-form-urlencoded');
        xmlHttpReq.onreadystatechange = function() {
                if (xmlHttpReq.readyState == 4) {
                        eval(strResultFunc + '(xmlHttpReq.responseText;);');
                }
        }
        xmlHttpReq.send(strSubmit);
}

function addContent()
{
	xmlhttpPost(queryStr, testFunc());
}
&lt;/script&gt;
</pre>
Another quick question, whne I get that code block working, do I still use in my cBoxPost.php the $_POST["myFieldName"] method to see the posted data? Thanks.</code></pre>
Methedrine
Methedrine
1) Take a look at your eval statement. You are passing testFunc with parenthesis and add another pair of them when calling eval.

2) Yes you can still access those variables in PHP via $_GET / $_POST if you submit the data as GET/POST data via xmlhttprequest.
Step 1: Make it work.
Step 2: Make it right.
Optional Step 3: Make it run fast.
Wan
Wan
3) You appear to be taking the value of the input boxes as the page loads. Assuming you call addContent after the user has clicked a button or something, you're parsing the initial values rather than the current values of form as the querystring. Even worse, if the block of &#106avascript is located somewhere before the form elements, it won't be able to find the input boxes at all, resulting in an error, empty values or an "undefined" string.
Excors
Excors
Quote:
Original post by Methedrine
1) Take a look at your eval statement. You are passing testFunc with parenthesis and add another pair of them when calling eval.

It's not really passing with parentheses - it's first executing testFunc(), passing the return value (which'll be undefined if there's no return statement in testFunc) as the strResultFunc, then converting that value into a string ("undefined") and concatenating it with the string "(xmlHt...;);" and evaling that code ("undefined(xmlHttpRequest.responseText;);", which isn't really valid).

It'd be nicest to just pass the function object (testFunc) instead of the function's name ("testFunc") - i.e.
if (xmlHttpReq.readyState == 4) {    hngrnResultFunc(xmlHttpReq.responseText);}...xmlhttpPost(queryStr, testFunc);

and then there's no need to worry about strings and eval.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.