RGriffiths
RGriffiths

Reputation: 5970

Select tag to set a session variable

I have a web page written in html such that it displays the results of a query in the form of a list. Each record includes a link which when pressed opens a pdf report for that record.

I would like to be able to allow the user the option of choosing what might be included and have used a <select> tag at the top of the page to enable them to make a choice (in the example "Include comments in the report")

enter image description here

Is it possible for a session variable to be set so that each time the <select> is picked the session variable takes the value of the selection, enabling me to then know what needs to be included when the pdf document is created?

If not, are there any ways of achieving what I am trying to achieve?

This is an edited version of the code that is used to create the download link is:

Include comments in report
<br>
<select value="selectedReport">
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>


<?php
while($row = mysql_fetch_array($result))
{
?>
<tr>
<td>
<form name="createPdf" action="https://...createpdf.php" method="Post" target="_blank">
<input name="pdfID" size="0" type="submit" value="<?php echo $row['pdfID'] ?>" class="pdfButton">
</form>
</td>
</tr>
<?php
}
?>

Upvotes: 1

Views: 4737

Answers (2)

RGriffiths
RGriffiths

Reputation: 5970

I have found a really good solution to this and so thought I would share it:

HTML - the script sends a request to setSessionVariable.php when the select chosen

<script language="JavaScript" type="text/javascript">
function takeValue()
{
var sel = document.getElementById("selectedReport");
var selectedReport = sel.options[sel.selectedIndex].text;
var xmlhttp;

if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
}

urlString="https://website/setSessionVariable.php?choiceSelected=" + selectedReport;
xmlhttp.open("GET",urlString,true);
xmlhttp.send();
}
</script>

The select tag - when option is chosen the takeValue function is called.

<select id="selectedReport" onchange="takeValue()">
<option value="Yes" Selected>Yes</option>
<option value="No" >No</option>
</select>

setSessionVariable.php - runs in the background when select is chosen and sets session variable $selected.

<?php
session_start();
if (isset($_GET[choiceSelected])) 
{
$_SESSION['$selected']=$_GET[choiceSelected];   
}

?>

Upvotes: 1

cнŝdk
cнŝdk

Reputation: 32145

If you're redirecting to a new Page using PHP you just need to use $_POST['selectedReport'] in your php file:

$selectedReport = $_POST['selectedReport'];

And your select should be like this:

<select name="selectedReport">
   <option value="1">Report1</option>
   <option value="2">Report2</option>
   <option value="3">Report3</option>
</select>

And if you are in the same page you can do it with Javascript:

var sel = document.getElementById("selectedReport");

//to get the value of the selected option
var selectedReport = sel.options[sel.selectedIndex].value;

//to get the text of the selected option
var selectedReport = sel.options[sel.selectedIndex].text;

And your select Should be like this:

<select id="selectedReport">
   <option value="1">Report1</option>
   <option value="2">Report2</option>
   <option value="3">Report3</option>
</select>

EDIT:

Answering to your comment: you can use a hidden input inside your Form like this:

<form action="..." method="POST">
<input type="hidden" id="myReport" value=""/>
//... and the content of your form goes here
</form>

And use the following javascript function to put the selected option in the hidden input value so you can submit it with your form:

function takeValue(){
var sel = document.getElementById("selectedReport");
var selectedReport = sel.options[sel.selectedIndex].text;
//put the selected value in the hidden input value
document.getElementById("myReport").value=selectedReport;
}

And apply it using the onchange event of your select like this:

<select id="selectedReport" onchange="takeValue()">
   <option value="1">Report1</option>
   <option value="2">Report2</option>
   <option value="3">Report3</option>
</select>

And the value will be submitted with the form.

Upvotes: 1

Related Questions