I apologize for my bad English, I hope I can explain myself better.
- Json file: through this code the call is made to view the contents of the file:
<? php
$ login = 'XL8T7924H9G9U1M99GTSVV9BHC2KYRFL';
$ password = '';
$ url = 'https://www.website.com/api/products?&output_format=JSON&display=full';
$ ch = curl_init ();
curl_setopt ($ ch, CURLOPT_URL, $ url);
curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt ($ ch, CURLOPT_USERPWD, "$ login: $ password");
$ result = curl_exec ($ ch);
curl_close ($ ch);
$ json = json_decode ($ result, true);
/ echo ($ result);
?>
- results are shown with datatable this code
<form action = "" method = "post">
<table id = "example" class = "table table-striped table-bordered zero-configuration">
<thead>
<tr>
<th class = "th-sm"> # </th>
<th class = "th-sm"> Name </th>
<th class = "th-sm"> Description </th>
<th class = "th-sm"> Short Description </th>
<th class = "th-sm"> Image </th>
<th class = "th-sm"> Activate </th>
</tr>
</thead>
<tbody>
<div class = "form-group" style = "text-align: -webkit-center;">
<div class = "form-check form-check-inline">
<? php
foreach ($ json ['products'] as $ data)
{
echo "<tr>";
echo "<td>". $ data ['id']. "</td>";
echo "<td>". $ data ['name']. "</td>";
echo "<td>". $ data ['description']. "</td>";
echo "<td>". $ data ['description_short']. "</td>";
echo "<td>". '<img src = "http://www.website.com/'.$data [" id_default_image "] .'- home_default /'.$ data [" link_rewrite "].'. jpg" class = "img-thumbnail "width =" 120 "height =" auto "/> ';
echo "<td> <input type = 'checkbox' name = 'checkbox' value = '1'> </td>";
echo "</tr>";
}?>
</tbody>
</table>
<input type = "submit" name = "submitp" id = "submitp" value = "submit">
</form>
- through a checkbox and submit I can save the information on a txt file this is the code
<? php
if (isset ($ _ POST ['submitp'])) {
$ id = "product id:". $ _ POST ['id']. "";
$ name = "product name:". $ _ POST ['name']. "";
$ description = "product description:". $ _ POST ['description']. "";
$ description_short = "short product description:". $ _ POST ['description_short']. "";
$ image = "product image:". $ _ POST ['image']. "";
$ file = fopen ("file.txt", "w ");
fwrite ($ file, $ id);
fwrite ($ file, $ name);
fwrite ($ file, $ description);
fwrite ($ file, $ short_description);
fwrite ($ file, $ image);
fclose ($ file);
}
?>
I know I'm wrong but I can't figure out how to save the entire selected row.
what you want to achieve: saving the selected row with its id, name, description and short description through the checkbox
errors: none
what it saves: only the contents of the checkbox
I hope I have explained myself well and thank you for your help
CodePudding user response:
When you submit, it only submitsinput
s (and a couple of other input types, like select
, textarea
).
<td>123</td>
will not be submitted.
You can add hidden inputs, built and populated at the same time as your build your table, which will then also be submitted, eg:
<td>123<input type='hidden' name='id[0]' value='123'></td>
You'll need to use the correct format for name=id[0]
to ensure php picks up all the rows in different values; there's different ways to do this.
The essential part of this question/answer is to include a hidden input.
CodePudding user response:
Only form fields are submitted to the server. The table cells without a field will not be sent on submission
You can have a set of hidden fields populated by JavaScript when you check the box, (see later)
Alternatively you could use AJAX
You have checkboxes, do you want to save more than one row? If not, use radios instead
Change
echo "<td>". $data ['id']. "</td>";
echo "<td>". $data ['name']. "</td>";
echo "<td>". $data ['description']. "</td>";
echo "<td>". $data ['description_short']. "</td>";
to
echo "<td data-key=\"id\">". $data ['id']. "</td>";
echo "<td data-key=\"name\">". $data ['name']. "</td>";
echo "<td data-key=\"description\">". $data ['description']. "</td>";
echo "<td data-key=\"description_short\">". $data ['description_short']. "</td>";
echo "<td data-key=\"image\">". '<img src = "http://www.website.com/'.$data [" id_default_image "] .'- home_default /'.$ data [" link_rewrite "].'. jpg" class = "img-thumbnail "width =" 120 "height =" auto "/> ';
Then use
$(function() {
$("form").on("submit",function(e) {
e.preventDefault(); // stop submission
let details = $('input[name="checkbox"]:checked')
.closest("tr") / parent row
.find("td[key]") // only the ones with a key attribute
.map(function() { return { [this.dataset.key] : this.textContent || $(this).find(img).attr("src") })
.get();
$.post("yourSaveUrl.php",details,function() { console.log("Details saved") })
})
})
Using hidden fields for ONE checked box (same data attributes as above):
<form>
<input type="hidden" name="id">
<input type="hidden" name="name">
<input type="hidden" name="description">
<input type="hidden" name="description_short">
<input type="hidden" name="image" />
using
$(function() {
$("form").on("submit",function(e) {
const form = this; // save the form
$('input[name="checkbox"]:checked')
.closest("tr") / parent row
.find("td[key]") // only the ones with a key attribute
.each(function() {
form[key].value = this.textContent || $(this).find(img).attr("src");
})
})
})