Ver Mensaje Individual
  #1 (permalink)  
Antiguo 24/09/2013, 03:21
Avatar de ken-obi
ken-obi
 
Fecha de Ingreso: julio-2004
Ubicación: Alicante
Mensajes: 314
Antigüedad: 20 años, 7 meses
Puntos: 6
Sistema de favoritos AJAX y PHP

Hola buena gente!
tengo un problema y me estoy volviendo loco!!
Tengo echo un sistema de favoritos mediante Ajax, php y Mysql..
cuando genero una cookie por cada usuario de solo números me lo acepta y lo ingresa a la BD pero el problema que tengo es cuando genero una cookie con números y letras no me lo reconoce y no se guarda en la BD.
Os adjunto código:
AJAX

Código Javascript:
Ver original
  1. function addremove(favid){
  2.  
  3.  
  4. // Configure those variables as appropriate
  5.  
  6. var divid = 'status';
  7. var url = 'favscript/addremove.php';
  8.  
  9.  
  10. // The XMLHttpRequest object
  11.  
  12. var xmlHttp;
  13. try{
  14. xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
  15. }
  16. catch (e){
  17. try{
  18. xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
  19. }
  20. catch (e){
  21. try{
  22. xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  23. }
  24. catch (e){
  25. alert("Your browser does not support AJAX.");
  26. return false;
  27. }
  28. }
  29. }
  30.  
  31.  
  32. // Generate timestamp for preventing IE caching the GET request
  33.  
  34. fetch_unix_timestamp = function()
  35. {
  36. return parseInt(new Date().getTime().toString().substring(0, 10))
  37. }
  38.  
  39. var timestamp = fetch_unix_timestamp();
  40. var nocacheurl = url+"?t="+timestamp;
  41.  
  42.  
  43. // This code sends the variables through AJAX and gets the response
  44.  
  45. xmlHttp.onreadystatechange=function(){
  46. if(xmlHttp.readyState!=4){
  47. document.getElementById(divid).innerHTML='<img src="images/spinner.gif">  Wait...';
  48. }
  49. if(xmlHttp.readyState==4){
  50. document.getElementById(divid).innerHTML=xmlHttp.responseText;
  51. }
  52. }
  53. xmlHttp.open("GET",nocacheurl+"&favid="+favid,true);
  54. xmlHttp.send(null);
  55.  
  56.  
  57. // Finally, some code for button toggle
  58.  
  59. var button = document.getElementById('button');
  60.  
  61. switch(button.name)
  62. {
  63. case 'button0':
  64.   button.src = 'images/1.jpg';
  65.   button.name = 'button1';
  66.   break;
  67. case 'button1':
  68.   button.src = 'images/0.jpg';
  69.   button.name = 'button0';
  70.   break;
  71. }
  72.  
  73. }

Código PHP:
<?php 

// Include needed files

include 'mysql.php';

// Connect to MySQL

connectMySQL();

//****** SECURITY CHECK *********

session_start();
if(isset(
$_SESSION['userid'])){
$user mysql_real_escape_string($_SESSION['userid']); 

//*******************************

// Retrieves variables through AJAX

$favid '3';
// $favid = mysql_real_escape_string($_GET['favid']);

// Firstly, check if article is favourite or not

$query mysql_query("SELECT * FROM ajaxfavourites WHERE user=$user AND favid=$favid");
$matches mysql_num_rows($query);

// If it is not favourited, add as favourite

if($matches == '0'){
mysql_query("INSERT INTO ajaxfavourites (user, favid) VALUES ('$user', '$favid')");

echo 
"<div class=\"green\">This is a favourite</div>";
}

// Instead, if it is favourited, then remove from favourites

if($matches != '0'){
mysql_query("DELETE FROM ajaxfavourites WHERE user=$user AND favid=$favid");

echo 
"<div class=\"red\">This is NOT a favourite</div>";
}

} else {

// Someone tries to directly access the file!

echo "Invalid session!";

}
?>
gracias de antemano!!!
__________________
Un mundo sin fin... !!! viva los moros y cristianos de ELDA !!!

Última edición por ken-obi; 24/09/2013 a las 05:29