...

View Full Version : session variables



jake66
01-07-2012, 10:01 PM
Hi,

I have a form that uses javascript to, from the value of a select box, show only one of 5 possible date fields with a different mini-calendar attached, keeping the other 4 hidden. These are labelled date1, date2 and so on.

Firstly, I would like so help with making sure that if the user changes the value of the select box, there by changing the date field displayed that any other fields which would at this point be hidden are cleared so only the one currently visible would be submitted.

Then then want to post the value of whichever date box has been filled in to another page using sessions and make the recieving page recognise it and assign the posted value to a variable.

I realise that to do this, I will probably need to either:
-Change the name of the text box with data in before or after posting it so my $sentdate = $_POST['date']; command works.
or
-Have a script the recognises which of the 5 boxes has a value in it and post only that one and assign it to a variable.

I realise that this may be quite a complex code but I am grateful for any input you can give.

I post my code for the first page below in case it is helpful as you will see I have not put in the popup calendars yet.

Thanks :)




<?php session_start();
include "auth.php";
?>
<script language="javascript">
function ShowMenu(num, menu, max)
{
//num is selected value, menu is the name of the div, max is the number of divs
for(i = 1; i <= max; i++){
//add number onto end of menu
var menu_div = menu + i;

//if current show
if(i == num) {
document.getElementById(menu_div).style.display = 'block';
} else {
//if not, hide
document.getElementById(menu_div).style.display = 'none';
}
}
}
</script>
<body>
<centre>
<p><a href="logout.php">Logout</a></p>
<p><img src="Header.png" alt="Header" width="449" height="254" align="middle" /></p>
</centre>
<p>Please select an option:</p>
<form id="form1" name="form1" method="post" action="\logrequest.php">
<p>
<select id='service'
onChange="javascript: ShowMenu(document.getElementById('service').value,'divdate', 5);">
<option value='0'>Please select...
<option value='1'>White
<option value='2'>Black
<option value='3'>Red
<option value='4'>Green
<option value='5'>Blue
</select>
<br />
<div id='divdate1' style="display: none;">
Please select the date you would like:<br /> <br />
Date:
<input name="date1" type="text" id="date1" /> <br>
</div>
</p>
<div id='divdate2' style="display: none;">
Please select the date you would like:<br />
<br />
Date:
<input name="date2" type="text" id="date2" /> <br>
</div>
<div id='divdate3' style="display: none;">
Please select the date you would like:<br />
<br />
Date:
<input name="date3" type="text" id="date3" /> <br>
</div>
<div id='divdate4' style="display: none;">
Please select the date you would like:<br />
<br />
Date:
<input name="date4" type="text" id="date4" /> <br>
</div>
<div id='divdate5' style="display: none;">
Please select the date you would like:<br />
<br />
Date:
<input name="date4" type="text" id="date4" /> <br>
</div>
<br>
<input type="submit" value="Continue">
</form>
<? include ("footer.html")
?>
</body>
</html>

sunfighter
01-08-2012, 07:31 PM
Then then want to post the value of whichever date box has been filled in to another page using sessionsI did not do this. You have a post method form that has been passing variable for centuries and works every time, why over complicate things?

You call your footer with a php short tag <? Does not work on most servers. Get in the habit of using <?php to open php.

I removed the two php areas because they are not needed for this example.

The <option> tag needs a </option> tag to close it. ADDED

Your code works. But the call to your javascript is a little complicated and so I simplified where I could. You can find or you know the values that you sent so I just eliminated them.

I have also add code to show the second file reading the date input POSTs.

Main page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script type="text/javascript">
function clearup()
{
document.getElementById('divdate1').style.display = 'none';
document.getElementById('date1').value = '';
document.getElementById('divdate2').style.display = 'none';
document.getElementById('date2').value = '';
document.getElementById('divdate3').style.display = 'none';
document.getElementById('date3').value = '';
document.getElementById('divdate4').style.display = 'none';
document.getElementById('date4').value = '';
document.getElementById('divdate5').style.display = 'none';
document.getElementById('date5').value = '';
return;
}
function ShowMenu()
{
clearup();
divdate = document.form1.service.selectedIndex;
thediv = 'divdate'+ divdate;
document.getElementById(thediv).style.display = 'block';
}
</script>
</head>
<body>
<div>
<p><a href="logout.php">Logout</a></p>
<p><img src="Header.png" alt="Header" style="text-align: center;" /></p>
</div>
<p>Please select an option:</p>

<form id="form1" name="form1" method="post" action="test_3.php"> <!--action="\logrequest.php">-->
<p>
<select id='service' onchange="ShowMenu();">
<option value='0'>Please select...</option>
<option value='1'>White</option>
<option value='2'>Black</option>
<option value='3'>Red</option>
<option value='4'>Green</option>
<option value='5'>Blue</option>
</select>
</p>
<br />
<div id='divdate1' style="display: none;width:225px;height:100px;background-color:pink;">
Please select the date you would like:<br /><br />
Date:<input name="date1" type="text" id="date1" />
</div>
<div id='divdate2' style="display: none;width:225px;height:100px;background-color:black;color:white;">
Please select the date you would like:<br /><br />
Date:<input name="date2" type="text" id="date2" />
</div>
<div id='divdate3' style="display: none;width:225px;height:100px;background-color:red;">
Please select the date you would like:<br /><br />
Date:<input name="date3" type="text" id="date3" />
</div>
<div id='divdate4' style="display: none;width:225px;height:100px;background-color:green;">
Please select the date you would like:<br /><br />
Date:<input name="date4" type="text" id="date4" />
</div>
<div id='divdate5' style="display: none;width:225px;height:100px;background-color: #0066CC;">
Please select the date you would like:<br /><br />
Date:<input name="date5" type="text" id="date5" />
</div>
<br />
<input type="submit" value="Continue" />
</form>
</body>
</html>

The form action="test_3.php" should be changed back by you. Here is the code I used in test_3.php:

<?php

if($_POST["date1"] != '')
$date = $_POST["date1"];
if($_POST["date2"] != '')
$date = $_POST["date2"];
if($_POST["date3"] != '')
$date = $_POST["date3"];
if($_POST["date4"] != '')
$date = $_POST["date4"];
if($_POST["date5"] != '')
$date = $_POST["date5"];

echo $_POST["date1"] . 'IAM 1<br />';
echo $_POST["date2"] . 'IAM 2<br />';
echo $_POST["date3"] . 'IAM 3<br />';
echo $_POST["date4"] . 'IAM 4<br />';
echo $_POST["date5"] . 'IAM 5<br />';
echo $date;
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum