HTML form values are not varying within while PHP

Asked

Viewed 54 times

2

Well, I made an extract that selects the DB data and prints them on the screen through while, which is generating the correct values, but now I’m trying to create an edit button that opens a popup with inputs already filled with values, but this form is only receiving the values of the 1st loop. Does anyone have any suggestions to solve the problem?

Follow the code I’m trying to fix:

while($linha=$buscarextrato->fetch(PDO::FETCH_ASSOC)){
echo '
<div class="div" style="border:0px;width:80px;">
    <div data-role="popup" id="Popup" class="ui-content" style="min-width:500px;">
            <form style="display:inline;" name="lanc" action="editalanc.php" method="POST" enctype="multipart/form-data" >
                <input type="hidden" name="numlanc" value="'.$linha[lancamento].'"/>
                <input type="date" name="data" value="'.$linha[data].'"/>
                <input type="number" name="debito" value="'.$linha[debito].'"/>
                <input type="number" name="credito" value="'.$linha[credito].'"/>
                <input type="number" name="valor" value="'.$linha[valor].'"/>
                <input type="text" name="descricao" value="'.$linha[descricao].'"/>
            </form>
    </div>
    <a href="#Popup" data-rel="popup"><i class="material-icons">mode_edit</i></a>
</div>
    ';

JS code obtained by reference:

 <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
 <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>

CSS:

<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">

OBS: The generated values (line) vary correctly in each loop, the only thing that does not change is the form, it seems to me that it receives the value of 1 loop and ignores the rest.

  • Probably your error is in Javascript, trying to open the popup through id #Popup. Like the attribute id defines a unique DOM element, it will always be the first form. You will need to change this to fix the problem, either generating ids unique to each form, whether changing the way of referencing it.

  • in case I am using jquery, I have no idea how to modify the code, I must change the way to do it then?

  • Yes, but first edit your question and add your JS code that deals with the popup.

  • the source is in the links I posted above.

2 answers

1


Problem solved, as suggested by Anderson Carlos Woss the problem was in the ID of the popup that was the same in all cases, through a gambiarra I did the following:

<div data-role="popup" id="Popup'.linha[lancamento].'" class="ui-content" style="min-width:500px;">

I concluded with the lancamento line that it is unique to generate a popup in each loop. I did the same for the button that the reference.

<a href="#Popup'.linha[lancamento].'" data-rel="popup"><i class="material-icons">mode_edit</i></a>

0

Use fetchAll instead of fetch

The fetch will return you a simple array, it is ideal when there is only one record, already fetchAll return an array with all results.

  • Can you quote the source who said what you said in the reply? Why the fetch is not indicated for multiple records? And what is the relation of this to the question problem, since the records are being listed correctly?

  • 1

    @Andersoncarloswoss, I misunderstood the question, in relation to what I said, anyone knows that using native functions has a higher performance, can be milliseconds depending on the records, but the question is, if there is a function precisely for that purpose why not usela? According to the fetch documentation returns the next line, and fetchAll all lines, and the discussion should stop there, it is not reference but text interpretation. (By the way, in this case, all the benchmarks I have seen to date give positive results for fetchAll)

  • Yes, the difference would be in storing all records in memory or not. The fetchAll stores all in memory, while the fetch stores only one record at a time, only when used. I particularly see no reason to use the fetchAll here as there is no need to have any data in memory.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.