How to send form data via _SESSION?

Asked

Viewed 185 times

0

How can I send data from the first form to the second and then send to mysql ?

I tried everything, and when I send it to the database, only the data from the second comes in. I will put down my forms and mysql .

I am grateful in advance .

form 1

    <main>

<div class="pedidos">
<form method="post" action="fazer2.php">
<table >
    <tr>
        <td colspan="4" width="100">
         <span>Mesa</span>
         <input type="text" id="numero_mesa" name="numero_mesa">
         <label>
                    <span>Comanda:</span>
                    <?php
                    $row = mysqli_fetch_array($resultado_id);
                     echo '<span>'.$row['ultimo'].'</span>';
                    ?>

                </label>
                <br>
                <br>
        </td>

    </tr>
    <tr>
    <td width="280" bgcolor="#00BFFF">Refeição</td>
    <td width="70" bgcolor="#00BFFF">Quantidade</td>

    </tr>

    <tr>
        <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Costela de Tambaqui sem Espinha">Costela de Tambaqui sem Espinha</td>
        <td width="20"><input type="text" name="num_refeicao[]" size="7"></td>

    </tr>
    <tr>
        <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Lombo de Tambaqui Frito sem Espinha">Lombo de Tambaqui Frito sem Espinha</td>
        <td width="20"><input type="text" name="num_refeicao[]" size="7"></td>

    </tr>
        <tr>
        <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Caldeirada de Tambaqui sem Espinha">Caldeirada de Tambaqui sem Espinha</td>
        <td width="20"><input type="text" name="num_refeicao[]" size="7"></td>

    </tr>
        <tr>
        <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Caldeirada de Tucunaré">Caldeirada de Tucunaré</td>
        <td width="20"><input type="text" name="num_refeicao[]" size="7"></td>

    </tr>
        <tr>
        <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Peixe no Tucupi com Camarão">Peixe no Tucupi com Camarão</td>
        <td width="20"><input type="text" name="num_refeicao[]" size="7"></td>

    </tr>
        <tr>
        <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Escabeche de Pirarucu">Escabeche de Pirarucu</td>
        <td width="20"><input type="text" name="num_refeicao[]" size="7"></td>

    </tr>
        <tr>
        <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Escabeche de Tambaqui">Escabeche de Tambaqui</td>
        <td width="20"><input type="text" name="num_refeicao[]" size="7"></td>

    </tr>
        <tr>
     <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Escabeche de Tucunaré">Escabeche de Tucunaré</td>
        <td width="20"><input type="text" name="num_refeicao[]" size="7"></td>

    </tr>
        <tr>
        <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Tucunaré Frito">Tucunaré Frito</td>
        <td width="20"><input type="text" name="num_refeicao[]" size="7"></td>

    </tr>
        <tr>
        <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Sardinha Frita">Sardinha Frita</td>
        <td width="20"><input type="text" name="num_refeicao[]" size="7"></td>

    </tr>
        <tr>
        <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Jaraqui Frito">Jaraqui Frito</td>
        <td width="20"><input type="text" name="num_refeicao[]" size="7"></td>

    </tr>
        <tr>
        <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Pacu Frito">Pacu Frito</td>
        <td width="20"><input type="text" name="num_refeicao[]" size="7"></td>

    </tr>
        <tr>
        <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Filé de Pirarucu Frito">Filé de Pirarucu Frito</td>
        <td width="20"><input type="text" name="num_refeicao[]" size="7"></td>

    </tr>
        <tr>
        <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Filé de Pirarucu a Milanesa">Filé de Pirarucu a Milanesa</td>
        <td width="20"><input type="text" name="num_refeicao[]" size="7"></td>

    </tr>
        <tr>
        <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Guisado de Pirarucu">Guisado de Pirarucu</td>
        <td width="20"><input type="text" name="num_refeicao[]" size="7"></td>

    </tr>
    <tr>
       <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Caldeirada No Tucupi com Camarao">Caldeirada No Tucupi com Camarão</td>
        <td width="20"><input type="text" name="num_refeicao[]" size="7"></td>

    </tr>
    <tr>
       <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Caldinho de Tambaqui">Caldinho de Tambaqui</td>
        <td width="20"><input type="text" name="num_refeicao[]" size="7"></td>

    </tr>
    <tr>
      <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Feijão">Feijão</td>
        <td width="20"><input type="text" name="num_refeicao[]" size="7"></td>

    </tr>
    <tr>
     <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Arroz">Arroz</td>
        <td width="20"><input type="text" name="num_refeicao[]" size="7"></td>

    </tr>
    <tr>
     <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Farofa">Farofa</td>
        <td width="20"><input type="text" name="num_refeicao[]" size="7"></td>

    </tr>
    <tr>
     <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Vinagre">Vinagre</td>
        <td width="20"><input type="text" name="num_refeicao[]" size="7"></td>

    </tr>
    <tr>
    <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Isca de Pirarucu">Isca de Pirarucu</td>
        <td width="20"><input type="text" name="num_refeicao[]" size="7"></td>

    </tr>
    <tr>
    <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Bolinho">Bolinho</td>
        <td width="20"><input type="text" name="num_refeicao[]" size="7"></td>

