Link to home
Start Free TrialLog in
Avatar of Member_2_6479049
Member_2_6479049

asked on

How save a variable value after submit button is clicked?

Hi guys,

My page displays a table with days of the month, on each day there is a radio button, so the user clicks on the radio button of the day he select, once the day is selected, I save the number of the selected day in a variable $day,  then,  he click on the submit button to display another table that contains hours of the day.

The problem is when the submit button is clicked, the variable $day becomes empty.

I'm using this action:

<form action="<?PHP echo $_SERVER['PHP_SELF'] ?>" method="post">

Please guys can you help me on how to save the value of my variable after the submit button is clicked.

Thank you.
Avatar of Brian Tao
Brian Tao
Flag of Taiwan, Province of China image

The action should be correct if you are handling the submit in the same PHP script.
Please post the code where you submit the form, and the PHP part where you handles the submit.  There's nothing we can do by just looking at that single line of your form tag.
You say you're saving the day in a variable, but ho do you're doing it? With javascript? Becaque if you're using only php, before to save the value you must submit the form.
Let's say you have this page with the day of the month: each radio button must be called uing notation array
<input type="radio" name="day[]" value="1" />

Open in new window

When the form is submitted ( side note: don't use $_SERVER[PHP_SELF]; just drop the action property and you'll get the same result without security issues), when the form is submitted, then you'll can check if the user has selected a day and which day has been selected:
//check if there is a radio button value submitted
//because if a radio is not checked it is not submitted and it doesn't appear in $_POST array
if (isset($_POST['day']))
{
  //here you can ave the day in a variable
  $day = $_POST['day'][0];
  ....
}

Open in new window

That's all.
Avatar of Member_2_6479049
Member_2_6479049

ASKER

THIS IS THE FORM ACTION

<form action="<?PHP echo $_SERVER['PHP_SELF'] ?>" method="post">

THIS IS PART OF MY TABLE WHERE I'M USING THE RADIO BUTTONS
      <table width="420" border="0" align="center">
            <tr>
              <td align="center">&nbsp;</td>
              </tr>
            <tr>
          <td bgcolor="#006A00" align="center"><span class="style42">ENERO</span></span></td>
              </tr>
      </table>
      <table width="420" border=1 align="center">
            <TR bgcolor="#008800">
            <TD width="60" ALIGN=center><span class="style42"><strong>DOM</strong></span></TD>
             <TD width="60" ALIGN=center><span class="style42"><strong>LUN</strong></span></TD>
             <TD width="60" ALIGN=center><span class="style42"><strong>MAR</strong></span></TD>
             <TD width="60" ALIGN=center><span class="style42"><strong>MIE</strong></span></TD>
             <TD width="60" ALIGN=center><span class="style42"><strong>JUE</strong></span></TD>
             <TD width="60" ALIGN=center><span class="style42"><strong>VIE</strong></span></TD>
             <TD width="60" ALIGN=center><span class="style42"><strong>SAB</strong></span></TD>
            </TR>
            <TR>
            <TD ALIGN=center bgcolor="#BBBBBB"><span class="style45"></span></TD>
             <TD ALIGN=center bgcolor="#BBBBBB"><span class="style45"></span></TD>
             <TD ALIGN=center bgcolor="#BBBBBB"><span class="style45"></span></TD>
             <TD ALIGN=center bgcolor="#BBBBBB"><span class="style45"></span></TD>
            <?php
            //-------------------------------DIA 1 PARA DIBUJAR EL CALENDARIO
            if(substr("$dia1",0,1) == "N") {
                  ?><TD ALIGN=center bgcolor="#666666"><span class="style46">1</a></span></TD><?php
            }else{
                  ?><td bgcolor="#BBBBBB"><span class="style46"><div align="center"<label><input type="radio" name="RadioGroup1" value="1">1</label></span></td><?php
            }
            //-------------------------------DIA 2 PARA DIBUJAR EL CALENDARIO
            if(substr("$dia2",0,1) == "N") {
                  ?><TD ALIGN=center bgcolor="#666666"><span class="style46">2</a></span></TD><?php
            }else{
                  ?><td bgcolor="#BBBBBB"><span class="style46"><div align="center"<label><input type="radio" name="RadioGroup1" value="2">2</label></span></td><?php
            }
            //-------------------------------DIA 3 PARA DIBUJAR EL CALENDARIO
            if(substr("$dia3",0,1) == "N") {
                  ?><TD ALIGN=center bgcolor="#666666"><span class="style46">3</a></span></TD><?php
            }else{
                  ?><td bgcolor="#BBBBBB"><span class="style46"><div align="center"<label><input type="radio" name="RadioGroup1" value="3">3</label></span></td><?php
            }
            ?> </TR>
            <TR> <?php
      </table>
      <table width="280" border="0" align="center">
            <tr>
          <td><div align="center">
            <input type="submit" name="Submit" value="Asignar horario">
          </div></td>
              </tr>
      </table>
      <?php
