I asked this question already but have since over hauled the code following the help I got. I am trying to make my php drop menu sticky but it clears to the top menu item every time after the submit button is pressed. I am not sure where I am going wrong so any help is greatly appreciated. Code as follows:
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
</head>
<body>
<?php
if (isset($_POST['Question']))
{
$menuVar = $_POST['fontFamily'];
}
?>
<p id="info-req">How did you find about this site?</p>
<form name="TestMenu" method="post" id="marketing">
<select name="Question">
<option <?php if($menuVar=="----------") echo 'selected="selected"'; ?> value="----------">----------</option>
<option <?php if($menuVar=="WebSearch") echo 'selected="selected"'; ?> value="WebSearch">Web Search</option>
<option <?php if($menuVar=="SocialMedia") echo 'selected="selected"'; ?> value="SocialMedia">Social Media</option>
<option <?php if($menuVar=="Wordofmouth") echo 'selected="selected"'; ?> value="Wordofmouth">Word of mouth</option>
<option <?php if($menuVar=="Other") echo 'selected="selected"'; ?> value="Other">Other</option>
</select>
<input type="submit" />
</form>
</body>
</html>
First of all, there is no $POST['fontFamily'] variable in your form. Why are you trying to use it?
You should use $_POST['Question'] in order to get this value.
So it should be:
if (isset($_POST['Question']))
{
$menuVar = $_POST['Question'];
}
Also you should init $menuVar if there's no $POST in order not to get a Notice: Undefined variable $menuVar. So in the end you code should be:
if (isset($_POST['Question']))
{
$menuVar = $_POST['Question'];
} else {
$menuVar = "----------";
}
Related
This question already has answers here:
Keep values selected after form submission
(12 answers)
Closed 4 months ago.
I have the selection menu which takes the value from the txt file. I want the selected value to remain selected even after the form submission.
<?php
$filename = 'select.txt';
$eachlines = file($filename, FILE_IGNORE_NEW_LINES);
?>
<form action="#" method="post">
<select id="toolchain" name="toolchain" onchange='this.form.submit()'>
<option selected value="base">Please Select</option>
<?php foreach($eachlines as $lines){
echo "<option value='".$lines."'>$lines</option>";
}?>
</select>
</form>
For keeping the value selected,I tried this:
<?php foreach($eachlines as $lines){
echo "<option value='".$lines."'" if($_POST['$lines']) echo $_POST['$lines'];">$lines</option>";
}?>
But it is not working, may be I am using echo inside the echo. Please correct me.
You can use this code. Although I haven't tested the code but let me share my logic with you to make you a better understanding of it. I have added a condition that if the form is submitted then it should display the form with $_POST['toolchain'] selected otherwise it should display it in normal ways.
<?php
$filename = 'select.txt';
$eachlines = file($filename, FILE_IGNORE_NEW_LINES);
if(isset($_POST['toolchain'])
{
?>
<form action="#" method="post">
<select id="toolchain" name="toolchain" onchange='this.form.submit()'>
<option selected value="<?php echo $_POST['toolchain']; ?>"><?php echo $_POST['toolchain']; ?></option>
<?php foreach($eachlines as $lines){
if($lines!=$_POST['toolchain'])
echo "<option value='".$lines."'>$lines</option>";
}?>
</select>
</form>
<?php }
else{
?>
<form action="#" method="post">
<select id="toolchain" name="toolchain" onchange='this.form.submit()'>
<option selected value="base">Please Select</option>
<?php foreach($eachlines as $lines){
echo "<option value='".$lines."'>$lines</option>";
}?>
</select>
</form>
<?php }
?>
It's a normal but a lengthy way. You can also add a condition inside form. If the form is submitted mean if(isset($_POST['toolchain'])) then you can make the option selected for the $_POST['toolchain'] inside foreach loop
Update
I have put the condition inside the form. Kindly update if you face any error as I haven't tested the code
<?php
$filename = 'select.txt';
$eachlines = file($filename, FILE_IGNORE_NEW_LINES);
?>
<form action="#" method="post">
<select id="toolchain" name="toolchain" onchange='this.form.submit()'>
<?php if(isset($_POST['toolchain']))
{
?>
<option value="base">Please Select</option>
<?php foreach($eachlines as $lines){
if($_POST['toolchain']==$lines))
{
echo "<option selected value='".$lines."'>$lines</option>";
}
else {
echo "<option value='".$lines."'>$lines</option>";
}
}
}
else {
?>
<option selected value="base">Please Select</option>
<?php foreach($eachlines as $lines){
echo "<option value='".$lines."'>$lines</option>";
}
}
?>
</select>
</form>
I know this is asked question however I have tried almost steps but its not working. I know there is a silly mistake somewhere need an experts eye.
My code:
// if(isset($_POST["country"]))
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$country = $_POST["country"];
echo '<script language="javascript">';
echo "alert(' Officer Already Alloted..!!!');";///Tried getting alert once POST, but no message
echo '</script>';
}
//? $country = $_POST["country"] : $company=1;
?>
<form action="#" method="POST">
<select class="country" name="wcpbc-manual-country" id="country" >
<?
$list=mysqli_query($con,"select * from country where status!='False'");
while($row_list=mysqli_fetch_assoc($list)){
$display="+".$row_list['phonecode']."-".$row_list['name'];
$flag=$row_list['isosmall'];
?>
<!--<select class="country" name="wcpbc-manual-country" id="country">-->
<option value="<?$row_list['phonecode']?>" data-iconurl="https://ipdata.co/flags/<?php echo $flag; ?>.png" <?php if($country==$row_list['name']){echo "selected";} ?>><? echo $display;?></option>
<!--<option value="IN" data-iconurl="https://ipdata.co/flags/in.png">IN some text</option>-->
<?
}
?>
</select>
</form>
<script type="text/javascript">
$("#country").selectBoxIt();
</script>
Actually I am trying to get selected value on to dropdown, it seems POST is not working for assigning value to $country variable
I tried:
1. if($_SERVER['REQUEST_METHOD'] == "POST")
2. used form action="<?php echo $_SERVER['PHP_SELF']; ?>"
Please help.Thanks
The <option /> values are empty:
<?$row_list['phonecode']?>
Should be
<?php echo $row_list['phonecode']; ?>
or at least
<?= $row_list['phonecode']; ?>
Also as mentioned by Always Sunny you are not visibly submitting the form. We don't know if there is Javascript going on but there must be some kind of submit action. I think you checked that in the browsers developer console.
you should use
<select class="country" name="country" id="country" >
<option value="<?php echo $row_list['phonecode']?>" data-iconurl="https://ipdata.co/flags/<?php echo $flag; ?>.png" <?php if($country==$row_list['name']){echo "selected";} ?>><? echo $display;?></option>
Rajesh I succeeded to submit the page using javascript onclick event like you told me but now for some reason the variables don’t get submitted to the javascript redirect page.
Can you tell me what I am doing wrong?
Thanks a lot for your help.
Here is my new code:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<!--For dropdown auto submit-->
<script>function onSelectChange(){
document.getElementById("myselect").submit();
}</script>
<!--For final page submit-->
<script>
function submit() {
window.location = "action_page.php";
}
</script>
</head>
<body>
<form action="test.php" method="post">
<select name="s1" style="width: 70px;" id="mySelect" onchange="this.form.submit()" </select>
<optgroup label="170011 Non-Isolated RS-422">
<option value="">Select...</option>
<option value="170011-3" <?php if ($_POST['s1'] == '170011-3') echo 'selected="selected"'; ?> >0011-3v3</option>
<option value="170011-5" <?php if ($_POST['s1'] == '170011-5') echo 'selected="selected"'; ?> >0011-5v0</option>
<optgroup label="170013 Isolated LVTTL Input">
<option value="">Select...</option>
<option value="170013-3" <?php if ($_POST['s1'] == '170013-3') echo 'selected="selected"'; ?> >0013-3v3</option>
<option value="170013-5" <?php if ($_POST['s1'] == '170013-5') echo 'selected="selected"'; ?> >0013-5v0</option>
<optgroup label="170015 Isolated LVDS Input">
<option value="">Select...</option>
<option value="170015-3" <?php if ($_POST['s1'] == '170015-3') echo 'selected="selected"'; ?> >0015-3v3</option>
<option value="170015-5" <?php if ($_POST['s1'] == '170015-5') echo 'selected="selected"'; ?> >0015-5v0</option>
<optgroup label="170016 Isolated RS-422 Input">
<option value="">Select...</option>
<option value="170016-3" <?php if ($_POST['s1'] == '170016-3') echo 'selected="selected"'; ?> >0016-3v3</option>
<option value="170016-5" <?php if ($_POST['s1'] == '170016-5') echo 'selected="selected"'; ?> >0016-5v0</option>
<optgroup label="170017 Non-Isolated LVTTL Input">
<option value="">Select...</option>
<option value="170017-3" <?php if ($_POST['s1'] == '170017-3') echo 'selected="selected"'; ?> >0017-3v3</option>
<option value="170017-5" <?php if ($_POST['s1'] == '170017-5') echo 'selected="selected"'; ?> >0017-5v0</option>
</select></form>
<!--<input type="submit" id="form_page" value="Submit">-->
<input type="submit" value="Submit Final" onclick="submit()">
</body>
</html>
The id attribute specifies a unique id for an HTML element (the value must be unique within the HTML document). You should give the different id to form tag and button tag.
You've gave same id in form tag and button tag. You've take two form tags and you've taken Submit button instead simple button. So html document only consider first form tag so when you click on button you will simply redirect to test.php
Now for redirect to another url using button you can use javascript onclick event like:
<input type="button" value="Submit" onclick="yourfunction">
and in function redirect using javascript like:
window.location = "http://www.example.com";
First php script -
<?php
session_start();
?>
<html>
<head>
</head>
<body><form method = "post">
<select name="feature" id="feature">
<?php
?>
<option value > Select Feature</option>
<?php
foreach($newFeature as $feat)
{
?>
<option value="<?php echo $feat;?>"><?php echo $feat;?></option>
<?php
}
?>
</select>
</form>
</body>
</html>
<?php
$_SESSION['feature'] = $feature;
?>
second php script -
<?php
session_start();
echo $_SESSION['feature'];
?>
When I run second php script, I get Array as echoed element instead of the element I selected.
What is wrong with the logic here ?
Please guide.
You have to submit the select. It is impossible to set $feature at that moment because the user hasn't yet selected anything.
<form method = "post">
<select name="feature" id="feature">
<option value > Select Feature</option>
<?php foreach($newFeature as $feat) : ?>
<option value="<?php echo $feat;?>" <?= $feat == $_SESSION['feature'] : " selected = 'selected'" : "" ?>><?php echo $feat;?></option>
<?php endforeach; ?>
</select>
<input type="submit" value="send" name="mySubmit" />
</form>
When you hit 'send' you can get the value by using $_POST['feature']; on the same page. If you want to go to another page you have to set the form action property.
session_start();
$_SESSION['feature'] = $_POST['feature'];
After the submit the page will 'reload'. Check if mySubmit is set and set the $_SESSION['feature'](don't forget to start your session at top of the page):
if (isset($_POST['mySubmit'])){
$_SESSION['feature'] = $_POST['feature'];
}
I'm just trying to get this simple php file to work:
<html>
<head>
<title>Aarons Editor</title>
</head>
<body>
<form action="index.php" method="get">
<select name="page">
<option value="default"> </option>
<option value="file">File</option>
</select>
<input type="submit">
</form>
<?php
if (page == $_GET['file']){
echo "<h1>File</h1>";
}
else {
echo "<h1>not file</h1>";
}
}
?>
</body>
</html>
Also, I can't figure out how to call a specific function in a form.
I think you were trying to do following
<html>
<head>
<title>Aarons Editor</title>
</head>
<body>
<!-- method="get" makes a form write attributes to url
ie. ?page=file or ?page=default
-->
<form action="index.php" method="get">
<select name="page">
<option value="default"> </option>
<option value="file">File</option>
</select>
<input type="submit">
</form>
<?php
// this checks if there is a parameter ?page set in url
if (isset($_GET['page'])) {
// this assigns a <select> value to variable $page
$page = $_GET['page'];
// this checks if selected value was 'file'
if ($page === "file") {
echo '<h1>'.$page.'</h1>';
} else { // this is if not
echo '<h1>not file</h1>';
}
}
?>
</body>
</html>
Try this, good sir
<html>
<head>
<title>Aarons Editor</title>
</head>
<body>
<form method="post">
<select name="page">
<option value="default"> </option>
<option value="file">File</option>
</select>
<input type="submit" name="gog">
</form>
<?php
if($_POST['gog']){
if ($_POST['page'] == 'file'){
echo "<h1>File</h1>";
}
else {
echo "<h1>not file</h1>";
}
}
?>
</body>
</html>