</table>
 <br>
<input class="btn" type="submit" value="Proximo" name="Proximo"> 
</form>

form 2

<?php
session_start();

error_reporting(-1);
ini_set('display_errors', 'On');

//Criar a conexao
$link = new mysqli ("localhost", "root", "", "peixaria");
if($link->connect_errno){
     echo"Nossas falhas local experiência ..";
     exit();
}

$sql= "SELECT MAX(m.id_pedido)+1 as ultimo FROM mpedido as m WHERE id_pedido ORDER BY m.id_pedido DESC LIMIT 1";

$resultado_id= mysqli_query($link,$sql);


$mesa = isset($_POST['numero_mesa']) ? $_POST[''] : '';
$_SESSION['numero_mesa'] = $mesa;

$pedido = isset($_POST['pedido_refeicao']) ? $_POST['pedido_refeicao'] : '';
$_SESSION['email'] = $pedido;

$bebida = isset($_POST['num_refeicao']) ? $_POST['num_refeicao'] : '';
$_SESSION['num_refeicao'] = $bebida;

?>

    <main>

<div class="pedidos">
<form method="post" action="relatorio.php">
<table >
    <tr>
   <tr>
        <td colspan="4" width="100">
         <span>Mesa</span>
         <input type="text" id="numero_mesa" name="numero_mesa">
         <label>
                    <span>Comanda:</span>
                    <?php
                    $row = mysqli_fetch_array($resultado_id);
                     echo '<span>'.$row['ultimo'].'</span>';
                    ?>

                </label>
                <br>
                <br>
        </td>

    </tr>
    <tr>

        <td width="280"><input type="checkbox" name="pedido_bebida[]" value="Fanta Laranja 1l">Fanta Laranja 1l</td>
        <td width="20"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
    <tr>

        <td width="280"><input type="checkbox" name="pedido_bebida[]" value="Fanta Laranja 2l">Fanta Laranja 2l</td>
        <td width="20"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
        <tr>

        <td width="280"><input type="checkbox" name="pedido_bebida[]" value="Cola Cola 1l">Cola Cola 1l</td>
        <td width="20"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
        <tr>

        <td width="280"><input type="checkbox" name="pedido_bebida[]" value="Cola Cola 2l">Cola Cola 2l</td>
        <td width="20"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
        <tr>

        <td width="280"><input type="checkbox" name="pedido_bebida[]" value="Bare 2l">Bare 2l</td>
        <td width="20"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
        <tr>

        <td width="280"><input type="checkbox" name="pedido_bebida[]" value="Fanta Uva">Fanta Uva</td>
        <td width="00"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
        <tr>

        <td width="280"><input type="checkbox" name="pedido_bebida[]" value="Fanta Laranja">Fanta Laranja</td>
        <td width="00"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
        <tr>

        <td width="280"><input type="checkbox" name="pedido_bebida[]" value="Sprit">Sprit</td>
        <td width="00"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
        <tr>

        <td width="280"><input type="checkbox" name="pedido_bebida[]" value="Cola Cola">Cola Cola</td>
        <td width="20"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
        <tr>

        <td width="280"><input type="checkbox" name="pedido_bebida[]" value="Cola Cola zero">Cola Cola zero</td>
        <td width="20"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
        <tr>

        <td width="280"><input type="checkbox" name="pedido_bebida[]" value="Guaraná Antarctica">Guaraná Antarctica</td>
        <td width="20"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
        <tr>

        <td width="280"><input type="checkbox" name="pedido_bebida[]" value="Guaraná Baré">Guaraná Baré</td>
        <td width="20"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
        <tr>

        <td width="280"><input type="checkbox" name="pedido_bebida[]" value="Suco Manga">Suco Manga</td>
        <td width="20"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
        <tr>

        <td width="280"><input type="checkbox" name="pedido_bebida[]" value="Suco Pessego">Suco Pessego</td>
        <td width="00"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
        <tr>

        <td width="280"><input type="checkbox" name="pedido_bebida[]" value="Suco Goiaba">Suco Goiaba</td>
        <td width="00"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
    <tr>

        <td width="280"><input type="checkbox" name="pedido_bebida[]" value="Suco Uva">Suco Uva</td>
        <td width="00"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
    <tr>

        <td width="280"><input type="checkbox" name="pedido_bebida[]" value="Suco Maracujá">Suco Maracujá</td>
        <td width="00"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
    <tr>

        <td width="280"><input type="checkbox" name="pedido_bebida[]" value="Suco Laranja">Suco Laranja</td>
        <td width="20"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
    <tr>

        <td width="280"><input type="checkbox" name="pedido_bebida[]" value="Suco Caju">Suco Caju</td>
        <td width="20"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
    <tr>

        <td width="280"><input type="checkbox" name="pedido_bebida[]" value="Agua Mineral">Agua Mineral</td>
        <td width="20"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
    <tr>

        <td width="280"><input type="checkbox" name="pedido_bebida[]" value="Agua com Gas">Agua com Gas</td>
        <td width="20"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
    <tr>

        <td width="280"><input type="checkbox" name="pedido_bebida[]" value="Cerveja em Lata">Cerveja em Lata</td>
        <td width="20"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
    <tr>
        <td width="280"><input type="checkbox" name="pedido_bebida[]" value="Limonada Natural">Limonada Natural</td>
        <td width="20"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
    <tr>

