简体   繁体   中英

How to assign unique value in hidden fields of a php edit form in a for loop

I'm working on a edit form, which is based on the titleId field of the database. On clicking the update button it gets redirected to a javascript function.

The page has a list of titles, after clicking on any of them you get to a editable form in which you can edit the data and update it. The page appears on selecting the type of titles the user wishes to see. Here's the code:

Edit form:

 <div class="panel-group" id="facultyAccordion">
        <?php      
        for ($i = 0; $i < count($getAll); $i++) {         
        echo <<<HTML
            <div class="panel panel-default">
                <div class="panel-heading"><h4 class="panel-title">
                    <a data-toggle="collapse" data- 
       parent="#facultyAccordion" href="#collapseF{$i}">{$getAll[$i]['title']} 
   </a></h4>
                 </div>
                <div id="collapseF{$i}" class="panel-collapse collapse">
                    <div class="panel-body">
                       <form id="editTitle" method="post">
                          <div class="table-responsive">
                            <div class="form-group">
                    <label for="desc">Title:</label>
                    <input type="text" class="form-control" id="editadminTitle" name="title" value="{$getAll[$i]['title']}">
                             </div>

                            <div class="form-group">
                    <label for="desc">Units:</label>
                    <input type="text" class="form-control" id="editadminUnits" name="units" value="{$getAll[$i]['units']}">
                            </div>

                           <div class="form-group">
                    <label for="desc">Category:</label>
                    <input type="text" class="form-control" id="editadminCategory" name="category" value="{$getAll[$i]['category']}">
                          </div>                           
                       <input type="hidden" id="editTitleId" name="titleId" value="{$getAll[$i]['titleId']}"> 
                            <input type="hidden" id="editTypeId" name="typeId" value="$typeId">     
                        </div>
                       </form>     

                         <button type="Submit" class="btn btn-warning btn-sm" onclick="editTitleModalSubmit('{$getAll[$i]['titleId']}')"> Update</button>                     
   </div>
             </div>  
            </div>
HTML;
   }
   ?>
   </div>

main.js

function editTitleModalSubmit(titleId) {
 console.log("1");
   event.preventDefault();
$.ajax({
    url: 'functions/administration-functions.php',
    type: 'POST',
    data: {"title": $('#editadminTitle').val(), "units": 
$('#editadminUnits').val(), "category": $('#editadminCategory').val(), "titleId": $('#editTitleId').val(), "typeId": $('#editTypeId').val(), "switch":"edit"},
    dataType: "json",
    success: function () {
        $('#adminForm').trigger('reset');
        resultAlert('#adminResult', '#resultAdminContent', '<strong>You data was succefully saved!</strong>', 'alert-success');
    }, error: function (error) {
        console.log(error);
    }
});
 }

I just want help with assigning unique ids to the hidden field - titleId in the Edit form and on how to call the same in the main.js file.

So, in your PHP, you're using this code currently:

<input type="hidden" id="editTitleId" name="titleId" value="{$getAll[$i]['titleId']}">

If you don't need this value, you could change it to something like a randomly generated number:

$randTitleId = rand(1000, 9999);

...

<input type="hidden" id="editTitleId" name="titleId" value="{$randTitleId}"> `

Then, using jQuery, you could get this ID by referencing the name or id attributes of the input field:

var randTitleId = $('#editTitleId').val();

You can then use the variable randTitleId in your AJAX call.

I fear this may be a bit too simple an answer... :) So if I've missed something, please leave a comment and I'll amend.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM