How to keep value of selected value after form submission? - php

I know there are a lot of questions related to it but believe me, no solution is working for me.
<form action="index" method="post" >
<select id="s" name="dtype" class="dropdown" required
style="float: left; text-align: left;width: 20%; margin: 10px;">
<option value="400">Select Data Type</option>
<option value="401">Current</option>
<option value="402">Voltage</option>
<option value="403">kWh</option>
</select>
</form>
What I have tried
I have tried the following
<option <?php if ($_GET['dtype'] == '401') { ?>selected="true" <?php }; ?>value="401">Current</option>
<option <?php if ($_GET['dtype'] == '402') { ?>selected="true" <?php }; ?>value="402">Voltage</option>
<option <?php if ($_GET['dtype'] == '402') { ?>selected="true" <?php }; ?>value="402">kwh</option>
Tried below solution
Keep values selected after form submission
PHP keep dropdown value after submit
All the solution gives me unidentified index: dtype error
Any help would be highly appreciated.

In your form you have: method="post"
And then you use the $_GET['dtype']
Either change the method of the form to method="get" or change the if conditions to check for $_POST['dtype'].
That's why it says unidentified index. You are sending the data as post and trying to get it from the $_GET array.
Apart from this, if you are using xhtml the attribute value should be selected="selected" whereas if you use an html 5 doctype it should be just selected.

The first option (Select Data Type) must have an empty value in order for "required" option to work.
<form action="index.php" method="POST">
<select id="s" name="dtype" class="dropdown" required style="...">
<option value="">Select Data Type</option>
<option value="401" <?php if(isset($_POST['dtype']) && $_POST['dtype']=="401") {echo'selected';} ?>>Current</option>
<option value="402" <?php if(isset($_POST['dtype']) && $_POST['dtype']=="402") {echo'selected';} ?>>Voltage</option>
<option value="403" <?php if(isset($_POST['dtype']) && $_POST['dtype']=="403") {echo'selected';} ?>>kWh</option>
</select>
</form>

Related

How to display text on dropdown select

