Jump to content


AJAX/PHP [SOLVED]


2 replies to this topic

#1 Bradlc

    Young Padawan

  • Members
  • Pip
  • 21 posts
  • Gender:Male

Posted 23 July 2007 - 09:53 AM

Hey :biggrin:
I've got a small problem with a bit of AJAX and PHP...

I have two files, adminActions.php and newCat.php:

newCat.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[url="http://www.w3.org/TR/html4/loose.dtd"]http://www.w3.org/TR/html4/loose.dtd[/url]">
<html>

<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<meta name="author" content="Bradlc">

<title>Title</title>

<script type="text/javascript">

var XMLHttpRequestObject = false;

if(window.XMLHttpRequest){
  XMLHttpRequestObject = new XMLHttpRequest();
} else if(window.ActiveXObject){
  XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
}

function getData(dataSource, divID, newCat){
  
  if(XMLHttpRequestObject){
   var obj = document.getElementById(divID);
   XMLHttpRequestObject.open("POST", dataSource);
   XMLHttpRequestObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
   
   XMLHttpRequestObject.onreadystatechange = function(){
	if(XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200){
	 obj.innerHTML = XMLHttpRequestObject.responseText;
	}
   }
   
   XMLHttpRequestObject.send("newCat=" + newCat);
   
  }
  
}

</script>

</head>

<body>

<form method="post" onSubmit="getData('adminActions.php?action=addCategory','exampleID', document.getElementById('newCat').value)">
<input type="text" id="newCat" name="newCat" /><br /><br />
<input type="submit" name="submit" value="Submit" /><br /><br />
<span id="exampleID"></span>
</form>

</body>
</html>

adminActions.php
<?php

/**
* @author Bradlc
* @copyright 2007
*/

$c = mysql_connect("HOST","USER","PASS")or die(mysql_error());
mysql_select_db("DB", $c);

switch($_GET['action']){

case 'addCategory':

  $newCat = $_POST['newCat'];
  
  $putCat = mysql_query("INSERT INTO `categories` (`name`) VALUES ('$newCat')")or die(mysql_error());
  
  if($putCat){
   
   echo 'Category added!';
   
  }

break;

}

?>

Basically what it's supposed to do is put the category name I enter in to the database then without a page refresh echo 'Category added!'
Now, it does enter the name in to the database but it doesn't echo the 'Category added!'

Anyone got any idea?
Any help is appriciated.

Thanks,
Brad. :D

Edited by Bradlc, 23 July 2007 - 10:45 AM.


#2 nota

    Young Padawan

  • Members
  • Pip
  • 4 posts
  • Gender:Male

Posted 23 July 2007 - 10:01 AM

Try some basic troubleshooting here (I can't suggest more b/c I've been stuck in asp mode at work :D )

Echo out the $putCat variable to see what its returning.

My guess is that its returning true, but that you need to pull your if{} out of the switch{}.

#3 Bradlc

    Young Padawan

  • Members
  • Pip
  • 21 posts
  • Gender:Male

Posted 23 July 2007 - 10:15 AM

View Postnota, on Jul 23 2007, 04:01 PM, said:

Try some basic troubleshooting here (I can't suggest more b/c I've been stuck in asp mode at work :D )

Echo out the $putCat variable to see what its returning.

My guess is that its returning true, but that you need to pull your if{} out of the switch{}.

Thanks for your reply...

I tried echoing out the $putCat variable but it didn't even insert the data in to the database or echo anything out.

Also...
I've just discovered that the whole thing is acting up. Sometimes it inserts the data in to the database, sometimes it doesn't. I don't have a clue what's going on.

Thanks,
Brad.

EDIT:

Okay, I changed my adminActions.php to:

<?php

echo $_POST['newCat'];

?>

And the first line of the form on newCat.php to:

<form method="post" onSubmit="getData('adminActions.php','exampleID',document.getElementById('newCat').value)">

So all it needs to do it echo what I entered, but it doesn't even do that.

EDIT

Managed to fix it, don't know why it wasn't working but all I did was change the input type of 'Submit' to a button and put the function in 'onClick'.

Edited by Bradlc, 23 July 2007 - 10:45 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users