</table>
 <br>
<button class="btn" type="submit">Fazer Pedido</button> 
</form>
</div>



    </main>

and mysql

<?php
error_reporting(-1);
ini_set('display_errors', 'On');

//Criar a conexao
$link = new mysqli ("localhost", "root", "", "peixaria");
if($link->connect_errno){
     echo"Nossas falhas local experiência ..";
     exit();
}

       if($_POST)
{  
    $numero_mesa = $_POST['numero_mesa'];
    $pedido_refeicao = implode(', ', $_POST['pedido_refeicao']);
    $num_refeicao = implode(', ', $_POST['num_refeicao']);
    $pedido_bebida = implode(', ', $_POST['pedido_bebida']);
    $num_bebida = implode(', ', $_POST['num_bebida']);
    $sql="INSERT INTO mpedido(numero_mesa,pedido_refeicao,num_refeicao,pedido_bebida,num_bebida) VALUES('$numero_mesa','$pedido_refeicao','$num_refeicao','$pedido_bebida','$num_bebida')";
        $resultado_pedido = mysqli_query($link,$sql);
}


         $sql= "SELECT id_pedido,numero_mesa,pedido_refeicao,num_refeicao,pedido_bebida,num_bebida,data FROM mpedido ORDER BY id_pedido DESC LIMIT 1";
        $consulta = mysqli_query($link,$sql);

