Multiple OPTION value to use as GET in form - php

I have searched many solutions and almost all over the internet but found no answer.
I am having a form like:
<form action="index.php" method="get">
<select id="store" type="text" name="s">
<option valA="value1" valB="value2">Selection 1</option>
<option valA="value3" valB="value4">Selection 2</option>
</select>
<input id="submit" type="submit" value="Submit" />
</form>
This form is extended via PHP with MySQL which takes valA and valB from database for each "Selection" in while loop and generate form with that form (given above).
I know that sending it via POST will reload my website with value given by value name. But I need to do it with two values.
So my link when I select one of the options will be:
http://mywebsite.com/index.php?valA=value1&valB=value2
Form is still active every time website reloads, so when I choose another option, I want to change it with submit and change URL.
Everything I found is to use multiple option and send two of them at the same time, but I have no multiple selection, but one selection with many parameters each (for me - two).
Is it possible to do it via HTML/Form, or I need to use PHP, but how?
I need to reload website - not change it only, because based on valA and valB website will load datatables script based on it and will produce table from database one of the values is just a key to table, another to database.

Maybe you can join your required values with some delimiter, for example:
<select id="store" type="text" name="s">
<option value="value1:value2">Selection 1</option>
<option value="value3:value4">Selection 2</option>
</select>
Your url becomes:
http://mywebsite.com/index.php?s=value1:value2
On server side you can:
$parts = explode(':', $_GET['s']);

You will need javascript to achieve that without php
If your php is whats receiving the call then why don't you have a value seperated by a delmiter value="value1|value2"
then in your php I would do the following
$values = explode("|",$_GET['s']);
then you have $values[0] and $values[1] - containing the two values

Related

Empty url parameter with array not marked empty with PHP empty() function

I have a simple GET form that a user can submit to search for posts (real estates) on my WordPress website.
My HTML search form looks a little like this:
<form method="get" action="estates">
<select name="city[]" multiple>
<option value="">Select an option</option>
<option value="city-1">City 1</option>
<option value="city-2">City 2</option>
</select>
<select name="purpose[]" multiple>
<option value="1">For sale</option>
<option value="2">For rent</option>
</select>
<input type="submit" value="Search">
</form>
Let's say a user selects no other option for city and selects a purpose with value '1'.
The 'purpose'value is correctly added to the URL, and the 'city' field has a value of "".
The URL that is generated would look like this: mysite.com/estates/?city%5B%5D=&purpose%5B%5D=1
Now the problem is with the 'city' field in this example. Because I use a default option for 'city', with a value of "", it is added to the URL but without a value.
However, when I do my PHP checks and build up to search query on my posts page, the empty($GET["city"]) is not returning true, and my query is not working correctly.
I have tried many things, including $GET["city"] == "" and array_key_exists('city', $_GET) but my PHP code always says that $GET["city"] is not empty and should be added to my search query, which then results in a bad query result.
Am I missing something, or is there another way to check if a value is set for this parameter?
When I do print_r($_GET['city']), I get the following return:
Array ( [0] => )
Html: a good practice is to have a default value that is disabled
<option disabled selected value> -- select an option -- </option>
default select option as blank
*Prefer not to pass empty value.
How I usually approach this:
Use the html disabled selected value
Use isset($_GET[.etc.]) then
Sanitize and trim your input
Check if it's a valid option in_array() or other method and then
Allow it to reach your DB
The behavior can vary on your liking. For instance if there is not value you want to inform the user, look for everything or both etc. Consider this, someone passes an argument from url, a city that does not exist. What you want then to happen?

Hide select option if already in DB

