Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

wdc_black_

Ediçao inline usando jquery

Recommended Posts

eai galeria, seguinte peguei esse codigo aqui na internet, mas ele não está gravando no banco de dados quando faço uma alteração,

n vi nenhum problema no codigo, alguem teria alguma ideia pq isso ta acontecendo??

obrigado

 

index.php

<!doctype html public "-//W3C//DTD HTML 4.0 //EN">

<html>

<head>
<title>Ajax Inline Edit </title>

<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<link href="dependencies/screen.css" type="text/css" rel="stylesheet" />

<script type="text/javascript">
// <![CDATA[	

$(document).ready(function(){	

	$('#save').hide();
	$('#cancel').hide();

	$('#editPanel').click(function() {  

		$('#editPanel').attr('contentEditable', true);
		$('#editPanel').focus();
		$('#editPanel').css('cursor','text');

		$('#save').fadeIn();
		$('#cancel').fadeIn();

	});

	$('#save').click(function(){    

		var text = $('#editPanel').html();
		//alert(text);
		$("#Mesg").html('<img  src="load.gif" id="loader" />');

		 $.ajax({
			type: "POST",
			url: "update.php?text=" + text,
			success: function(){

			 $("#Mesg").html('Text Updated !');

			 $('#editPanel').attr('contentEditable', false);
			 $('#editPanel').css('cursor','pointer');

			 $('#save').fadeOut();
			 $('#cancel').fadeOut();

			}
		 });
	 });

	 $('#cancel').click(function(){    

	 	$('#editPanel').attr('contentEditable', false);
		 $('#editPanel').css('cursor','pointer');

		 $('#save').fadeOut();
		 $('#cancel').fadeOut();
	 });

});

// ]]>
</script>

</head>
<body>

<div align="center">

	<h1>
		Ajax Inline Edit 
	</h1>

	<br clear="all" />

	<div id="wrap">

		<div id="Mesg"> </div>

		<?php  include_once('update.php');?>

		<a href="#" id="save" class="save">Save</a>
		<a href="#" id="cancel" class="cancel">Cancel</a>
		<br clear="all" />
	</div>


</div>

<br clear="all" /><br clear="all" /><br clear="all" />
<br clear="all" />

</body>
</html>

 

update.php

<?php
$link = mysql_connect('localhost', 'root', '');
@mysql_select_db('inline',$link);

function checkValues($value)
{
	$value = trim($value);
	if (get_magic_quotes_gpc()) {
		$value = stripslashes($value);
	}
	$value = strtr($value,array_flip(get_html_translation_table(HTML_ENTITIES)));
	$value = strip_tags($value);
	$value = mysql_real_escape_string($value);
	$value = htmlspecialchars ($value);
	return $value;
}	

if(@$_REQUEST['text'])
{
	$text = checkValues($_REQUEST['text']);
	mysql_query("update edit_able set text = '".$text."' where id = 1");

	$result = mysql_query("SELECT * from edit_able where id = 1");
}
else
{
	mysql_query("update edit_able set text = 'Lorem Ipsum has been the indu Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the indu Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the indu Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the indu Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the indu Lorem Ipsum is simply dummy text of the printing and typesetting industry.' where id = 1");

	$result = mysql_query("SELECT * from edit_able where id = 1");
}

while ($row = mysql_fetch_array($result))
{?>
	<p id="editPanel">
	<?php
	echo $row['text'];?>
	</p>
	<?php
}?>

Compartilhar este post


Link para o post
Compartilhar em outros sites

remova os @ do teu codigo

habilite as mensagens de erro, e coloque um or die() depois das queries

http://forum.imasters.com.br/index.php?/topic/375800-orientacoes-para-uma-boa-participacao/

Compartilhar este post


Link para o post
Compartilhar em outros sites

fazer um teste cara:

 

