Solved

How save a variable value after submit button is clicked?

Posted on 2015-02-09
11
122 Views
Last Modified: 2015-02-10
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.
0
Comment
Question by:elm9999
  • 6
  • 4
11 Comments
 
LVL 9

Expert Comment

by:Brian Tao
ID: 40599726
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.
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 40600129
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.
0
 

Author Comment

by:elm9999
ID: 40601029
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);
                  }
            }
0
 

Author Comment

by:elm9999
ID: 40601045
Sorry, the post above is ok let me put it again.
0
 

Author Comment

by:elm9999
ID: 40601154
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);
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:elm9999
ID: 40601174
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
0
 
LVL 30

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 40601196
The problem is that in your second table, where you click submit for the second time, you have to put a hidden input with the $diaseleccionado so that this variable ibe submitted with the rest of values

//immediately before the submit button
<input type="hidden" value="<?php echo $diaseleccionado; ?>" name="diaseleccionado" />

Open in new window

0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 40601198
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?
0
 

Author Comment

by:elm9999
ID: 40601245
Thank you so much Marco :)

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

This was solution. Wonderful!!!
0
 

Author Closing Comment

by:elm9999
ID: 40601248
Very good Job!!!

Thank you.
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 40601249
Glad to help you. Cheers
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now