1

bit of a selfish question but I am really strugerling with ajax in general so I need some help. So basically Im trying to update and sql database using an 'onblur' function. heres my code:

code on index.php

   function saveStatus(){
var status =document.getElementById("statusForm").value;
$.ajax({
                url: 'saveStatus.php',
                type: 'post',
                data: 'feed_id=' + status,
                success: function(result){
                }
              }    
    <form id="statusUpdate" action = "whosout.php" method="post"> 
        <input type="text" id="statusForm" onblur="saveStatus()" 
        placeholder="<?php if($status!=null){ echo '&lsquo;'.$status.'&rsquo;';}
        else { echo 'Enter your status here.';}?>"> 
        </form>

and code on saveStatus.php

    <?
     require 'core.php';
     require 'connect.php';

      $status = $_POST['feed_id'];
      $idPerson = $_SESSION['user_id'];
      $query = "UPDATE person SET status = '".mysql_real_escape_string($status)."' 
     WHERE idPerson = '$idPerson'"; 
    $query_run = mysql_query($query);
     ?>

at the moment the sql database does not update when i click of the input box. Any help would be great!!

2
  • 1
    Please attempt to narrow down error. Commented Apr 25, 2014 at 22:12
  • 1
    Check for errors in your browser's console. Commented Apr 25, 2014 at 22:14

1 Answer 1

5

Answer:

  1. You have to devide scripts and html output
  2. You forget );.You have to close $.ajax block.
  3. You miss } closing function saveStatus().

Code:

<script>
function saveStatus(){
    var status =document.getElementById("statusForm").value;
    $.ajax({
        url: 'saveStatus.php',
        type: 'post',
        data: 'feed_id=' + status,
        success: function(result){

        }
    }); // <-- Also, you forget `);` here 
} // <-- Also, you foget closing `}` here
</script>

<form id="statusUpdate" action = "whosout.php" method="post"> 
<input type="text" id="statusForm" onblur="saveStatus()" 
placeholder="<?php if($status!=null){ echo '&lsquo;'.$status.'&rsquo;';}
else { echo 'Enter your status here.';}?>"> 
</form>

Not error, but advice:

Also, note this: you used jQuery ajax function, so use jQuery in other cases too (you used pure JavaScript instead).

You getting value this way:

var status = document.getElementById("statusForm").value;

Use jQuery syntax instead:

var status = $("#statusForm").val();

Additional info

As, @Utkanos noticed in comments to this answer:

...all of which would be obvious if you look in the error console.

You have to use some debug tool, like FireBug or DevTools (Chrome, also in Chrome you can use CTRL+SHIFT+J). Not have to. You MUST do it.

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

2 Comments

...all of which would be obvious if you look in the error console.
Also, mention about $('#statusForm').val() instead of var status =document.getElementById("statusForm").value;.

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.