BEFORE SUMBIT THE VAR $diaseleccionado HAS THE VALUE I CHOOSE
      if(isset($_POST['Submit'])) {
            if(isset($_POST['RadioGroup1'])){
                  $diaseleccionado = $_POST['RadioGroup1'];
                  //*********************BUSCA horarios********************************
                  $mysqliY = new mysqli($DBhost, $DBuser, $DBpass, $DBname);
                  if ($mysqliY->connect_errno) {
                        printf("No fue posible conectarse a la base de datos: %s\n", $conn->connect_error);
                        exit();
                  }else{
                        $resultY = mysqli_query($mysqliY,"SELECT * FROM control_citas WHERE ano = '$ano' && mes = '$mes' && dia = '$diaseleccionado'");
                        if ($resultY) {
                              while($rowY = mysqli_fetch_array($resultY)) {
                                    $v0400_0415 = $rowY['h0400_0415'];
                                    $v0415_0430 = $rowY['h0415_0430'];
                                    $v0430_0445 = $rowY['h0430_0445'];
                                    $v0445_0500 = $rowY['h0445_0500'];
                                    $v0500_0515 = $rowY['h0500_0515'];
                                    $v0515_0530 = $rowY['h0515_0530'];
                                    $v0530_0545 = $rowY['h0530_0545'];
                                    $v0545_0600 = $rowY['h0545_0600'];
                                    $v0600_0615 = $rowY['h0600_0615'];
                                    $v0615_0630 = $rowY['h0615_0630'];
                                    $v0630_0645 = $rowY['h0630_0645'];
                                    $v0645_0700 = $rowY['h0645_0700'];
                                    $v0700_0715 = $rowY['h0700_0715'];
                                    $v0715_0730 = $rowY['h0715_0730'];
                                    $v0730_0745 = $rowY['h0730_0745'];
                                    $v0745_0800 = $rowY['h0745_0800'];
                                    $v0800_0815 = $rowY['h0800_0815'];
                                    $v0815_0830 = $rowY['h0815_0830'];
                                    $v0830_0845 = $rowY['h0830_0845'];
                                    $v0845_0900 = $rowY['h0845_0900'];
                              }
                        mysqli_close($mysqliY);
                  }
            }
Sorry, the post above is ok let me put it again.
This is part of my table:
            if(substr("$dia29",0,1) == "N") {
                  ?><TD ALIGN=center bgcolor="#666666"><span class="style46">29</a></span></TD><?php
            }else{
                  ?><td bgcolor="#BBBBBB"><span class="style46"><div align="center"<label><input type="radio" name="RadioGroup1" value="29">29</label></span></td><?php
            }
            //-------------------------------DIA 30 PARA DIBUJAR EL CALENDARIO
            if(substr("$dia30",0,1) == "N") {
                  ?><TD ALIGN=center bgcolor="#666666"><span class="style46">30</a></span></TD><?php
            }else{
                  ?><td bgcolor="#BBBBBB"><span class="style46"><div align="center"<label><input type="radio" name="RadioGroup1" value="30">30</label></span></td><?php
            }
            //-------------------------------DIA 31 PARA DIBUJAR EL CALENDARIO
            if(substr("$dia31",0,1) == "N") {
                  ?><TD ALIGN=center bgcolor="#666666"><span class="style46">31</a></span></TD><?php
            }else{
                  ?><td bgcolor="#BBBBBB"><span class="style46"><div align="center"<label><input type="radio" name="RadioGroup1" value="31">31</label></span></td><?php
            }
This is where I submit the form
      <?php
      if(isset($_POST['Submit'])) {
            if(isset($_POST['RadioGroup1'])){
                  $diaseleccionado = $_POST['RadioGroup1'];
                  echo "Testing-1....... " . "$diaseleccionado" . "<br>"; // // HERE MY VARIABLE STILL HAS THE VALUE I CHOOSE
                  //*********************BUSCA horarios********************************
                  $mysqliY = new mysqli($DBhost, $DBuser, $DBpass, $DBname);
                  if ($mysqliY->connect_errno) {
                        printf("No fue posible conectarse a la base de datos: %s\n", $conn->connect_error);
                        exit();
                  }else{
                        $resultY = mysqli_query($mysqliY,"SELECT * FROM control_citas WHERE ano = '$ano' && mes = '$mes' && dia = '$diaseleccionado'");
                        if ($resultY) {
                              while($rowY = mysqli_fetch_array($resultY)) {
                              …….
This is another table that is displayed after the first Submit button
At this point there is no problem

            echo "Testing-2....... " . "$diaseleccionado"; // HERE MY VARIABLE STILL HAS THE VALUE I CHOOSE
            ?>
            <table width="1004" border="0" align="center">
                  <tr>
                  <td bgcolor="#339933"><div align="center">HORARIOS</div></td>
                  </tr>
            </table>
            <table width="1000" border="1" align="center">
            <tr>
            <?php
            if("$put0400_0415" == "N") {
                  ?><TD height="30" ALIGN=center bgcolor="#666666">4:00 - 4:15</TD><?php
            }else{
                  ?><td bgcolor="#E1FFE1"><div align="center"<label><input type="radio" name="RadioGroup2" value="0400_0415">4:00 - 4:15</label></div></td><?php
            }
            if("$put0415_0430" == "N") {
                  ?><TD height="30" ALIGN=center bgcolor="#666666">4:15 - 4:30</TD><?php
            }else{
                  ?><td bgcolor="#E1FFE1"><div align="center"<label><input type="radio" name="RadioGroup2" value="0415_0430">4:15 - 4:30</label></div></td><?php
            }
Here is the second Submit where my variable lose the value
<?php
echo "Testing-3....... " . "$diaseleccionado" . "<br>"; // HERE MY VARIABLE STILL HAS THE VALUE I CHOOSE
if(isset($_POST['Submit2'])) {
      echo "Testing-4....... " . "$diaseleccionado" . "<br>"; // HERE MY VARIABLE LOST THE VALUE
      if(isset($_POST['RadioGroup2'])){
            $horaseleccionada = $_POST['RadioGroup2'];
             $conn = mysqli_connect($DBhost, $DBuser, $DBpass, $DBname);
             if (!$conn) {
                 die("Connection failed: " . mysqli_connect_error());
            }            
            $sql = "INSERT INTO transacciones (nombre, paterno, materno, telefono, tramite, diatramite, mestramite, anotramite, horatramite, fechaproceso, horaproceso, usuarioproceso) VALUES ('$nombrecliente', '$paternocliente', '$maternocliente', '$telefonocliente', '$tramitecliente', '$diaseleccionado', '$mes', '$ano', '$horaseleccionada', '$fecha_proceso', '$hora_proceso', '$username')";
            
            if (mysqli_query($conn, $sql)) {
                echo "New record created successfully";
            } else {
                echo "Error: " . $sql . "<br>" . mysqli_error($conn);
            }
            
            mysqli_close($conn);
Actually, When I press the first submit button, the variable retains the value, but at the moment I press the second submit, the variable lose the value, so I need to retain the value after the second submit.

Thank you in advance guys
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
My only doubt is that you say that in testing 3 the variable has its value and in Testin4 it doesn't, but I suppose you're meaning that the variable has its value before you click submit, isn't it?
Thank you so much Marco :)

<input type="hidden" value="<?php echo $diaseleccionado; ?>" name="diaseleccionado" />

This was solution. Wonderful!!!
Very good Job!!!

Thank you.
Glad to help you. Cheers