0

The following code works properly and I am using it 'cause I need to update some notes in DB without refreshing a page. But what have to do when I need in the same event get some data back?

index.php where change should occur

<?php
$s = $GET['...']
$p = $GET['...']
print '
 <form id="notesForm">
  <textarea name="c">'.$note.'</textarea>
  <input type="text" value="LAST_CHANGE_NEED_GIVE_BACK_FROM_DB">
  <input type="hidden" name="s" value="'.$s.'">
  <input type="hidden" name="p" value="'.$p.'">
  <input type="submit" value="save note">
  <div id="response"></div>
</form>
';
?>

script which is posting data to live_notes.php and probably should be changed because of the goal

$(document).ready(function(){
    $('#notesForm').submit(function(){
        event.preventDefault();
        $('#response').html("<b>saving...</b>");
        $.ajax({
            type: 'POST',
            url: 'notes/live_notes.php',
            data: $(this).serialize()
        })
        .done(function(data){
            $('#response').html(data);
        })
        .fail(function() {
            alert("bad luck");
        });
        return false;
    });
});

notes/live_notes.php

<?php
$s = $_POST['s'];
$p = $_POST['p'];
$c = $_POST['c'];

// connecting DB

mysql_query("
UPDATE `poznamky`
SET
    last_change = now(),
    page = '$p',
    content = '$c'
WHERE
    page = '$p';
");

any idea?

5
  • You want to return some data from notes/livenotes.php? Commented Mar 28, 2014 at 6:19
  • Actually, need to reload some data from db in the same event and really don't know how to do it...:/ Commented Mar 28, 2014 at 6:20
  • you want to perform update then select some data from db and return to ajax? Commented Mar 28, 2014 at 6:21
  • Yes, exactly and print them on the screen. Commented Mar 28, 2014 at 6:22
  • Any idea what I have to do? I'm newer about this... Commented Mar 28, 2014 at 6:23

3 Answers 3

1
<?php
$s = $_POST['s'];
$p = $_POST['p'];
$c = $_POST['c'];

// connecting DB

$blnSuccess = mysql_query("
UPDATE `poznamky`
SET
    last_change = now(),
    page = '$p',
    content = '$c'
WHERE
    page = '$p';
");

if($blnSuccess){
echo "success";
}
else {
echo "not success!";
}

in your jquery :

.done(function(data){
            $('#response').html(data); 
}

data have success or not success! which return as ajax response.

Update
If you want to return some data from database then:

$result = mysql_query("
   select * 
      from `poznamky`
");

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    echo $row['content'];
}

Then it will return all content field from poznamky table into data in ajax.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for ur time and response Awlad :)
Well, you said "which return as ajax response, but how can I require specific column instead of echoing (not) success. Isn't Sarah's response something what I need?? Thanks
Ops... Im sorry.. .] Have a nice day
1
<?php
$s = $_POST['s'];
$p = $_POST['p'];
$c = $_POST['c'];

// connecting DB

$OK = mysql_query("
UPDATE `poznamky`
SET
    last_change = now(),
    page = '$p',
    content = '$c'
WHERE
    page = '$p';
");

$id = mysql_insert_id();
$res = mysql_query("select * from poznamky where id = $id");


$data = mysql_fetch_array($res);

//Print raw or JSON of $data like json_encode($data) etc

1 Comment

I would like to, but have not enough reputation .)
0

If you want to return something back then in your notes.php you can do something like this:

$s = $_POST['s'];
$p = $_POST['p'];
$c = $_POST['c'];

// connecting DB

mysql_query("
UPDATE `poznamky`
SET
    last_change = now(),
    page = '$p',
    content = '$c'
WHERE
    page = '$p';
");

$select = "//use your select query and loop here and get all data in this variable";

exit($select);

and in jquery:

.done(function(data){
           //data has the response. You can do whatever you want to do with it.
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.