0

I have a table in my PHP page inside the HTML block of a PHP. Here is the code below for the table.

<form enctype="application/x-www-form-urlencoded">
<table width="200" border="1">
  <tr>
    <td>Product</td>
    <td>Promotional Price</td>
    <td>Regular Price</td>
    <td>Stacking</td>
  </tr>
  <tr>
    <td><input type="text" id="product"></td>
    <td><input type="text" id="pp1"></td>
    <td><input type="text" id="rp1"></td>
    <td><input type="text" id="stacking"></td>
  </tr>
</table>
<div id ="div1">
<input type="button"  value="Submit"  onClick="PostData();"/><br/>
</div>
</form> 

Javascript for the same to send it to another PHP is as below.

<script type="text/javascript">
function PostData() {




    // 1. Create XHR instance - Start
    var xhr;
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        xhr = new ActiveXObject("Msxml2.XMLHTTP");
    }
    else {
        throw new Error("Ajax is not supported by this browser");
    }
    // 1. Create XHR instance - End

    // 2. Define what to do when XHR feed you the response from the server - Start
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status == 200 && xhr.status < 300) {
                document.getElementById('div1').innerHTML = xhr.responseText;
            }
        }
    }
    // 2. Define what to do when XHR feed you the response from the server - Start
    var product = document.getElementById("product").value;
    var pp1 = document.getElementById("pp1").value;
    var rp1 = document.getElementById("rp1").value;
    var stacking = document.getElementById("stacking").value;

    // var image = document.getElementById("image").value;
    // 3. Specify your action, location and Send to the server - Start
xhr.open('POST', 'report.php');
    //xhr.open('POST', 'config.php');
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send("product=" + product + "pp1=" + pp1 + "rp1=" + rp1 + "stacking=" + stacking);
}

</script>

and the PHP to store the value to the database is as below.

    <?php  
    //Updated after the answer from AnikIslam
    $servername = "localhost";
$username = "root";
$password = "";
$dbname = "testing";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO tbl_report (product,pp1, rp1,stacking) VALUES ('$product', '$pp1', '$rp1','$stacking')";

if ($conn->query($sql) === TRUE) {
    echo "Successful";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
    ?>

As seen in the screenshot, I have 2 blocks to enter the data. Now the data is entered into the database from the first block only. I want the data to be stored from the second block to the second row in the database. How can it be achieved.

enter image description here

14
  • try doing a var_dump($_POST); to see if you are receiving any data at all Commented Aug 7, 2015 at 4:06
  • why this line is out : //xhr.open('POST', 'config.php'); Commented Aug 7, 2015 at 4:10
  • is this right way if(isset($_POST['product'],$_POST['pp1'],$_POST['rp1'],$_POST['stacking'])) Commented Aug 7, 2015 at 4:12
  • @Satya where it should be done?? Commented Aug 7, 2015 at 4:12
  • @Shehary I did it before for others and it works perfectly fine Commented Aug 7, 2015 at 4:14

1 Answer 1

2

Try like this

 xhr.send("product=" + product + "&pp1=" + pp1 + "&rp1=" + rp1 + "&stacking=" + stacking);

For Php Part

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testing";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO tbl_report (product,pp1, rp1,stacking) VALUES ('$product', '$pp1', '$rp1','$stacking')";

if ($conn->query($sql) === TRUE) {
    echo "Successful";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

Edit

each form's should have unique id

pass block name in submit button

Like this

first block

onClick="PostData('Block1');"

second block

onClick="PostData('Block2');"

JS

function PostData(block){



  if(block==="Block1"){
     // fetech data from block1
  }
  else if(block==="Block2"){
     // fetech data from block2
  }

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

12 Comments

Now it says No Database Selected
now it's passing data to your php file :) .. please provide the code of config.php file
You are mixing mysql with mysqli .. both are different things. I have updated my answer . please check.
Works completely fine. Thanks a lot.
|

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.