1

trying to insert a record in my DB using AJAX for the very first time. I have the following...

Form

 <form>
     <input type="text" id="salary" name="salary">
     <input type="button" onclick="insertSalary()">
 </form>

AJAX

<script type="text/javascript">

function insertSalary()
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById('current-salary').innerHTML=xmlhttp.responseText;
    }
  };
xmlhttp.open("POST","insert_salary.php",true);
xmlhttp.send("salary=" + document.getElementById("salary").value);
}

</script>

PHP

$uid = $_SESSION['oauth_id'];      
$monthly_income = mysql_real_escape_string($_POST['salary']);

#Insert a new Record
$query = mysql_query("INSERT INTO `income` (user_id, monthly_income) VALUES ($uid, '$monthly_income')") or die(mysql_error());
$result = mysql_fetch_array($query);
return $result;

Nowmy data is being inserted into the table BESIDES the 'salary' which is being inputted as '0'

Once inserted I also have a div 'current-salary' that should then be populated with there inputted value only it isnt, Can anybody help me to understand where im going wrong?

1
  • Can you debug it in firebug or something and see if salary is being properly passed to your php? Commented Apr 12, 2012 at 19:16

1 Answer 1

4

If you want to save your self a lot of time, effort, and heartache, use the jquery library for your ajax requests. You can download it at http://jquery.com/

After adding a reference(Script tag) to the jquery script your javascript for the ajax request would become:

function insertSalary()
{
    var salary = $("#salary").val();
    $.post('insert_salary.php', {salary: salary}, function(data) 
    {
        $("#current-salary").html(data); 
    });
}

Also keep in mind that using "insert_salary.php" as the url means it is a relative path and must be in the folder of the current running script.

Your php script needs to echo whatever you would like to be injected into your current-salary tag also.

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

6 Comments

wow, that easy! It works fine, my only problem is it doesnt show a response in the div...
Eh, you caught me before I could edit it. :) You need php to echo whatever you would like in your div tag.
So if I was to add 'echo $monthly_income;' beneath return $result that should work? It's not however for some strange reason... @james
Remove the return $result line altogether and try echoing a hardcoded value just to see if it will work, then replace your hardcoded value with a variable that you want to use.
ahh my bad @james! Im running an if statement at the top of the page to say if the user has no row in table do this, else show the form, on return, I need to page to refresh, or run the IF query again somehow?
|

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.