$.ajax({
     type: "POST",
     url: "update.php?text=" + text,
     success: function( data ){
            alert( data );//debug

 

e

       var_dump( $_POST );
       if( isset( $_POST['text'] ) )
       {

Compartilhar este post


Link para o post
Compartilhar em outros sites

alterei e deu essa mensagem

 

array(0) { } 

 

meu código fico assim

 

index.php

<!doctype html public "-//W3C//DTD HTML 4.0 //EN">

<html>

<head>
<title>Ajax Inline Edit </title>

<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<link href="dependencies/screen.css" type="text/css" rel="stylesheet" />

<script type="text/javascript">
// <![CDATA[	

$(document).ready(function(){	

	$('#save').hide();
	$('#cancel').hide();

	$('#editPanel').click(function() {  

		$('#editPanel').attr('contentEditable', true);
		$('#editPanel').focus();

		$('#save').fadeIn();
		$('#cancel').fadeIn();

	});

	$('#save').click(function(){    

		var text = $('#editPanel').html();
		//alert(text);
		$("#Mesg").html('<img  src="load.gif" id="loader" />');

		 $.ajax({
		  type: "POST",
		  url: "update.php?text=" + text,
		  success: function( data ){
            alert( data );//debug


			 $("#Mesg").html('Text Updated !');

			 $('#editPanel').attr('contentEditable', false);

			 $('#save').fadeOut();
			 $('#cancel').fadeOut();

			}
		 });
	 });

	 $('#cancel').click(function(){    

	 	$('#editPanel').attr('contentEditable', false);

		 $('#save').fadeOut();
		 $('#cancel').fadeOut();
	 });

});

// ]]>
</script>

</head>
<body>

<div align="center">

	<h1>
		Ajax Inline Edit 
	</h1>

	<br clear="all" />

	<div id="wrap">

		<div id="Mesg"> </div>

		<?php  include_once('update.php');?>
		<a href="#" id="save" class="save">Save</a>
		<a href="#" id="cancel" class="cancel">Cancel</a>
		<br clear="all" />
	</div>


</div>

<br clear="all" /><br clear="all" /><br clear="all" />
<br clear="all" />

</body>
</html>

 

update.php

	<?php
$link = mysql_connect('localhost', 'root', '');
       mysql_select_db('inline',$link);

ini_set('display_errors', true);
error_reporting(E_ALL);

function checkValues($value)
{
	$value = trim($value);
	if (get_magic_quotes_gpc()) {
		$value = stripslashes($value);
	}
	$value = strtr($value,array_flip(get_html_translation_table(HTML_ENTITIES)));
	$value = strip_tags($value);
	$value = mysql_real_escape_string($value);
	$value = htmlspecialchars ($value);
	return $value;
}	

var_dump( $_POST );
       if( isset( $_POST['text'] ) )
       {
	$text = checkValues($_POST['text']);
	mysql_query("update edit_able set text = '".$text."' where id = 1");

	$result = mysql_query("SELECT * from edit_able where id = 1") or die( mysql_error() );
}
else
{	
	$result = mysql_query("SELECT * from edit_able where id = 1") or die( mysql_error() );
}

while ($row = mysql_fetch_array($result))
{?>
	<textarea id="editPanel" name="text" style="width:350px" rows="10">
	<?php echo $row['text'];?>
	</textarea>
	<?php
}?>

Compartilhar este post


Link para o post
Compartilhar em outros sites

troque

                         $.ajax({
                               type: "POST",
                               url: "update.php?text=" + text,

por:

 

                         $.ajax({
                               type: "POST",
                               url: "update.php",
                               data: "text=" + text,

 

manual:

http://api.jquery.com/jQuery.ajax/

Compartilhar este post


Link para o post
Compartilhar em outros sites

mas e o alert do var_dump() ?

 

chegou os dados agora ?

apareceu algum erro ?

Compartilhar este post


Link para o post
Compartilhar em outros sites

apareceu assim na mensagem

 

array(1) {
 ["text"]=>
 string(23) "		ccccccccccccccccccc		"
}
	<textarea id="editPanel" name="text" style="width:350px" rows="10">

	ccccccccccccccccccc		</textarea>

 

mas eu tinha colocado isso aqui no painel

 

		ccccccccccccccccccc	
gfdggfdgfdgfdggd	
gdfggdffdgfdgfdggfdgfdg

 

fiz um teste aqui e no internet explorer ele salva no banco :S

ai meu deus

Compartilhar este post


Link para o post
Compartilhar em outros sites

separe o arquivo que vai receber a requisição, desse código do textarea.

 

o arquivo que vai ser chamado, só deve conter saida que for interessante ao retorno do ajax que você quer (no teu caso, só a conexão com o banco, a string de update, e a resposta se deu certo)

 

faça esse select pro textarea, em outro arquivo.

Compartilhar este post


Link para o post
Compartilhar em outros sites

Faça assim no meu caso funcionou

 

 

 

 

index.php




 <?php

    // esta vido de uma outra pagina para editar detalhe da galeria de foto 
    //  
    $verfoto = $_GET['verfoto'];  


     include"funcoes/conexao.php";

       $gal = mysql_query("select * from roteiro where id = '$verfoto' ")or die(mysql_error());
	$g = mysql_fetch_array($gal);
	$idr   =  $g['id'];
	$nome  =  $g['nome'];
	$texto =  $g['texto'];
	$ms_texto =  $g['texto'];


	?>

<!doctype html public "-//W3C//DTD HTML 4.0 //EN">

<html>

<head>
<title>Ajax Inline Edit </title>

<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<link href="dependencies/screen.css" type="text/css" rel="stylesheet" />

<script type="text/javascript">
// <![CDATA[	

$(document).ready(function(){	

	$('#save').hide();
	$('#cancel').hide();

	$('#editPanel').click(function() {  

		$('#editPanel').attr('contentEditable', true);
		$('#editPanel').focus();
		$('#editPanel').css('cursor','text');

		$('#save').fadeIn();
		$('#cancel').fadeIn();

	});

	$('#save').click(function(){    

		var text = $('#editPanel').html();
		//alert(text);
		$("#Mesg").html('<img  src="load.gif" id="loader" />');

		 $.ajax({
			type: "POST",
			url: "update.php?idr=<?php echo "$idr"; ?>
                               data :"text=" + text,
			success: function(){

			 $("#Mesg").html('Text Updated !');

			 $('#editPanel').attr('contentEditable', false);
			 $('#editPanel').css('cursor','pointer');

			 $('#save').fadeOut();
			 $('#cancel').fadeOut();

			}
		 });
	 });

	 $('#cancel').click(function(){    

	 	$('#editPanel').attr('contentEditable', false);
		 $('#editPanel').css('cursor','pointer');

		 $('#save').fadeOut();
		 $('#cancel').fadeOut();
	 });

});

// ]]>
</script>

</head>
<body>

<div align="center">

	<h1>
		Ajax Inline Edit 
	</h1>

	<br clear="all" />

	<div id="wrap">

		<div id="Mesg"> </div>

		<?php  include_once('update.php');?>

		<a href="#" id="save" class="save">Save</a>
		<a href="#" id="cancel" class="cancel">Cancel</a>
		<br clear="all" />
	</div>


</div>

<br clear="all" /><br clear="all" /><br clear="all" />
<br clear="all" />

</body>
</html>

 

update.php

<?php
$link = mysql_connect('localhost', 'root', '');
@mysql_select_db('inline',$link);

function checkValues($value)
{
	$value = trim($value);
	if (get_magic_quotes_gpc()) {
		$value = stripslashes($value);
	}
	$value = strtr($value,array_flip(get_html_translation_table(HTML_ENTITIES)));
	$value = strip_tags($value);
	$value = mysql_real_escape_string($value);
	$value = htmlspecialchars ($value);
	return $value;
}	

if( isset($_POST['text'])){

               // estou pegando o id do ajax
               $idr = $_GET['idr'];  

	$text = checkValues($_REQUEST['text']);
	mysql_query("update minha_tabela set text = '".$text."' where id = '$idr'");

	$result = mysql_query("SELECT * from minha_tabela where id = '$idr'");
}
else
{
               // estou pegando o texto do banco
	mysql_query("update minha_tabela set text = '$ms_texto' where id = '$idr'");

	$result = mysql_query("SELECT * from minha_tabela where id = '$idr'");
}

while ($row = mysql_fetch_array($result))
{?>
	<p id="editPanel">
	<?php
	echo $row['texto'];?>
	</p>
	<?php
}?>

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.