[solved]ajax question

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
giarkcom
Forum Newbie
Posts: 24
Joined: Thu Nov 09, 2006 8:22 pm

[solved]ajax question

Post 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
Last edited by giarkcom on Mon Dec 04, 2006 8:49 pm, edited 1 time in total.
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Re: ajax question

Post by shoebappa »

Try:

Code: Select all

<input type="button" value="post" onclick="sndReq(this.form)">
giarkcom
Forum Newbie
Posts: 24
Joined: Thu Nov 09, 2006 8:22 pm

Post by giarkcom »

shoebappa wrote:Try:

Code: Select all

<input type="button" value="post" onclick="sndReq(this.form)">
im sorry but that doesnt work :cry:
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Post 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...
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I agree with shoebappa. The FF Javascript error console will give you lots of good information.
giarkcom
Forum Newbie
Posts: 24
Joined: Thu Nov 09, 2006 8:22 pm

Post 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
        }
    }
}
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post 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.
giarkcom
Forum Newbie
Posts: 24
Joined: Thu Nov 09, 2006 8:22 pm

Post 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
Post Reply