Hello, I'm a newb in PHP and webprogramming actually ..
anyways I'm trying to store a session variable when a user selects the drop down menu and pressing a submit button.
any idea? or direction to point me?
Hello, I'm a newb in PHP and webprogramming actually ..
anyways I'm trying to store a session variable when a user selects the drop down menu and pressing a submit button.
any idea? or direction to point me?
How far have you got?
<form action="Main.php">
<select name="Domain_Choice">
<?php
foreach($_SESSION['DomainList'] as $DomainName)
{
echo "<option value=$DomainName>$DomainName</option>";
}
?>
</select>
<input type="button" value="Select_Domain" OnClick= >
</form>
not sure what I can write after the OnClick
I wanna be able to store what ever is chosen in the drop box
to $_SESSION['CurrentDomain']
<form action="Main.php">
<select name="Domain_Choice">
<?php
foreach($_SESSION['DomainList'] as $DomainName)
{
echo "<option value=$DomainName>$DomainName</option>";
}
?>
</select>
<input type="button" value="Select_Domain" OnClick="sessionset()">
</form>
</td>
</tr>
</table>
</body>
<?php
function sessionset()
{
$_SESSION['CurrentDomain'] = $Domain_Choice;
}
?>
Tried this, doesnt seem to work
Well you can't onClick() to a PHP function .. that is for client side scripting.
Try something list this:
<form name="form1" action="main.php" method="post">
<select name="Domain_Choice">
<?php
foreach($_SESSION['DomainList'] as $DomainName)
{
echo "<option value=$DomainName>$DomainName</option>";
}
?>
</select>
<input type="submit" name="submit" value="Select_Domain">
</form>
</td>
</tr>
</table>
</body>
File: main.php
<?php
$_SESSION['CurrentDomain'] = $_POST['Domain_Choice'];
?>
Hi TenZor,
I've only just noticed SpanKie's reply but I'd nearly finished myself so you can have this too ... it's a slightly different way, but the crucial point was made by SpanKie.
Yes, your problem is that you can't 'call' PHP from the browser.
PHP only is working on the server ... it 'delivers' a page to the browser, but can't affect the page afterwards.
By contrast, javascript (which would go in the 'onClick') works at the client-side, i.e. the browser and that has no access at all to the server! So you can't 'call' PHP with javascript.
What you have to do is:
1) Submit the form along with the choice back to the SAME page
2) Set the session variable at the server
3) Re-display the form
Here's a template ...
<?php
// Every time the page is called, we check for a 'domain change'
// which only occurs when a user clicks the submit button on the
// form below, in which case $_GET['Select_Domain'] is 'set' (see the comment below).
if(isset($_GET['Select_Domain'])){
$_SESSION['CurrentDomain'] = $_GET['Domain_Choice'];
}
?>
<html><head><title>Form</title></head>
<body>
<?php
// Either display the current domain or a message
if(isset($_SESSION['CurrentDomain']){
echo '<h1>Current domain: '.$_SESSION['CurrentDomain'].'</h1>';
} else {
echo '<h1>Select a domain</h1>';
}
?>
<form action="<?=$_SERVER['PHP_SELF']?>">
<select name="Domain_Choice">
<?php
foreach($_SESSION['DomainList'] as $DomainName){
echo '<option value="'.$DomainName.'">'.$DomainName.'</option>';
}
?>
</select>
<!-- Must submit the 'select' value back to the page'
along with the name of the submit button 'Select_Domain'
which will be accessible as $_GET['Select_Domain'] (see the note above) -->
<input type="submit" value="Select_Domain">
</form>
</body>
</html>
thanks everyone! I got it completed