I don't know if what I want to do is possible (or too complicated).
I have a form that inserts the data into my DB. In it I have a select. I want to hide or remove the options that are already in my DB (so the user can't see them).
Every user has his own .php page where I edit the form with new options after the old ones are used.
PHP (In the same page, above the form):
<?php
if ($registro != "") {
include("conexao.php");
$conexao = #mysql_connect($host,$user,$pass)
or die("Cadastro");
$query = "INSERT INTO certificados VALUES ('$id','$registro','$name', now() )";
mysql_select_db($db);
mysql_query($query,$conexao);
}
else {
?>
HTML:
<form name="inserir" action="<? echo $PHP_SELF; ?>" method="post">
<select name="registro" id="registro">
<option value="">Select one</option>
<option value="11891">11891</option>
<option value="11892">11892</option>
<option value="11893">11893</option>
<option value="11894">11894</option>
<option value="11895">11895</option>
</select>
<input type="text" name="name" placeholder="Name">
<button type="submit" id="submit" name="B3">Send</button>
</form>
Edit 1: I'll try to explain better what I want to do.
Imagine that the user selected the first option value "11891" and submitted the form (thus inserting this value in the DB). After the page reloads, this value is no longer listed for him in the select. So based on the form that I posted here, now he would only have the options "11892", "11893", "11894" and "11895".
PS: Sorry for any mistakes or use of outdated parameters. My father wrote this code (he has some knowledge of php). In my case, my knowledge is almost nonexistent.
Thank you all for the help.
You should have 2 more tables. One 'tb_options' where you retrieve all the options. Another one 'tb_users_options' where you store all option based on user ID. So when retrieving the options from tb_options you select only those not present in tb_users_options Taking into account the user ID. This is a logic matter.

multiple select dropdown - need a hidden selected value or always selected value

I have a multiple select dropdown such as so. I need to ALWAYS include a specific value in 'settings[users][]' when it is passed regardless if other values are selected in the dropdown or not.
I have found some jquery solutions... is there any other way to add a specific value to this array? The actual value does not need to be shown in the selection area it just needs to always be included in the array when posted.
Basically what is happening is a user is selected to be edited which bring up different settings which can be changed. I am using this select box to allow them to set these new settings to other users if they wish. So, the original user value they are editing must always be present in 'settings[users][]' when it passed to my php script for processing.
<select multiple="multiple" name="settings[users][]" size="8">
<option value="" selected="selected">no additional users...</option>
<option value="1">User 1</option>
<option value="2">User 2</option>
<option value="3">User 3</option>
<option value="4">User 4</option>
</select>
EDIT :
I didn't really get the responses I was looking for... here is what I am doing currently.
added hidden field to the form of the always selected user :
<input type="hidden" name="user_id" value="someuserID"/>
in php then :
// push always selected user id to the posted array
array_push($_POST['settings']['users'], $_POST['user_id']);
// filter out any empty value such as if 'no additional users...' was left selected
$userArray = array_filter($userArray);
$userArray now equals all users I need to edit/change in my php
The comments here are trying to (gently) point out the fact that what you want to do is almost certainly unnecessary, and it's needlessly complicating the code -- do you really want to complicate the HTML (the bit that any user can see) just to simplify the PHP?
Having said that I can imagine a possible case where you might need to do this, and I'll get to that. But let me cover the other cases first.
First, if the fixed value is permanently fixed over the entire web site / application then just hardcode it in PHP. As Amir Bawab says in the comments:
$_POST['settings'][] = "someuserID"; // or array_push if you prefer
// Carry on as normal here ...
Second, if the value changes depending on the site context and so is only known to the HTML then your solution in the edit is absolutely the right way to do it. It's clean, it's obvious what's going on in the HTML and PHP, it'll be easy to debug and support later.
But, perhaps you don't have access to the PHP page and absolutely need to post the right structure. As you say you can do this in jQuery/Javascript by intercepting the form submit and inserting the extra hidden value marked as selected.
However, if you're determined to do it as pure HTML (maybe you don't have scripting) then you can do this:
<select multiple="multiple" name="settings[users][]" size="8">
<option value="" selected="selected">no additional users...</option>
<option value="1">User 1</option>
<option value="2">User 2</option>
<option value="3">User 3</option>
<option value="4">User 4</option>
</select>
<select style='display:none' multiple="multiple" name="settings[users][]">
<option value="someusedID" selected="selected"></option>
</select>
If I print_r($_POST) in PHP I get (I selected user 2):
Array
(
[settings] => Array
(
[users] => Array
(
[0] => 4
[1] => someusedID
)
)
)
Which is what you want, I think.
Note, I tried this on Chrome via a local server (XAMPP) I can't swear that all browsers and setups will handle this the same way. That's why the hidden field approach is a much better idea.

get the text of the selected option using php [duplicate]

This question already has answers here:
Get Text From <option> Tag Using PHP
(6 answers)
Closed 8 years ago.
in the following:
<form action="test.php" method="POST">
<select id="test" name="test">
<option value="1">Test One</option>
<option value="2">Test Two</option>
</select>
</form>
in test.php, I can get 1 or 2 as follow:
$result=$_POST['test'];
How can I get the text of the selected option (i.e. "Test One" or "Test Two") using php
This is not something that can be done through PHP alone. The PHP script can only "see" the information which is posted (the value for the selected option that is posted). You can use javascript to alter a hidden input field with the text contents of a selected option, and this will be included in the $_POST array:
<form action="test.php" method="POST">
<select id="test" onchange="document.getElementById('text_content').value=this.options[this.selectedIndex].text">
<option value="1">Test One</option>
<option value="2">Test Two</option>
</select>
<input type="hidden" name="test_text" id="text_content" value="" />
</form>
This will make the $_POST['test_text'] available with the selected index (but you should also force the onchange() function when the page loads so that it will be populated even if the user leaves the select field at the default value.
Only the value of the form control will be sent to the server. An easy way to send both is to simply include both in the the value:
<select name="test">
<option value="1|Test one">Test one</option>
<option value="2|Test two">Test two</option>
</select>
And then:
$test = explode('|', $_POST['test']);
Then you'll end up with $test[0] being "1" and $test[1] being "Test one".
You can't; that information is not sent back to the server. You will need to look at how you generated the HTML in the first place and get the text from there.
It is not sent so the only way to get it is having an array mapping values to titles in your PHP code.
This is a solution I could use except I already have the onChange calling a function to show a hidden block based on the selected building type in a statement.
I have building types I need the user to select. Several building types have a unique set of questions, e.g. Bank Branch, Data Center, Courtroom, etc. But there are many that have the same set of questions which I call Other Type. Each option has their associated value, e.g. Bank Branch has a value of "Bank_Branch", Data Center has "Data_Center", but Other Type has a value of "Other_Type". The text for "Other_Type" differs based on the building type, such as Convention Center, Museum, Performing Arts, etc. So I need the value "Other_Type" to "show" the questions in the "Other_Type" DIV block while using the text value to send in an email identifying the type of building, e.g. Convention Center, Museum, Performing Arts, etc.
Any way to use PHP to get the text value of the selected item? I'm already using the following inside the HTML
var sele = document.getElementById('building_type');
var seleVal = sele.options[sele.selectedIndex].value;
var seleTxt = sele.options[sele.selectedIndex].text;
document.getElementById("other_text").innerHTML = seleTxt;
I'm not seeing a way to do this.
SOLVED: I can simply create the hidden div and in my check for Other Type set the innerHTML for the hidden div.
The text for the selected option is not passed through the HTML form. If you want the text, you have to store that in a hidden HTML input on your page, or you need to add some business logic in your PHP code to translate the value of ìd into the text (through a switch statement, or by querying a database, etc.)
Unfortunately when you submit a form and a variable it only takes one parameter which is it's value. You would need to make the value. Test One the value in order for it to pass on to the PHP script. What is the purpose of value="1" cause you can probably use it in a different attribute?
Im not a experienced php programer, but u can check the value selected in SELECT tag with php.
i dont know why people say u can not.
if ( $_POST['test'] == 1)
{ do something }
else
{ option value 2 is selected, do something else }
Im sure this will work.
Try it if 1, 2 not needed. you will get required text as value
<form action="test.php" method="POST">
<select id="test" name="test">
<option value="Test One">Test One</option>
<option value="Test Two">Test Two</option>
</select>
</form>

storing multiple selections from html form in PHP array (I'm using Codeigniter)

I've got an HTML form which allows a user to select multiple options in a dropdown. I then pass that data on as post data to a PHP backend (I'm using codeigniter for the backend, and the data is being passed to a model).
In javascript, I can log the value being passed, and if there are multiple values, it shows as a proper, comma separated string of values. But if log the value in PHP, it only lists the last of the multiple values. How do I get it to retain all selections? This is my code:
FORM:
<form action="http://localhost:8888/index.php/control_form/add_all" method="post" accept-charset="utf-8">
<label for="sel_dep">Member Dependencies</label>
<select id="sel_dep" name="sel_dep" multiple>
<option value=""></option>
<option value="4">Soundgarden</option>
<option value="5">Rage Against the Machine</option>
<input type="submit" name="submit" value="Submit" />
</select>
</form>
PHP Codeigniter Model:
function edit_member(){
if (isset($_POST['submit'])) {
$v_memberdep = $this->input->post('sel_dep');
log_message('debug', 'DEP: ' . $this->input->post('sel_dep'));
}
}
Set the name of your select box to f_memberdep[] the [] will tell PHP that it should be passed as an array so you will receive all values.
Also should point out that your logging a field called sel_dep when your select box is called f_memberdep but that's probably just a formatting thing.

Categories