I'm trying to do something very simple in PHP, but keep getting an error message. Essentially, when someone selects "Cat", I want "Miaow" to appear.
My idea was:
<select name="demo">
<option value="Dog">Dog</option>
<option value="Cat">Cat</option>
<option value="Fish">Fish</option>
</select>
<?php if ($_POST['demo'] == 'Cat') { echo 'Miaow'; } ?>
However, in PHPFiddle,
I get 'E_NOTICE : type 8 -- Undefined index...'
as soon as the code runs. Am I missing something basic? Thanks!
Your form might be passing data by $_GET instead of $_POST.
Did you specify the method ?
<form method="post" action="index.php">
<select name="demo">
<option value="Dog">Dog</option>
<option value="Cat">Cat</option>
<option value="Fish">Fish</option>
</select>
<input type="submit" value="Submit">
</form>
You can var_dump($_POST); and var_dump($_GET); to see what those variables contains in your PHP file.
Or you can do it in javascript like this :
function animal(value) {
if(value == "Cat") {
document.getElementById("myAnimal").innerHTML = "Miaouw";
} else {
document.getElementById("myAnimal").innerHTML = "Rooooah";
}
}
<form action="#">
<select name="demo" onchange="animal(this.value)">
<option value="Dog">Dog</option>
<option value="Cat">Cat</option>
<option value="Fish">Fish</option>
</select>
</form>
<span id="myAnimal"></span>
Is it even possible to do this using PHP so that "Miaow" comes up automatically on select, rather than having to submit the form?
You are looking for JavaScript code, not PHP. Here is a jQuery example:
$(document).on('change', '.animals', function(){
$('.noise-target').html( $('option:selected', this).data('noise') );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="animals">
<option>Select</option>
<option data-noise="Woof">Dog</option>
<option data-noise="Meow">Cat</option>
</select>
<div class="noise-target"></div>
Maybe this will help, I write some block as far as I understand...
<form action="#" method="post">
<select name="demo">
<option value="Dog">Dog</option>
<option value="Cat">Cat</option>
<option value="Fish">Fish</option>
</select>
<input type="submit" name="sub" />
</form>
<?php
if(isset($_POST['sub'])){
if($_POST['demo'] == "cat"){
echo "Miao";
}
}
?>

HTML Forms: The value attribute doesn't change what's sent to php

so I'm having problems with forms sending data to my php script.
My html is:
<form method="get" action="form.php">
<select name="course">
<option value"COMM1">COMM1111</option>
<option value"COMM2">COMM2222</option>
<option value"COMM3">COMM3333</option>
<option value"COMM4">COMM4444</option>
</select>
</form>
What's sent to form.php when I do
$_GET['course']
is the inner text of the chosen option.
So choosing COMM1111 gives me COMM1111 instead of COMM1
In your option tag you forgot your equal sign for value
<option value = "COMM1">COMM1111</option>
You are missing the = sign in your HTML for the value of the options.
Consider the following code which produces the values you are looking for ::
HTML:
<form method="get">
<select name="course">
<option value="COMM1">COMM1111</option>
<option value="COMM2">COMM2222</option>
<option value="COMM3">COMM3333</option>
<option value="COMM4">COMM4444</option>
</select>
<input type='submit'>
</form>
PHP:
<?php
if(!empty($_GET)){
print_r($_GET);
}
?>

How do I Pass Values from One Form to Another?

I have this form:
<form name="summaryform">
<select name="category" id="category" style="width:500px;">
<option value="">Choose category...</option>
<option value="ActionScript">ActionScript</option>
<option value="AppleScript">AppleScript</option>
<option value="Asp">Asp</option>
<option value="BASIC">BASIC</option>
<option value="C">C</option>
<option value="C++">C++</option>
<option value="Clojure">Clojure</option>
<option value="COBOL">COBOL</option>
<option value="ColdFusion">ColdFusion</option>
<option value="Erlang">Erlang</option>
<option value="Fortran">Fortran</option>
<option value="Groovy">Groovy</option>
</select><br>
<select name="subcategory" id="subcategory" style="width:500px;">
<option value="">Choose sub category...</option>
<option value="Haskell">Haskell</option>
<option value="Java">Java</option>
<option value="JavaScript">JavaScript</option>
<option value="Lisp">Lisp</option>
<option value="Perl">Perl</option>
<option value="PHP">PHP</option>
<option value="Python">Python</option>
<option value="Ruby">Ruby</option>
<option value="Scala">Scala</option>
<option value="Scheme">Scheme</option>
</select><br>
<input type="text" name="comments" id="comments" value=" Enter request comments..." onfocus="clearText(this)" onblur="restoreText(this)" style="width:493px;color:#999;font-size:9pt;height:20px;">
On the form above, I have two dropdowns (name="categoryId" and "subcategoryid") and one textboxt (name="comments")
How do I pass the values from this form to another form?
Let's say below is the form I want to pass the values to:
<div>
<p>
<form name="summaryform" method='POST' action='process.php'>
<div style="font-size:12pt;">
value from one dropdown
value from the other dropdown
value from textbox
</form>
</p>
</div>
jQuery Method
If the forms are on the same page, you can use jQuery to get the values and write to the div:
var content = $('#category').val()+'<br>'+$('#subcategory').val()+'<br>'+$('#comments').val();
$('#div-to-write-to').html(content);
PHP Method
If the forms are on different pages, you will need to use PHP and the $_POST[] variable to pass the values you are looking for.
NOTE: for this method to work the page being POSTed to MUST be PHP. You will also need to add an action and method in the first form to POST to the second form.
This would be the code in the form on the page being POSTed to:
<?php echo $_POST['category'].'<br>'.$_POST['subcategory'].'<br>'.$_POST['comments']; ?>
A value can be passed between forms using the POST and GET methods. The first form (Which is sending the values) does not have this defined, so modify the first form to this -
<form name="summaryform" method='POST' action='process.php'>
Then, in process.php, put the following php code -
$dropdown_first = $_POST['category'];
$dropdown_second= $_POST['subcategory'];
$comment = $_POST['comments'];
After that, you can just use the variables as you see fit!
If you want just print the selected values from first form #zsaa14's
answer is good
If you want to print whole select list, autoselected your value, heres a code.
foreach($categories as $category) {
$selected = $category['name'] === $_POST['category'] ? 'selected="selected"' : '';
echo " <option value="{$category['name']}" {$selected}>{$category['name']}</option>" ;
}
NOTE: the variable names and keys are for example, use Yours. This is only fragment of code.
You can use post data fields to send the values to the next form

How to select the dropdownlist value and validate it

I have created the dropdown list in my index page i want to select the one value from that list and validate it if not selected any value the code for that is as follows:
<form action="" method="post">
<select value="state" name="state">
<option selected="">---please enter---</option>
<option value="1">andhra</option>
<option value="2">thamil</option>
<option value="3">kerela</option>
</select>
</form>
php code for the above file is as follows:
<?php
if(!empty($_POST['state'])) {
$state = $_POST['state'];
}
else {
echo "required";
}
?>
I dont want to be select the first option in selection list please enter to be selected but the code which I have used is taking that value also I want relevant code how to validate that list?
I prefer adding a disabled attribute to the placeholder option, that way, a user has to choose something if they click the drop-down:
<select value="state" name="state">
<option selected disabled>---please enter---</option>
<option value="1">andhra</option>
<option value="2">thamil</option>
<option value="3">kerela</option>
</select>
Quick demo: http://jsfiddle.net/hxxJZ/
something like this should to the trick:
<form action="" method="post">
<select name="state">
<option value="0" selected>---please enter---</option>
<option value="1">andhra</option>
<option value="2">thamil</option>
<option value="3">kerela</option>
</select>
</form>
php
<?php
if( 0 != $_POST['state'] ) {
$state = $_POST['state'];
}
else {
echo "required";
}
?>
your HTML is not right,
try edit this line
<option selected=""...
to this one
<option selected value="">---
select elements do not have an (HTML) value attribute, so remove value="state".
The selected attribute should be selected="selected" (or just SELECTED).

How to return a URL and submit the form using onchange

Firstly, there may be a similar question answered amoungst the site but after carefully considering what solutions and problems I'm still stumped as to what to do.
I'm looking to return a URL and post my users to a page of the chosen URL from a select list.
Currently it works using the normal submit button using the following code;
<form id="formurl" name="formurl" method="post" action="" onSubmit="return getURL(this.url.value)">
<!-- begin postcode list -->
<select size="1" name="url" id="posctode_srch" onchange="this.form.submit()">
<option value="e1-office-space/">E1</option>
<option value="e7-office-space/">E7</option>
<option value="e10-office-space/">E10</option>
<option value="e11-office-space/">E11</option>
<option value="e14-office-space/">E14</option>
<option value="e15-office-space/">E15</option>
<option value="e16-office-space/">E16</option>
<option value="e17-office-space/">E17</option>
<option value="ec1-office-space/">EC1</option>
<option value="ec2-office-space/">EC2</option>
<option value="ec3-office-space/">EC3</option>
<option value="ec4-office-space/">EC4</option>
</select>
<input type="image" src="images/graphics/new/btn_go.png" class="go_btn" width="119" border="0" value="submit" onclick="_gaq.push(['_trackEvent', 'Contact', 'AreaSearchForm',,, false]);" alt="Area search submit" />
</form>
I'm looking to use post but I am testing it with get to see what result is created when choosing an option. when the form is set to get i'm seeing ?url=e7-office-space%2F so am I safe to assume it's working to some degree but there is something missing for the output to just take the value of the option and nothing more?
Being new to this kind of stuff i'm guessing it's a php / html problem as the functionality is there it's just a misused element somewhere so if anyone can help me see where i'm going wrong i'd really appreciate it.
Thanks
You have three things -
setting the url
setting the google stuff
submitting the form if not in the submit event
In the code below you can omit the image if you can rely on javascript only
DEMO
window.onload=function() {
document.getElementById("formurl").onsubmit=function() {
var val = this.url.value;
if (val=="") {
alert("Please select a value");
return false;
}
this.action="http://bing.com/search?q="+val;
_gaq.push(['_trackEvent', 'Contact', 'AreaSearchForm',,, false]);
}
document.getElementById("posctode_srch").onchange=function() {
var val = this.value;
if (val=="") return;
this.form.onsubmit(); // not 100% sure but this should not trigger twice
this.form.submit(); // since this is not triggering onsubmit
}
}
using
<form id="formurl" name="formurl" method="post" action="">
<!-- begin postcode list -->
<select size="1" name="url" id="posctode_srch">
<option value="">Please select</option>
<option value="e1-office-space/">E1</option>
<option value="e7-office-space/">E7</option>
<option value="e10-office-space/">E10</option>
<option value="e11-office-space/">E11</option>
<option value="e14-office-space/">E14</option>
<option value="e15-office-space/">E15</option>
<option value="e16-office-space/">E16</option>
<option value="e17-office-space/">E17</option>
<option value="ec1-office-space/">EC1</option>
<option value="ec2-office-space/">EC2</option>
<option value="ec3-office-space/">EC3</option>
<option value="ec4-office-space/">EC4</option>
</select>
<input type="image" src="images/graphics/new/btn_go.png"
class="go_btn" width="119" border="0" value="submit"
alt="Area search submit" />
</form>

Categories