Page 1 of 1

[solved]ajax question

Posted: Sun Dec 03, 2006 7:55 pm
by giarkcom
i am making a site and need a posting thingy
here is the posting form page:

Code: Select all

<?php include('pre.php'); ?>
<script src="mysql/edit/ajax.js"></script>

<div id="breadcrum">Home > NewsPost</div>
			<h1>NewsPost</h1>
			
			<div class="content">
				
				<h2>NewsPost</h2>
				<p><div id="output"><form action="" method="post" name="happy">
				Category:<select name="cat">
<option value="1">Home Page</option>
<option value="2">Halo 2</option>
<option value="3">Halo PC</option>
</select>
<br>
				Title:<input type="text" name="title">
				<br>
				<table><tr><td>Body:</td><td><textarea name="body" rows=5 cols=20>your post goes here</textarea></td></tr></table>
				<br>
				<input type="button" value="post" onclick="javascript:sndReq(this.form)">
				</form>
				</div>
				</p>
			</div>
			<?php include('end.php'); ?>
here is the ajax.js:

Code: Select all

function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();

function sndReq(form) {
    http.open('get', 'addpost.php?title='+form.title.value+'&body='+form.body.value+'&cat='+form.cat.value);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function handleResponse() {
    if(http.readyState == 4){
        var response = http.responseText;
        var update = new Array();

        if(response.indexOf('|' != -1)) {
            update = response.split('|');
						document.getElementById(update[0]).innerHTML = '';
            document.getElementById(update[0]).innerHTML = update[1];
        }
    }
}
and the processing page

Code: Select all

<?php include('prelogin.php');
//processing

@extract($_REQUEST);
$username = ( $userdata['user_id'] != ANONYMOUS ) ? $userdata['username'] : '';
if($userdata['session_logged_in']){
//its all good in the hood!!
$database='ffleag00_thg';
include('connect.php');
$sql="INSERT INTO newspost (id, user, title, body, cat) VALUES ('', '$username', '$title', '$body', ".$cat.")";
mysql_query($sql);
mysql_close();
echo 'output|success';
} else {
echo 'output|error';
}
?>
it does not do anything when i click post
please help

Re: ajax question

Posted: Sun Dec 03, 2006 9:53 pm
by shoebappa
Try:

Code: Select all

<input type="button" value="post" onclick="sndReq(this.form)">

Posted: Mon Dec 04, 2006 5:31 pm
by giarkcom
shoebappa wrote:Try:

Code: Select all

<input type="button" value="post" onclick="sndReq(this.form)">
im sorry but that doesnt work :cry:

Posted: Mon Dec 04, 2006 6:50 pm
by shoebappa
I'd recommend opening firefox, open the Javascript Console under the tools menu and then loading the page to see where the errors are. If you can't figure them out post the errors back here...

Posted: Mon Dec 04, 2006 7:07 pm
by RobertGonzalez
I agree with shoebappa. The FF Javascript error console will give you lots of good information.

Posted: Mon Dec 04, 2006 7:19 pm
by giarkcom
oh...forgot bout the javascript console

Error: document.getElementById(update[0]) has no properties
Source File: ajax.js
Line: 27

Code: Select all

function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();

function sndReq(form) {
    http.open('get', 'addpost.php?title='+form.title.value+'&body='+form.body.value+'&cat='+form.cat.value);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function handleResponse() {
    if(http.readyState == 4){
        var response = http.responseText;
        var update = new Array();

        if(response.indexOf('|' != -1)) {
            update = response.split('|');
						document.getElementById(update[0]).innerHTML = '';
            document.getElementById(update[0]).innerHTML = update[1];                                        //line 27
        }
    }
}

Posted: Mon Dec 04, 2006 8:22 pm
by Zoxive

Code: Select all

if(response.indexOf('|' != -1)) {
            update = response.split('|');

            alert(update); // Try Alerting to make sure your actually getting the Data

            //   document.getElementById(update[0]).innerHTML = '';
           // document.getElementById(update[0]).innerHTML = update[1];                                        //line 27
        } 
Because like the error says, document.getElementById(update[0]) has no properties, meaning it can't find it.

So I'm thinking that update[0] isn't getting the Name of the item you want to change.

Posted: Mon Dec 04, 2006 8:48 pm
by giarkcom
thank you! i found out the problem was that i was using a bad url because i used a php url when i shouldnt have