Variable passed from another page is missing after Submit the form

Hi guys,

I have two variables that come from a page1 'dianum' and 'mesnum', on page2 everything displays fine even if I echo the value of the variables but when I click on submit button appears this error:
Notice: Undefined index: dianum in C:\inetpub\wwwroot\Intranet2014\aplicaciones\citas\admin_update.php on line 10
Notice: Undefined index: mesnum in C:\inetpub\wwwroot\Intranet2014\aplicaciones\citas\admin_update.php on line 11

The idea is to display the values of some data base fields and updated as needed.

this is my code and hope anybody can help me. Thank you in advance!

<?php
session_start();
$nombreusuario = $_SESSION['sesion_nombre'];
$deptousuario = $_SESSION['sesion_departamento'];
$username = $_SESSION['sesion_usuario'];
$DBhost = "xxxxxx";
$DBuser = "xxxxxxxxxx";
$DBpass = "xxxxxxxxxxx";
$DBname = "xxxxxxxx";
$dia = sprintf("%02d", $_GET['dianum']);
$mes = sprintf("%02d", $_GET['mesnum']);
$ano="2015";
$odd = "-odd";
$h1 = $h2 = $h3 = $h4 = "0";
?>
<HTML>
<form action="<?PHP echo $_SERVER['PHP_SELF'] ?>" method="POST">

<table width="1000" border="0" align="center">
	<tr>
    <td><a href="//intralan/index.php"><img src="//intralan/Imagenes/topbaner.jpg" width="1000" height="88" border="0" align="top"></a></td>
  	</tr>
</table>
<table width="1000" border="0" align="center">
	<tr>
    <td class="style22"><div align="right" class="style23 style22 style26"><em><?PHP echo $deptousuario . " - " . $nombreusuario; ?></em></div></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td><div align="center"><span class="style23">Administrac&oacute;n de Citas para Documentaci&oacute;n del Turno Vespertino </span></div></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    </tr>
</table>
<table width="1000" border="0" align="center">
</table>
<table width="1050" border="1" align="center">
	<tr bgcolor="#009900">
	<td width="50"><div align="center"><span class="style72"><span class="style73">Día</span></span></div></td>
	<td width="50"><div align="center"><span class="style63">4:00 4:15</span></div></td>
	<td width="50"><div align="center"><span class="style63">4:15 4:30</span></div></td>
	<td width="50"><div align="center"><span class="style63">4:30 4:45</span></div></td>
	<td width="50"><div align="center"><span class="style63">4:45 5:00</span></div></td>
	</tr>
	<?php
	$mysqli = new mysqli($DBhost, $DBuser, $DBpass, $DBname);
	if ($mysqli->connect_errno) {
		printf("No fue posible conectarse a la base de datos: %s\n", $conn->connect_error);
		exit();
	}else{
		$result = mysqli_query($mysqli,"SELECT * FROM control_citas WHERE ano = '$ano' && mes = '$mes' && dia = '$dia' ORDER BY ano, mes, dia ASC");
		if (!$result) { 
			echo "NO SE ENCONTRARON REGISTROS PARA ESTE MES";
			exit();
		}else{
			while($row = mysqli_fetch_array($result)) {
				?>
			  	<tr bgcolor="#009900">
				<td><div align="center" class="style74"><?php echo ($row['dia']); ?></div></td>
	        	<td><div align="right"><input name="d01" type="text" id="d01" size="2" maxlength="2" value="<?php echo ($row['h0400_0415']); ?>"></div></td>
				<td><div align="right"><input name="d02" type="text" id="d02" size="2" maxlength="2" value="<?php echo ($row['h0415_0430']); ?>"></div></td>
				<td><div align="right"><input name="d03" type="text" id="d03" size="2" maxlength="2" value="<?php echo ($row['h0430_0445']); ?>"></div></td>
		    	<td><div align="right"><input name="d04" type="text" id="d04" size="2" maxlength="2" value="<?php echo ($row['h0445_0500']); ?>"></div></td>
				<?php
			}
			?>
			<td><div align="right"><input name="Submit" type="submit" id="Submit" value="Actualizar"></div></td>
			</tr>
			</table>
			<?php
		}
		$mysqli->close();
	}	
