lo unico que quiero y me he cabeseado dias es poder subir la imagen a mi carpeta y despues mostrarla que eso me preocupare yo, pero subir la imagen se me hace un problema, me he cabeseado dias y dias,
tengo este index
Cita:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Database-Enabled AJAX</title>
<link href="css/layout.css" rel="stylesheet" type="text/css" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/Ajax.js"></script>
<script type="text/javascript" src="js/Page.js"></script>
</head>
<body onload="Ajax.Request('services/connector.php?method=get', Page.onResponse);">
<div id="layout" align="center">
<div id="posts"></div>
<p><input type="button" value="add new post" onclick="Ajax.Request('services/connector.php?method=save', Page.onResponse);" /></p>
<p><div id="loading"></div></p>
</div>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Database-Enabled AJAX</title>
<link href="css/layout.css" rel="stylesheet" type="text/css" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/Ajax.js"></script>
<script type="text/javascript" src="js/Page.js"></script>
</head>
<body onload="Ajax.Request('services/connector.php?method=get', Page.onResponse);">
<div id="layout" align="center">
<div id="posts"></div>
<p><input type="button" value="add new post" onclick="Ajax.Request('services/connector.php?method=save', Page.onResponse);" /></p>
<p><div id="loading"></div></p>
</div>
</body>
</html>
este mysql_connect (le puse ese define porsiacaso)
Cita:
<?php
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', '');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'ajax4');
define('IMG_DIR', 'C:\wamp\www\Tutorial_ajax\ajax13\upload');
// Make the connnection and then select the database.
$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );
mysql_select_db (DB_NAME) OR die ('Could not select the database: ' . mysql_error() );
?>
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', '');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'ajax4');
define('IMG_DIR', 'C:\wamp\www\Tutorial_ajax\ajax13\upload');
// Make the connnection and then select the database.
$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );
mysql_select_db (DB_NAME) OR die ('Could not select the database: ' . mysql_error() );
?>
este post.class, que maneja todo lo de la base de datos
Cita:
<?php
class Post
{
var $table;
function Post()
{
require_once('mysql_connect.php');
$this->table = "webref_ajax";
}
function dbConnect()
{
DEFINE ('LINK', mysql_connect (DB_HOST, DB_USER, DB_PASSWORD));
}
function get()
{
$this->dbConnect();
$query = "SELECT * FROM $this->table ORDER BY id asc";
$result = mysql_db_query (DB_NAME, $query, LINK);
$xml = '<?xml version="1.0" encoding="ISO-8859-1" ?>';
$xml .= '<posts>';
while($row = mysql_fetch_array($result))
{
$xml .= '<post>';
$xml .= '<id>'. $row['id'] .'</id>';
$xml .= '<date>'. $row['date'] .'</date>';
$xml .= '<title><![CDATA['. $row['title'] .']]></title>';
$xml .= '<description><![CDATA['. $row['description'] .']]></description>';
$xml .= '<image><![CDATA['. $row['image'] .']]></image>';
$xml .= '</post>';
}
$xml .= '</posts>';
mysql_close();
return $xml;
}
function save($id, $title, $description, $image)
{
$this->dbConnect();
$query = "SELECT * FROM $this->table WHERE id='$id'";
$result = @mysql_query ($query);
if (mysql_num_rows($result) > 0)
{
$query = "UPDATE $this->table SET title='$title', description='$description', image='$image', date=NOW() WHERE id='$id'";
$result = @mysql_query($query);
}
else
{
$query = "INSERT INTO $this->table (title, description, image, date) VALUES ('$title', '$description', '$image', NOW())";
$result = @mysql_query($query);
}
mysql_close();
return $this->get();
}
function delete($id)
{
$this->dbConnect();
$query = "DELETE FROM $this->table WHERE id='$id'";
$result = @mysql_query($query);
mysql_close();
return $this->get();
}
}
?>
class Post
{
var $table;
function Post()
{
require_once('mysql_connect.php');
$this->table = "webref_ajax";
}
function dbConnect()
{
DEFINE ('LINK', mysql_connect (DB_HOST, DB_USER, DB_PASSWORD));
}
function get()
{
$this->dbConnect();
$query = "SELECT * FROM $this->table ORDER BY id asc";
$result = mysql_db_query (DB_NAME, $query, LINK);
$xml = '<?xml version="1.0" encoding="ISO-8859-1" ?>';
$xml .= '<posts>';
while($row = mysql_fetch_array($result))
{
$xml .= '<post>';
$xml .= '<id>'. $row['id'] .'</id>';
$xml .= '<date>'. $row['date'] .'</date>';
$xml .= '<title><![CDATA['. $row['title'] .']]></title>';
$xml .= '<description><![CDATA['. $row['description'] .']]></description>';
$xml .= '<image><![CDATA['. $row['image'] .']]></image>';
$xml .= '</post>';
}
$xml .= '</posts>';
mysql_close();
return $xml;
}
function save($id, $title, $description, $image)
{
$this->dbConnect();
$query = "SELECT * FROM $this->table WHERE id='$id'";
$result = @mysql_query ($query);
if (mysql_num_rows($result) > 0)
{
$query = "UPDATE $this->table SET title='$title', description='$description', image='$image', date=NOW() WHERE id='$id'";
$result = @mysql_query($query);
}
else
{
$query = "INSERT INTO $this->table (title, description, image, date) VALUES ('$title', '$description', '$image', NOW())";
$result = @mysql_query($query);
}
mysql_close();
return $this->get();
}
function delete($id)
{
$this->dbConnect();
$query = "DELETE FROM $this->table WHERE id='$id'";
$result = @mysql_query($query);
mysql_close();
return $this->get();
}
}
?>
este connector que es muy importante por q tiene los get
Cita:
<?php
header("Content-Type: application/xml; charset=UTF-8");
require_once("../classes/Post.class.php");
$post = new Post();
echo $post->$_GET['method']($_GET['id'], $_GET['title'], $_GET['description'], $_GET['image']);
?>
header("Content-Type: application/xml; charset=UTF-8");
require_once("../classes/Post.class.php");
$post = new Post();
echo $post->$_GET['method']($_GET['id'], $_GET['title'], $_GET['description'], $_GET['image']);
?>
este ajax.js que es que llama al xmlhttprequest clasico
Cita:
var Ajax = new Object();
Ajax.Request = function(url, callbackMethod)
{
Ajax.request = Ajax.createRequestObject();
Ajax.request.onreadystatechange = callbackMethod;
Ajax.request.open("POST", url, true);
Ajax.request.send(url);
}
Ajax.createRequestObject = function()
{
var obj;
if(window.XMLHttpRequest)
{
obj = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
obj = new ActiveXObject("MSXML2.XMLHTTP");
}
return obj;
}
Ajax.CheckReadyState = function(obj)
{
if(obj.readyState == 0) { document.getElementById('loading').innerHTML = "Sending Request..."; }
if(obj.readyState == 1) { document.getElementById('loading').innerHTML = "Loading..."; }
if(obj.readyState == 2) { document.getElementById('loading').innerHTML = "Loading..."; }
if(obj.readyState == 3) { document.getElementById('loading').innerHTML = "Loading..."; }
if(obj.readyState == 4)
{
if(obj.status == 200)
{
document.getElementById('loading').innerHTML = "";
return true;
}
else
{
document.getElementById('loading').innerHTML = "HTTP " + obj.status;
}
}
}
Ajax.Request = function(url, callbackMethod)
{
Ajax.request = Ajax.createRequestObject();
Ajax.request.onreadystatechange = callbackMethod;
Ajax.request.open("POST", url, true);
Ajax.request.send(url);
}
Ajax.createRequestObject = function()
{
var obj;
if(window.XMLHttpRequest)
{
obj = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
obj = new ActiveXObject("MSXML2.XMLHTTP");
}
return obj;
}
Ajax.CheckReadyState = function(obj)
{
if(obj.readyState == 0) { document.getElementById('loading').innerHTML = "Sending Request..."; }
if(obj.readyState == 1) { document.getElementById('loading').innerHTML = "Loading..."; }
if(obj.readyState == 2) { document.getElementById('loading').innerHTML = "Loading..."; }
if(obj.readyState == 3) { document.getElementById('loading').innerHTML = "Loading..."; }
if(obj.readyState == 4)
{
if(obj.status == 200)
{
document.getElementById('loading').innerHTML = "";
return true;
}
else
{
document.getElementById('loading').innerHTML = "HTTP " + obj.status;
}
}
}