tags:

views:

386

answers:

4

Hi all,

Using PHP, and MySql, I am trying to implement this rather snazzy in-place editor available here:

tutorial: http://www.davehauenstein.com/code/jquery-edit-in-place/

js file: http://davehauenstein.com/code/scripts/jquery.inplace.min.js

OK what is happening is,

  1. I click the element to edit the text.
  2. I clear the old text, and enter the new text.
  3. I click outside of the element to initiate Ajax to save the new text.
    1. Ajax shows "Saving..", which is default text for updating element.
    2. The new text is updated in the database.
  4. Element reloads, and instead of showing the new text inside of element, the element shows (Click here to edit text), which is the default text in the js file for an element that returns empty.

The Problem: Only once I refresh the page manually, is the new text loaded in element. Instead of loading in the element without refreshing.

The element with in-place edit:

<div class="editme1"><?php
$content = $_GET['update_value'];
// i added this to try and get the updated value but im
// not really sure, just a guess. i have also used $_POST
// in an attempt to catch it....but i feel stupid even
// saying that..lol
if(!empty($content)) { echo "$content"; } else 
{ echo "$row[profilevalue_8]"; }
?></div>

The file (update.php) which updates the database:

<?php include('includes/config.php');
include('includes/functions.php');
$name = $_POST[update_value];
$update = mysql_query("UPDATE profilevalues SET profilevalue_8 = '".$name."' 
WHERE profilevalue_user_id = '".uid()."'") or die(mysql_error());
?>

The javascript

<script type="text/javascript">
    $(document).ready(function(){
        $(".editme1").editInPlace({
            url: "http://www.mysite.com/update.php",
            params: "ajax=yes",
        });
</script>

The issue is I guess, is that I cant seem to reflect the changes in the element as expected.

I would be so grateful for any help.

Thanks

+1  A: 

Your forgot something in the update.php :) Every In Place Editor does an AJAX request to the update file and then puts in the edited element the things it gets in the update.php with that request.

So your update.php needs to be like this

<?php include('includes/config.php');
include('includes/functions.php');
$name = $_POST[update_value];
$update = mysql_query("UPDATE profilevalues SET profilevalue_8 = '".$name."' 
WHERE profilevalue_user_id = '".uid()."'") or die(mysql_error());

echo $_POST['update_value']; //add this to your file and it should be working now
?>
Bogdan Constantinescu
I am so silly.... but sometimes it takes a person to point out the obvious for someone(me) to get it...lolol....Thank you, I am forever in your debt...lol
Lea
No worries, that's why StackOverflow is so cool :)
Bogdan Constantinescu
A: 

To add to what Bogdan Constantinescu said, I think it is worth pointing out that to guard against people entering nasty data you should really

  • Call htmlspecialchars() on any untrusted strings you are sending to the browser as HTML. This will help prevent XSS attacks.
  • Call mysql_real_escape_string() on any strings you are putting directly into SQL statements. This will protect you from SQL injection attacks.
Tom Haigh
Hi tomhaigh, thanks for your comment. I whole heartedly agree, very much so worth point out those two important functions. And to also note to any readers, that this is a "preliminary example" used to test, and that all input should be sanitized before posting. Thanks!
Lea
A: 

I'm looking to pass multiple values via Another In Place Editor, anyone done that/willing to share?

mahalie
You should probably make this into a new question, not an answer
Tom Haigh
A: 

Hi guys,

Thank you for this helpful post and special thanks to Bogdan. I get worked codes but just have a question. What if I have more than 1 field to update? How can I chance or add another value for update_value

Thanks

Ahmet Kemal