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 6100233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:21:44+00:00 2026-05-23T13:21:44+00:00

HTML <form action=’insert.php’ method=’POST’> <p><b>Client:</b><input type=’text’ name=’idclient’/> <p><b>Total:</b><br /><input type=’text’ name=’total’/> <p><input type=’submit’ value=’Save’

  • 0

HTML

<form action='insert.php' method='POST'>
    <p><b>Client:</b><input type='text' name='idclient'/> 
    <p><b>Total:</b><br /><input type='text' name='total'/> 

    <p><input type='submit' value='Save' id="btnSave"/>
    <input type='hidden' value='1' name='submitted' /> 
</form> 

PHP (insert.php)

<?php
    echo file_get_contents('php://input');
    include_once "connect.php"; 

    if ($db_found){
        if (isset($_POST['submitted'])) {
            foreach($_POST AS $key => $value) {
                 $_POST[$key] = mysql_real_escape_string($value);                
            } 

            $sql = "INSERT INTO `mytable` ( `idclient` ,  `total` ,  ) " . 
                       "VALUES(  {$_POST['idclient']} , {$_POST['total']}  ) ";
            mysql_query($sql) or die(mysql_error()); 
        }       
    }
    mysql_close($db_handle);
?>

This works OK, but when I try to call the insert using Ajax the $_POST function is empty and I cannot access the values from the form.

This is the ajax code and the function call:

<form action="javascript:Save()" method='POST'>

Ajax

function Save()
{                           
  xmlHttp = getXMLHttp(); // returns a new XMLHttpRequest or ActiveXObject
  xmlHttp.onreadystatechange = function(){

    if(xmlHttp.readyState == 4) { 
      document.getElementById("btnSave").value = "Saved";
      document.getElementById("result").innerHTML = xmlHttp.responseText;
    }
    else{
      document.getElementById("btnSave").value = "Saving...";
    }
  }

  xmlHttp.open("POST", "insert.php", true);     
  xmlHttp.send(null);  // Do I need to create and pass a parameter string here?             
}

Doing an echo file_get_contents('php://input'); indeed the $_POST is empty and the parameters values are not passed along.

I could concatenate the params value in the URL like this

xmlHttp.open("POST", "insert.php?idclient=123&total=43", true);   

But, is there a way to use $_POST and take advantage of it?

  • 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-05-23T13:21:45+00:00Added an answer on May 23, 2026 at 1:21 pm

    Based on the answers this is what I came up with:

    var xmlHttp = getXMLHttp(); // returns a new XMLHttpRequest or ActiveXObject
    
    function Save() {
      if(xmlhttp) { 
        xmlhttp.open("POST","insert.php",true); 
        xmlhttp.onreadystatechange  = handleServerResponse;
    
        xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');    
        xmlhttp.send("idclient=" + document.getElementById("idclient").value  
                    + "&total=" + document.getElementById("total").value); 
      }
    }
    
    function handleServerResponse() {
       if (xmlhttp.readyState == 4) {
         if(xmlhttp.status == 200) {
            alert("responseText= " + xmlhttp.responseText);
            document.getElementById("btnSave").value = "Saved";
            document.getElementById("result").innerHTML = xmlhttp.responseText; 
         }
         else {
            alert("Error during AJAX call. Please try again");
         }
       }
       else{
        document.getElementById("btnSave").value = "Saving...";
       }
    }
    

    After a while of searching I found out that defining as separate this function (and not inline in onreadystatechange, as in the original question) would get it to work.

    xmlhttp.onreadystatechange  = handleServerResponse;
    

    However, @Daren made a good point on “special” input values. For example, passing an ‘&’ character in a string field will get incomplete values.

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

Sidebar

Related Questions

<form action=/Villa/Add method=post> <table> <tr> <td> Name: </td> <td> <%= Html.TextBox(name) %> <%= Html.ValidationMessage(Name)
I have a html form like this : <form action=https://www.123.com/cgi-bin/action method=post> <input type=hidden name=item_name
I've a form with struts tags as below. <html:form styleId='catform' action='/cat/submit.html' method='post'> <html:select property='catName'
I have a html form that his action is for exmaple.com/mail.php?name=dan for example. How
I have two buttons inside a form call: <% using (Html.BeginForm()) {%> <input type=submit
I have a HTML form, and I have a Controller Action that accepts the
In an HTML form post what are valid characters for creating a multipart boundary?
I have a an HTML form which contains the YAHOO rich text editor on
I am implementing an HTML form with some checkbox input elements, and I want
I have an HTML form - with PHP, I am sending the data of

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.