How to pass TD id through POST via form

Viewed 85

I have a small problem. I have this in my code:

    <td id="def1s"></td>
    <td id="def1r"></td>
    <td id="def1g"></td>
    <td id="def1m"></td>

where the id's are populated through AJAX when i change select (dropdown).

Now i want to pass the id's to database when i press Save Changes button on the form.

I figured out i can't pass id value's through POST, neither can i use "name=". How can i make it possible to send the value's given after changing select via POST to database?

EDIT

Thanks for the input so far, i tried some things, but i guess it's a bit more complicated...

in html i have this:

<select class="form-control" id="weapon" name="weapon">
        <option value="<?= $general['weapongear']; ?>"><?= $general['weapongear']; ?></option>
    <?php foreach($gearweapons as $gearweapon): ?>
        <option value="<?= $gearweapon['ID']; ?>"><?= $gearweapon['gearname']; ?></option>
    <?php endforeach; ?>
</select>
<table class="table table-dark table-striped">
<thead>
  <tr>
    <th></th>
    <th>Siege</th>
    <th>Ranged</th>
    <th>Ground</th>
    <th>Mount</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td>ATT</td>
    <td id="att1s"></td>
    <td id="att1r"></td>
    <td id="att1g"></td>
    <td id="att1m"></td>
  </tr>
  <tr>
    <td>DEF</td>
    <td id="def1s"></td>
    <td id="def1r"></td>
    <td id="def1g"></td>
    <td id="def1m"></td>
  </tr>
  <tr>
    <td>HP</td>
    <td id="hp1s"></td>
    <td id="hp1r"></td>
    <td id="hp1g"></td>
    <td id="hp1m"></td>
  </tr>
</tbody>
</table>

Where all those id's are populated on select change:

$("#weapon").change(function() {
    //get the selected value
    var valweapon = this.value;
    //make the ajax call
    $.ajax({
        url: '/ajax/gear_ajax.php',
        type: 'POST',
        data: {field : 'ID', value: valweapon},
        success: function(data) {
          document.getElementById("att1s").innerText = data['atts'];
          document.getElementById("att1r").innerText = data['attr'];
          document.getElementById("att1g").innerText = data['attg'];
          document.getElementById("att1m").innerText = data['attm'];
          document.getElementById("def1s").innerText = data['defs'];
          document.getElementById("def1r").innerText = data['defr'];
          document.getElementById("def1g").innerText = data['defg'];
          document.getElementById("def1m").innerText = data['defm'];
          document.getElementById("hp1s").innerText = data['hps'];
          document.getElementById("hp1r").innerText = data['hpr'];
          document.getElementById("hp1g").innerText = data['hpg'];
          document.getElementById("hp1m").innerText = data['hpm'];
        }
    });
});

This is how the result looks like:

table

When i click submit button, all the values in the table should be update to database, regardless there is a value or it is NULL

1 Answers

There are many ways, but I am giving you the easiest one.

when you change select (dropdown), You need to pass the select box value plus COMMA SEPARATED STRING OF IDS

You can use json format and separate both in your ajax function. Thus in this way, assign the string to a existing form hidden value and when you will submit the form, you will get all the IDS

Related