Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7773313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:06:24+00:00 2026-06-01T17:06:24+00:00

trying to insert a record in my DB using AJAX for the very first

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:06:25+00:00Added an answer on June 1, 2026 at 5:06 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to insert a record into a table using Linq but get
I am experiencing a very frustrating issue when trying to insert a new record
I am trying to insert a record using Link to SQL. I know I
I need help with trying to insert a record using MVC and Entity Framework.
I am trying to insert a record into an Oracle database I have a
I am trying to insert a record in Excel using asp.net but it is
I'm using a mysql database, and I'm trying to insert a record into tableB
I am trying to insert a standard record into my db using linq2db, but
I'm trying to insert records into DB using AJAX. I'm not sure why, but
I am trying to use stored proc to insert record using detailsview and sqldatasource.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.