3

Have a look at the screenshot below. enter image description here

As per the screenshot, I have the data fetched into the different blocks based on the database. For example, based on the username and password, these values are fetched from the database and displayed in different blocks. I have the PHP to store the value into the database, but the problem that I am facing is that when i try to upload it from other block, it still saves the value from the first block. Codes as as below:

<?php  
include('includes/config.php');
 $upload = 'uploads/';

 session_start();
 $_SESSION['$userid'];

$sql = "SELECT * FROM tbl_store INNER JOIN tbl_job ON tbl_store.store_code = tbl_job.store_code WHERE username = '$userid'";


    $result = mysqli_query($conn,$sql);
    $rowcount=mysqli_num_rows($result);
   // echo "$rowcount";
    $stores = array();
    $stores_add = array();
    $stores_chain = array();
    $job = array();
    $client = array();
    $brand = array(); 
        $week= array();
    $x = 1;
    $imgCnt =1;

        while($row = mysqli_fetch_array($result)){


         echo "工作".'<br/>';
         echo $row['jobs'].'<br/>'.'<br/>'; 
         $job[] = $row['jobs'];

         echo "客戶".'<br/>';
         echo $row['Client'].'<br/>'.'<br/>'; 
         $client[] = $row['Client'];

         echo "牌子".'<br/>';
         echo $row['Brand'].'<br/>'.'<br/>'; 
         $brand[] = $row['jobs'];

         echo "週數".'<br/>';
         echo $row['week'].'<br/>'.'<br/>'; 
         $week[] = $row['week'];

         $target = $upload.'/'.$row['Client'].'/'.$row['Brand'].'/'.$row['jobs'].'/'.$row['store_code'].'/';
          $testpath = $row['Client'].'/'.$row['Brand'].'/'.$row['jobs'].'/'.$row['store_code'].'/';
         $_SESSION['target1'.$x] = $target; 
         if(!file_exists($target))
         {
             mkdir($target,0777,true);
         }
         ?>
    <form id='uploadForm-<?php echo $imgCnt; ?>' action = '' enctype='multipart/form-data' method = 'POST' class="form<?php echo $imgCnt; ?>">
        <input type="file"  class="image<?php echo $imgCnt; ?>" name="img" onChange="readURL(this);" />
        <img id="blah" src="#" alt="your image" /><br/><br/>
        <input type='button' id = '<?php echo $imgCnt; ?>' class='uploadPicture<?php echo $imgCnt; ?> btn btn-primary' value = '上載'>
        <!-- <input type="button" value="上載" class="uploadPicture"  id="upload_btn<?php echo $imgCnt; ?>"/> -->
    </form>
<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>

    <script> src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<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);
    //xhr.send("&pid=" + pid);
    // 3. Specify your action, location and Send to the server - End


}

</script>
<?php
echo "-------------------------------------------------------".'<br/>';
        $x = $x+1;
        $imgCnt++;
      }
?>

I have removed the code for image upload from it as it works completely fine. The problem is the data from the other block is not stored to the database. only the value for the first block is stored second time even. How to solve this problem.

PHP to store data:

<?php  
$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;
}
?>
2
  • Getting values using id will just cause to get the first value. Use class instead. And tweak some more on your script. Commented Aug 7, 2015 at 5:41
  • @LoganWayne I am unable to figure out how to do that. Can you explain it a bit more with a bit of code, if possible. Commented Aug 7, 2015 at 5:47

1 Answer 1

0

To expand on what @Logan Wayne pointed out...

An ID should be unique within a page. However, if more than one element with the specified ID exists, the getElementById() method returns the first element in the source code.

So, in your JavaScript, when you grab references to your table data elements, you'll always get the FIRST instance of a Document object with whatever id you provide.

// 2. Define what to do when XHR feed you the response from the server - Start

var product = document.getElementById("product").value; <-- will always return the same element 
var pp1 = document.getElementById("pp1").value; <-- will always return the same element
var rp1 = document.getElementById("rp1").value; <-- will always return the same element 
var stacking = document.getElementById("stacking").value; <-- will always return the same element 

You'll either have to assign unique ids to your td objects, or, again as @Logan Wayne mentioned, utilize the class property of HTML DOM objects.

Classes can be used to group like elements. After assigning class names to the different columns in your table (Product, Promotional Price, Regular Price, Stacking) you can use getElementsByClassName() to get an array of the td elements.

...
var products = document.getElementsByClassName("product"); <-- array of product td elements
...
Sign up to request clarification or add additional context in comments.

6 Comments

No, still it didnt work. the value from the second block are still not in the database..
When i use the above mentioned code, the value i get in the database is [object HTMLCollecti]
'<tr> <td><input type="text" class="product"></td> <td><input type="text" class="pp1"></td> <td><input type="text" class="rp1"></td> <td><input type="text" class="stacking"></td> </tr>' Here is the code
You must stringify each object before sending it to the backend/database
How to do that? Can you explain it a bit more.
|

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.