?>
  • Poxa Allan... although it is difficult for me(pq Naum am expert) I will try to help...

  • Thank you very much @Magichat

  • Let’s try to divide the problem into parts.... What are the forms you want to work on in this action ? Note that I see 2 Forms but none have a name, they have 2 different actions 1-doir2.php and 2.php report, what exactly do you want to do ? Here I’ll come out but I’ll be back...

  • they are Do.php and do 2.php and in the report send to mysql and list to show on screen .

  • Right, and where is Fazer.php ?

  • @Magichat is the first code . the second is to make 2.php and the third is the report.php

  • What data do you want to send from 1st to 2nd ? and where do you want to receive this data in 2nd form ?... Or what you want is to fill the first press next, fill the 2nd and send to the bank ?

  • well I wish to send the data from form one to 2 and from two to mysql , and I will select several checkbox and input number

  • So what I’m not getting is if you want to send the form 1 data to form2, how do you imagine that form2 will receive this data ? form2 will display the Form1 data? I’m not understanding the relation between Form1 and form2...

Show 4 more comments

2 answers

1

Cara I recommend using Ajax with Jquery, function $.post(), it is more "beautiful", if you do not want to use, do the following, give the action of formula1.php to formula2.php.
Within your formula2.php you will have $_POST available.
Do it:

var_dump($_POST);

Once done, take the $_POST data, like this:

input type="text" name="name" value="$_POST['value']"

Now in the inputs of your formula2.php just call the values. $_POST['value']

Now giving the Submit, goes to mysql.
I recommend using var_dump in your formule2 to guide you :D


-General edition, I will summarize

This is your 1° form

<div class="pedidos">
<form method="post" action="fazer2.php">

After giving Ubmit, you go to: do 2.php

//Dê var_dump no arquivo fazer2.php
var_dump($_POST); //irá mostrar os valores disponíveis do formulário 1

Now in doi2.php do this:

<input type="tipo_do_seu_input" name="nome_do_input" value="$_POST['valores_desejados_do_primeiro_formulario']">

- Now the user will fill out form 2
- Giving the Submit, within the 2.php, goes to your Mysql code

Please note: I know you want a Session, but it is not recommended (by me :D) for this, my tip is easier. Until!

  • I don’t quite understand how I can do with your answer .

1


Man I find it much safer to use $_SESSION with openssl.
I save everything in Sesssion and I will elaborate the form according to what I have in Sesssion so I can have several formulations on the same page without leaving it all using $_POST as a shipping mode and all this is analyzed with regex for each field and placed in $_SESSION.

A scheme of how I do:

if(!empty($_SESSION[NOMEDASESSAO]['contato']['status']) OR $_SESSION[NOMEDASESSAO]['contato']['status'] != 1){

if($_SESSION[NOMEDASESSAO]['contato']['nome'] == null){
Formulario();
} elseif($_SESSION[NOMEDASESSAO]['contato']['email'] == null){
Formulario();
} elseif($_SESSION[NOMEDASESSAO]['contato']['assunto'] == null){
Formulario();
} elseif($_SESSION[NOMEDASESSAO]['contato']['texto'] == null){
Formulario();
} elseif($_SESSION[NOMEDASESSAO]['contato']['erros']['texto'] == 1){
Formulario();
}else{
$_SESSION[NOMEDASESSAO]['contato']['ok'] = 1;
Formulario();
}
}

The Form() mounts according to the data unfolding. Only if everything is right is that the system makes a new form with the captcha field. There is no problem if at this point the person tries to change something and click send because all fields go through the regex at each push of button. Simple, fast and easy.
I use this on the contact page (I have no e-mail contact to avoid spams), partnership and follow the same principle for the login page, remember password, registration and everything I want to do purely in PHP without having to use Javascript.
Good studies kid!

Browser other questions tagged

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