John Davidson

php - save row value when I select the checkbox

0 comments
Message:


I apologize for my bad English, I hope I can explain myself better.



  1. 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);
?>


  1. 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>


  1. 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



My Answer:
// Read JSON file
$json = file_get_contents('data.json');

// Decode JSON file
$data = json_decode($json, true);

// Display data
foreach ($data as $row) {
echo '' . $row['name'] . '
';
}
?>
__HTML form: this code shows the form with the checkboxes and a submit button:__
html

' . $row['name'] . '
';
}
?>



__Save selected rows: this code saves the selected rows to a new JSON file called selected.json:__
php


__Explanation:__
1. The first code snippet reads the data from a JSON file and displays it as checkboxes in a form.
2. The second code snippet shows the HTML form with checkboxes and a submit button.
3. The third code snippet saves the selected rows to a new JSON file called selected.json when the form is submitted.
I hope this helps! Let me know if you have any questions.

Rate this post

3 of 5 based on 4611 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us