i got this variable:
$machine=isset($_POST['machine'])?$_POST['machine']:'';
for my selectbox:
<select id="machine" name="machine">
<option value=""></option>
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
<option value="4">D</option>
</select>
now i want to made some modifications. i have saved in my settings $favmachine=4.
for example i load the page with
if($favmachine!=0)
{
$machine=$favmachine;
}
my form looks like this (exactly what i want):
<select id="machine" name="machine">
<option value=""></option>
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
<option value="4" selected="selected">D</option>
</select>
the problem now is when i want to change for example from D to A it goes to D again.. right exactly what i wrote... but i dont know how to handle this problem
i want my $favmachine selected on pageload (works) BUT want to be able to change it to another machine and set this to $machine by $_POST
hope you understand my problem :D
I think you want to overwrite $machine with $favmachine only if the check for a POST value resulted in “nothing” before:
$machine=isset($_POST['machine'])?$_POST['machine']:'';
if($machine==='' && $favmachine!=0)
{
$machine=$favmachine;
}
or in shorter form, using the ? operator once more:
$machine = isset($_POST['machine']) ?
$_POST['machine']
:
( $favmachine != 0 ? $favmachine : '');
(Multiple lines and indentation for readability only.)
Related
I have a small Project website that I am learning to code with and in this website there is a Select Dropdown Menu of Music Genres
Here is a Video to help you visualise whats happening.
youtu.be/t4cJ7TCHiN8 (of the Problem)
And another video of how I can Paste "House" into the URL and get different MYSQL results
youtu.be/zf0S9WadQXo
I select a genre post the form and the reload shows me the results.
The value of the selected Option is then automatically selected by Jscript
So the Search area and its Values remain the same as before.
I have Mainly used DocumentGetElementById or jQuery to Select the value, and I am echoing PHP variables into the DocumentGetElementById function.
There is no problems with 99.9% of the values and selectable genres, they all are automatically selected without any problems.
Just 1 Problem, The value "House" Will not select.
No matter how I try it just will not select. I have of course tried other methods, including jQuery and no matter how I alter my code or method of execution.
Nothing seems to make the value "House" populate.
Here is the code. And images to help visualise the problem.
Thank you for any help you may provide.
CODE.
<select id='genregrab' name='genre' class='menusmall'>
<optgroup label='Genre'>
<option value="">All Genres</option>
<option value="EDM">EDM</option>
<option value="DnB">DnB</option>
<option value="Drum">Drum And Bass</option>
<option value="Breakbeat">Breakbeat</option>
<option value="Downtempo">Downtempo</option>
<option value="Dance">Dance</option>
<option value="Pop">Pop</option>
<option value="Disco">Disco</option>
<option value="Nu-Disco">Nu-Disco</option>
<option value="House">House</option>
<option value="Hard">Hard House</option>
<option value="Tech">Tech House</option>
<option value="Techno">Techno</option>
<option value="Deep">Deep House</option>
<option value="Future">Future House</option>
<option value="Tribal">Tribal House</option>
<option value="Tropical">Tropical House</option>
<option value="Progressive">Progressive</option>
<option value="Bass">Future Bass</option>
<option value="Bounce">Future Bounce</option>
<option value="Industrial">Industrial</option>
<option value="Electronic">Electronic</option>
<option value="Psychedelic">Psychedelic</option>
<option value="Trance">Trance</option>
<option value="Psy">Psy Trance</option>
<option value="Minimal">Minimal</option>
<option value="Ambient">Ambient</option>
<option value="Chillout">Chillout</option>
<option value="Synthwave">SynthWave</option>
<option value="Retro">Retro</option>
<option value="Hip">Hip-Hop</option>
<option value="Trip">Trip-Hop</option>
<option value="Glitch">Glitch-Hop</option>
<option value="Rap">Rap</option>
<option value="Afrobeat">AfroBeat</option>
<option value="Grime">Grime</option>
<option value="Trap">Trap</option>
<option value="Trapstep">TrapStep</option>
<option value="Dubstep">DubStep</option>
<option value="Drumstep">DrumStep</option>
<option value="Reggea">Reggea</option>
<option value="RnB">RnB</option>
<option value="Rock">Rock</option>
<option value="Metal">Metal</option>
<option value="Soul">Soul</option>
<option value="Country">Country</option>
<option value="Folk">Folk</option>
<option value="Jazz">Jazz</option>
<option value="Blues">Blues</option>
<option value="Funk">Funk</option>
<option value="World">World</option>
<option value="Gospel">Gospel</option>
<option value="Latin">Latin</option>
<option value="Ethnic">Ethnic</option>
<option value="Bollywood">BollyWood</option>
<option value="Moombahton">Moombahton</option>
<option value="Orchestral">Orchestral</option>
<option value="Classic">Classical</option>
<option value="Cinematic">Cinematic</option>
</optgroup>
</select>
Item Selected & POSTs Picked up By JScript to re-select the option
$(function(){
var genre = document.getElementById('genregrab');
var genredata = "<?php echo $genre ?>";
genre.value=genredata;
});
(Of course i initially tried the following)
document.getElementById("genregrab").value="<?php echo $genre ?>";
However unlike the others , the word "House" will not be selected.
I think its to do with the word or something, as every other word will work fine.
Same process just selecting another genre, and it works fine
As you can see , the Trance genre was selected
Just the word "House" has the problem.
And the Result after the Jscript
However , Select any other word and it works
As you can see in the image below
Any Ideas , Anybody?
I forgot to add the below code , from the GET and the MYSQL Results.
Its possible the sanitizing is effecting the variables.
The GET
if (isset($_GET['genre'])) {
$genre=$_GET['genre'];
$genre = filter_var($genre, FILTER_SANITIZE_STRING);
$genre = strip_tags($genre);
$genre = str_replace(['"',"'"], "", $genre);
} else {$genre="";}
And the MYSQL variable - Which also gets posted to the URL for GET
$genre=$row['genre'];
$genre = str_replace(['"',"'"], "", $genre);
if ($genre == "null" | $genre==" "){$genre="";}
$genresend = explode(',',trim($genre))[0];
I am using HTML + PHP to build a web App.
I wanted to create an editable form that includes a couple of <select> fields. As an example, we can assume that I want to create a user profile.
The thing is, when new user wants to edit some data about himself, all fields are empty. For the field sex it may look like this:
<select class="form-select" name="sex" required>
<option value="">-</option>
<option value="M">Male</option>
<option value="F">Female</option>
</select>
or:
<select class="form-select" name="sex" required>
<option selected value="">-</option>
<option value="M">Male</option>
<option value="F">Female</option>
</select>
Whereby default the empty value described by "-" will be displayed.
But what about when the user already has filled that field? Let's assume we have an object variable $user and may access his/hers sex by $user->sex. By default, I want the <select> to be selected with the proper attribute. Using PHP I may go for something like this:
<select class="form-select" name="sex" required>
<?php if ($user->sex == 'M') : ?>
<option value="">-</option>
<option selected value="M">Male</option>
<option value="F">Female</option>
<?php elseif($user->sex == 'F' : ?>
<option value="">-</option>
<option value="M">Male</option>
<option selected value="F">Female</option>
<?php else : ?>
<option selected value="">-</option>
<option value="M">Male</option>
<option value="F">Female</option>
<?php endif; ?>
</select>
But that requires a lot of if, elseif statements. As an example, I've provided only three-optional field. What about when there are more options?
Is there any elegant solution to choose a selected option of <select>, based on a variable (object fetched from database / POST / GET / SESSION - does not really matter), without using so many ifs? At best, I'd like to list possible options only once and then tell the site which is to be chosen by default.
Thanks everyone in advance!
Why do you repeat the options? Just check every option if it needs to be selected...
<select class="form-select" name="sex" required>
<option <?php if($user->sex == "") echo "selected" ?> value="">-</option>
<option <?php if($user->sex == "M") echo "selected" ?> value="M">Male</option>
<option <?php if($user->sex == "F") echo "selected" ?> value="F">Female</option>
</select>
I have this HTML code :
<select name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
and to make user able to select more than one items, I'm using this jQuery plugin : http://harvesthq.github.com/chosen/
but, once it submitted... the PHP script only able to retrieve one of $_POST['cars'] value. the last one. how to make PHP to be able to retrieve ALL of it?
I've found the answer...
<select name="cars[]" multiple="multiple">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
and in the PHP part :
$cars = $_POST['cars'];
print_r ($cars);
you must do the following:
//$_POST or $_GET is the method of your form request
foreach ($_POST['cars'] as $selected_option) {
echo $selected_option;
}
that's it.
I've written this piece of code:
<select name="account_type" id="account" class="form-control" required>
<option value="Asset">Asset</option>
<option value="Bank">Bank</option>
<option value="Capital">Capital</option>
<option value="Cash" <?php if($_POST['Cash'] == $ledger_account){?> selected <?php }?>>Cash</option>
<option value="Expense">Expense</option>
<option value="Income">Income</option>
<option value="Liability">Liability</option>
<option value="Creditors">S. Creditors</option>
</select>
I got a value from different page, which is stored in $ledger_account. However, I want to see an option (for example, Cash) to be selected here when the page loaded if the option value matches with $ledger_account value. Please help me to identify what I've done wrong here.
Your code are correct. but it's give some error..
then try to somting like this...
<option value="Cash" <?php if($_POST['Cash'] == $ledger_account){ ?> selected="selected" <?php }?> >Case</option>
I am not sure but i think this useful for you
Wow...all similar questions have serious downvotes. Guess I will be taking some as well, but been on this for a couple days and still stuck. So here it goes...
All of the following code is in index.php:
<form action="index.php" method="get">
<select name="month" id="month">
<option value="1">jan</option>
<option value="2">feb</option>
<option value="3">mar</option>
<option value="4">apr</option>
<option value="5">may</option>
<option value="6">jun</option>
<option value="7">jul</option>
<option value="8">aug</option>
<option value="9">sep</option>
<option value="10">oct</option>
<option value="11">nov</option>
<option value="12">dec</option>
</select>
</form>
<?php
$month = $_GET["month"];
echo "MONTH: $month";
$month in the php is NULL (Nothing prints out for $month). I want to be able to use it in the <?php ?> to make adjustments to it, then use it again in my HTML. How can I get the value from the HTML code for use in the PHP code?
I want to take 'value' and assign it to '$month'
Changes on the html page cannot change things within a php script as it has already executed on the server side. You need to setup some method to add the proper variable to the url (as you are using $_GET which is an array of the url variables in the query string, name value pairs after the ? in a url).
HTML
<select name="month" id="mySelect">
<option value="1">jan</option>
<option value="2">feb</option>
<option value="3">mar</option>
<option value="4">apr</option>
<option value="5">may</option>
<option value="6">jun</option>
<option value="7">jul</option>
<option value="8">aug</option>
<option value="9">sep</option>
<option value="10">oct</option>
<option value="11">nov</option>
<option value="12">dec</option>
</select>
JS
window.onload = function(){
var select = document.getElementById("mySelect");
select.onclick = function(){
var selectval = this.value;
window.location.href = "/somepage.php?month="+selectval;
};
};
Or using form submit, forms with the method GET will put all the input/select/textarea elements values into the url when submitted
<form action="somepage" method="GET">
<select name="month" id="mySelect">
<option value="1">jan</option>
<option value="2">feb</option>
<option value="3">mar</option>
<option value="4">apr</option>
<option value="5">may</option>
<option value="6">jun</option>
<option value="7">jul</option>
<option value="8">aug</option>
<option value="9">sep</option>
<option value="10">oct</option>
<option value="11">nov</option>
<option value="12">dec</option>
</select>
<input type="submit" name="submit" value="Submit">
</form>
PHP is a server side language. You cannot use php on client side. If you want to do something on client side, use JavaScript.
If you want to use php with your html either you have to refresh the page(see answer of Patrik Evans), or use Ajax.
If you are planning to use ajax as it is fast and simple, move your php to a different file like index-form.php and call it by ajax get method.
Basically when you load the page via browser, browser first lookup for your site IP from DNS. Then your server will compile the whole page (ignoring html, js and other client side script) and compile the server side scripts like PHP, Perl, Python etc depending upon the type of server. Then your server will send the compiled page to your browser and browser will read it, compile the client side scripts and render it in your viewport.