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.
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.
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
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" />
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];
....
}
That's all.
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"> </td>
</tr>
<tr>
<td bgcolor="#006A00" align="center"><span class="style42">ENERO</spa n></span>< /td>
</tr>
</table>
<table width="420" border=1 align="center">
<TR bgcolor="#008800">
<TD width="60" ALIGN=center><span class="style42"><strong>DO M</strong> </span></T D>
<TD width="60" ALIGN=center><span class="style42"><strong>LU N</strong> </span></T D>
<TD width="60" ALIGN=center><span class="style42"><strong>MA R</strong> </span></T D>
<TD width="60" ALIGN=center><span class="style42"><strong>MI E</strong> </span></T D>
<TD width="60" ALIGN=center><span class="style42"><strong>JU E</strong> </span></T D>
<TD width="60" ALIGN=center><span class="style42"><strong>VI E</strong> </span></T D>
<TD width="60" ALIGN=center><span class="style42"><strong>SA B</strong> </span></T D>
</TR>
<TR>
<TD ALIGN=center bgcolor="#BBBBBB"><span class="style45"></span></T D>
<TD ALIGN=center bgcolor="#BBBBBB"><span class="style45"></span></T D>
<TD ALIGN=center bgcolor="#BBBBBB"><span class="style45"></span></T D>
<TD ALIGN=center bgcolor="#BBBBBB"><span class="style45"></span></T D>
<?php
//------------------------ -------DIA 1 PARA DIBUJAR EL CALENDARIO
if(substr("$dia1",0,1) == "N") {
?><TD ALIGN=center bgcolor="#666666"><span class="style46">1</a></spa n></TD><?p hp
}else{
?><td bgcolor="#BBBBBB"><span class="style46"><div align="center"<label><inpu t 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></spa n></TD><?p hp
}else{
?><td bgcolor="#BBBBBB"><span class="style46"><div align="center"<label><inpu t 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></spa n></TD><?p hp
}else{
?><td bgcolor="#BBBBBB"><span class="style46"><div align="center"<label><inpu t 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['RadioGrou p1'])){
$diaseleccionado = $_POST['RadioGroup1'];
//*********************BUS CA 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,"SEL ECT * FROM control_citas WHERE ano = '$ano' && mes = '$mes' && dia = '$diaseleccionado'");
if ($resultY) {
while($rowY = mysqli_fetch_array($result Y)) {
$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);
}
}
<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"> </td>
</tr>
<tr>
<td bgcolor="#006A00" align="center"><span class="style42">ENERO</spa
</tr>
</table>
<table width="420" border=1 align="center">
<TR bgcolor="#008800">
<TD width="60" ALIGN=center><span class="style42"><strong>DO
<TD width="60" ALIGN=center><span class="style42"><strong>LU
<TD width="60" ALIGN=center><span class="style42"><strong>MA
<TD width="60" ALIGN=center><span class="style42"><strong>MI
<TD width="60" ALIGN=center><span class="style42"><strong>JU
<TD width="60" ALIGN=center><span class="style42"><strong>VI
<TD width="60" ALIGN=center><span class="style42"><strong>SA
</TR>
<TR>
<TD ALIGN=center bgcolor="#BBBBBB"><span class="style45"></span></T
<TD ALIGN=center bgcolor="#BBBBBB"><span class="style45"></span></T
<TD ALIGN=center bgcolor="#BBBBBB"><span class="style45"></span></T
<TD ALIGN=center bgcolor="#BBBBBB"><span class="style45"></span></T
<?php
//------------------------
if(substr("$dia1",0,1) == "N") {
?><TD ALIGN=center bgcolor="#666666"><span class="style46">1</a></spa
}else{
?><td bgcolor="#BBBBBB"><span class="style46"><div align="center"<label><inpu
}
//------------------------
if(substr("$dia2",0,1) == "N") {
?><TD ALIGN=center bgcolor="#666666"><span class="style46">2</a></spa
}else{
?><td bgcolor="#BBBBBB"><span class="style46"><div align="center"<label><inpu
}
//------------------------
if(substr("$dia3",0,1) == "N") {
?><TD ALIGN=center bgcolor="#666666"><span class="style46">3</a></spa
}else{
?><td bgcolor="#BBBBBB"><span class="style46"><div align="center"<label><inpu
}
?> </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['RadioGrou
$diaseleccionado = $_POST['RadioGroup1'];
//*********************BUS
$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,"SEL
if ($resultY) {
while($rowY = mysqli_fetch_array($result
$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);
}
}
ASKER
Sorry, the post above is ok let me put it again.
ASKER
This is part of my table:
if(substr("$dia29",0,1) == "N") {
?><TD ALIGN=center bgcolor="#666666"><span class="style46">29</a></sp an></TD><? php
}else{
?><td bgcolor="#BBBBBB"><span class="style46"><div align="center"<label><inpu t type="radio" name="RadioGroup1" value="29">29</label></spa n></td><?p hp
}
//------------------------ -------DIA 30 PARA DIBUJAR EL CALENDARIO
if(substr("$dia30",0,1) == "N") {
?><TD ALIGN=center bgcolor="#666666"><span class="style46">30</a></sp an></TD><? php
}else{
?><td bgcolor="#BBBBBB"><span class="style46"><div align="center"<label><inpu t type="radio" name="RadioGroup1" value="30">30</label></spa n></td><?p hp
}
//------------------------ -------DIA 31 PARA DIBUJAR EL CALENDARIO
if(substr("$dia31",0,1) == "N") {
?><TD ALIGN=center bgcolor="#666666"><span class="style46">31</a></sp an></TD><? php
}else{
?><td bgcolor="#BBBBBB"><span class="style46"><div align="center"<label><inpu t type="radio" name="RadioGroup1" value="31">31</label></spa n></td><?p hp
}
This is where I submit the form
<?php
if(isset($_POST['Submit']) ) {
if(isset($_POST['RadioGrou p1'])){
$diaseleccionado = $_POST['RadioGroup1'];
echo "Testing-1....... " . "$diaseleccionado" . "<br>"; // // HERE MY VARIABLE STILL HAS THE VALUE I CHOOSE
//*********************BUS CA 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,"SEL ECT * FROM control_citas WHERE ano = '$ano' && mes = '$mes' && dia = '$diaseleccionado'");
if ($resultY) {
while($rowY = mysqli_fetch_array($result Y)) {
…….
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</d iv></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><inpu t type="radio" name="RadioGroup2" value="0400_0415">4:00 - 4:15</label></div></td><?p hp
}
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><inpu t type="radio" name="RadioGroup2" value="0415_0430">4:15 - 4:30</label></div></td><?p hp
}
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['RadioGrou p2'])){
$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);
if(substr("$dia29",0,1) == "N") {
?><TD ALIGN=center bgcolor="#666666"><span class="style46">29</a></sp
}else{
?><td bgcolor="#BBBBBB"><span class="style46"><div align="center"<label><inpu
}
//------------------------
if(substr("$dia30",0,1) == "N") {
?><TD ALIGN=center bgcolor="#666666"><span class="style46">30</a></sp
}else{
?><td bgcolor="#BBBBBB"><span class="style46"><div align="center"<label><inpu
}
//------------------------
if(substr("$dia31",0,1) == "N") {
?><TD ALIGN=center bgcolor="#666666"><span class="style46">31</a></sp
}else{
?><td bgcolor="#BBBBBB"><span class="style46"><div align="center"<label><inpu
}
This is where I submit the form
<?php
if(isset($_POST['Submit'])
if(isset($_POST['RadioGrou
$diaseleccionado = $_POST['RadioGroup1'];
echo "Testing-1....... " . "$diaseleccionado" . "<br>"; // // HERE MY VARIABLE STILL HAS THE VALUE I CHOOSE
//*********************BUS
$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,"SEL
if ($resultY) {
while($rowY = mysqli_fetch_array($result
…….
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</d
</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><inpu
}
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><inpu
}
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['RadioGrou
$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);
ASKER
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
Thank you in advance guys
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
ASKER
Thank you so much Marco :)
<input type="hidden" value="<?php echo $diaseleccionado; ?>" name="diaseleccionado" />
This was solution. Wonderful!!!
<input type="hidden" value="<?php echo $diaseleccionado; ?>" name="diaseleccionado" />
This was solution. Wonderful!!!
ASKER
Very good Job!!!
Thank you.
Thank you.
Glad to help you. Cheers
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.