Pass input value to another page

Asked

Viewed 1,222 times

1

I have a page where I receive in a table values from the database.

        echo "<tr>";

        echo "<th>Empresa</th>";
        echo "<th>Categoria</th>";
        echo "<th>Serviço</th>";
        echo "<th>Descrição</th>";
        echo "<th>Pagamento</th>";
        echo "<th>Distrito</th>";
        echo "<th>Investimento</th>";



        echo "</tr>";

    $numLinhas = 0;
    while ($produto = mysql_fetch_array($produtos)) {
        $numLinhas++;

        echo "<tr class='table-hover'>";
        //echo "<td>".$produto['id']."-".$numLinhas."º</td>";
        echo "<td>autor:".$produto['user_of'] . "</td>";
        echo "<td>".$produto['id'] . "</td>";
        echo "<td>".$produto['categ'] . "</td>";
        echo "<td>".$produto['titulo'] . "</td>";
        echo "<td>".$produto['descricao'] . "</td>";
        echo "<td>".$produto['valor'] . "</td>";
        echo "<td>".$produto['local'] . "</td>";
        echo "<td>".$produto['investimento'] . "</td>";



        echo "<td><input style='width:40px' type='number' name='novo_investimento' value='0'><a href=up_invest.php?id=".$produto['id'].">
        <input name='submit' type='submit' value='Ok'></a></td> ";




        echo "</tr>";

    }
        echo "</table>";

In this same table I have an input number which I call 'new_investment'.

 echo "<td><input style='width:40px' type='number' name='novo_investimento'  value='0'><a href=up_invest.php?id=".$produto['id'].">
        <input name='submit' type='submit' value='Ok'></a></td> ";

On the up_invest.php page, where the data is processed, I would like to take the line id and the new value defined by the user of the 'new investment' field of this line.

The way it is, on the up_invest.php page when I do this

 $id= $_REQUEST['id'];
 $investimento=$_REQUEST['novo_investimento'];
 echo "$id $investimento";

He just picks up the id. How do I get the value of the 'new investment' field'?

Notice: Undefined index: novo_investimento

With the < form element >

    echo "<td><form action='up_invest.php' method='post'><input  style='width:40px' type='number' name='novo_investimento' value='0'>
        <input name='submit' type='submit' value='Ok'></form></td> ";

it takes the value of 'new investment', but loses the line id. How do I get the line id where this form is displayed?

Solved. I created an Hidden field with the line ID value.

 echo "<td><form id='form1' name='form1' action='up_invest.php'  method='post'><input style='width:40px' type='number' name='novo_investimento'  value='0'>

        <input type='hidden' name='id' value=".$produto['id'].">

        <input name='submit' type='submit' value='Ok'></form></td> ";
  • Seems right to me. You can put the HTML that reaches the page to be clearer as the HTML?

  • On the page where I handle the data, I don’t have html yet, I just wanted to get the values. On the page where the data are presented the first time is only one table, where they appear in their own <td>. I’m not getting the 'new investment' value. I don’t know why...

  • 2

    Put this HTML, even simple, in the question or in a jsFiddle to see if we find the problem... For example in this PHP I do not see the element <form>

  • 1

    I was with a similar problem I brought from the database by a submit_1 to a table and if it had 50% filled would need to take the id and make another submit_2 on the same page so I ended up saving the id of the first Ubmit in a Hidden input and when to fill the The rest of the table used this Hidden input to update with submit_2. Summary when bringing the data from the database save what you want in Hidden inputs (already inside form_2) to use later with submit_2.

  • That’s what I ended up doing. I created an Hidden input with the ID line value and passed it :)

  • I’ll put as an answer to help the other blz :)

Show 1 more comment

2 answers

1

I had a similar problem I brought from the database, data by a submit_1 to a table and if it had 50% filled would need to take the id and make another submit_2 on the same page. Then I ended up saving the id of the first submit_1 in an Hidden input and when I was going to fill the rest of the table I used this Hidden input to update(update) with submit_2.

Summary when bringing the data from the database save what you want in Hidden inputs (already inside form_2) to use later with submit_2

1


When we send a form to PHP (to the server in general) what is passed are elements like input, textarea, select. PHP receives an associative array where the keys are the attribute values name and the value that is assigned to each key comes from the attribute value.

So if there is no HTML element of this type with name="id" then PHP will not receive anything. We have to join something like

<input name='id' value="algumValor" />

If what you want is to save the value of that line/id that comes from PHP, and you don’t want it to be visible, then you can use type="hidden" and assign the $produto['id'] at the value of input. Something like that:

echo "<input type='hidden' name='id' value=".$produto['id']." />";

This way the element is invisible but the server still receives this information in the $_POST.

Browser other questions tagged

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