if(isset($_POST['Submit'])) {	
	$h1=($_POST["d01"]);
	$h2=($_POST["d02"]);
	$h3=($_POST["d03"]);
	$h4=($_POST["d04"]);
	$mysqli = new mysqli($DBhost, $DBuser, $DBpass, $DBname);
	if ($mysqli->connect_errno) {
		printf("No fue posible conectarse a la base de datos: %s\n", $conn->connect_error);
		exit();
	}else{
		$sql = "UPDATE control_citas SET h0400_0415 = '$h1', h0415_0430 = '$h2', h0430_0445 = '$h3', h0445_0500 = '$h4' WHERE ano = '$ano' && mes = '$mes' && dia = '$dia';";
		if ($mysqli->query($sql) === TRUE) {
			echo "La actualización fue exitosa";
		}else{
			echo "Error: no se actualizó la información";
		}
	}
	$mysqli->close();
}
?>
</form>
</body>
</html>

Open in new window

elm9999Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

elm9999Author Commented:
Lines 10 and 11 are this:
     $dia = sprintf("%02d", $_GET['dianum']);
     $mes = sprintf("%02d", $_GET['mesnum']);
I don't get any error when the form is displayed and the variables have the right values but I get the error after submit the form.
Dave BaldwinFixer of ProblemsCommented:
Since you are using POST for the form, there won't be any $_GET variables after submitting it.  And I don't see those variables anywhere else in that code so I don't see that they are being passed with the submit.
elm9999Author Commented:
Well,

On page1 I send the variables like this:
echo ('<td><font face="Arial, Helvetica, sans-serif" size="2"><div align="center"><a href = "admin_update.php?dianum=' . $row["dia"] . '&mesnum=' . $row["mes"] . '" target="_blank">' . $row["dia"] . '</font></a></div></td>');

Page2 displays good and the dianum and mesnum have the right values that i'm using to make the first query, until here, there is no problem, no error, but why after submit the variables doesn't exist?

What do I have to change?
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

Dave BaldwinFixer of ProblemsCommented:
Variables do not 'persist'.  They have to be resent for each page.  And since you are using POST for the form on the second page, you would have to include them in the form (you could use a 'hidden' input) and use $_REQUEST to pick them up from both pages.  $_REQUEST includes both $_GET and $_POST variables.
http://php.net/manual/en/reserved.variables.request.php
Chris StanyonWebDevCommented:
On line 17 of your code, you're setting the form action to PHP_SELF, which will only set it to the filename part of the URL - it'll drop the querystring (which is where your GET variables come from)

Change that to REQUEST_URI and when the form submits, it will include the querystring and your GET variables will work fine.

<form action="<?PHP echo $_SERVER['REQUEST_URI'] ?>" method="POST">

Open in new window


Bear in mind that mixing POST and GET (or using REQUEST) can get you into all sorts of trouble - POST takes precedence over GET, and you can find yourself getting all mixed up - it's also very easy to fudge a GET request - just type a new querystring into the browser!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
PHP external variables are documented here:
http://php.net/manual/en/reserved.variables.php

An introductory tutorial that teaches how to use the variables is available here:
http://php.net/tut.php

The nature of HTTP requests, here:
http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol

This is all stuff we have to understand in order to be able to write web applications.  If you read those resources and are still unsure about how to present and respond to a request, please post back and we'll try to help.
NerdsOfTechTechnology ScientistCommented:
Replace lines 10-11 with:
if(isset($_GET['dianum'])) {
   $dia = sprintf("%02d", $_GET['dianum']);
}
if(isset($_GET['mesnum'])) {
   $mes = sprintf("%02d", $_GET['mesnum']);
} 

Open in new window



The two errors are due to sprintf() not recognizing the GET reference on the initial run of the script (as GET variables  appear be set after the initial run according to your process description). By checking to see if the GET variables are set first, the error is eliminated.
elm9999Author Commented:
Guys thank you so much,

Sorry for comment until now, in my work I have to do a lot of things not only coding and to tell the truth, I'm learning and coding PHP at the same time, I was do it in php 4 but little by little I am recoding the apps.

Anyway the solution was modifying this:

<form action="<?PHP echo $_SERVER['REQUEST_URI'] ?>" method="POST">
 
and
 
if(isset($_GET['dianum'])) {
   $dia = sprintf("%02d", $_GET['dianum']);
}
if(isset($_GET['mesnum'])) {
   $mes = sprintf("%02d", $_GET['mesnum']);
}

Thank you so much Ray, Dave, Chris & Nerds.
elm9999Author Commented:
Thank you :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.