Jordanbaggs
Jordanbaggs

Reputation: 59

why is my form not submiting using ajax

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!!

Upvotes: 1

Views: 57

Answers (1)

Sharikov Vladislav
Sharikov Vladislav

Reputation: 7269

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.

Upvotes: 5

Related Questions