I am trying to build a php contact form with a dropdown list of values, say "A", "B" and "C", that fits the code structure below.
As well, I would like to call this form from a link on another page, and have the value of the dropdown list automatically populated with the passed-in parameter. For example, if the link is "...?val=B", I'd like the dropdown list to automatically set to "B".
I have code below that works for an email field. What does the code look like for a dropdown list?
It's been a long while since I've coded PHP, so any help is appreciated.
<div class="input-field">
<div>
<?php _e('Email','circles'); echo ' <span>('; _e('required','circles'); echo ')</span>'; ?>
</div>
<div class='input-style dlight-grey sc-input'>
<input type="email" name="form_email" size="20" value="<?php echo $form_email; ?>" />
</div>
</div>
Suppose you store options to a html select element(dropdown list) in array $ops
<form>
<select name='foo'>
<?php
$val = $_GET['val'];
while($option = next($ops)){
if ($option == $val){
echo "<option value=$option selected='selected'>$option</option";
else
echo "<option value=$option>$option</option>";
}
?>
</select>
<form>
Alternatively, you can use Javascript to add 'selected' attribute to the option equals to $_GET['val'], see .attr()
Related
I am creating a webpage with from-controls (2 in this exemple).
My code is the following :
<body>
<div class="option_choice_global">
Select your options
<div class="col-xs-2">
<label> Application </label>
<select class="form-control" id="application">
<?php
$applications = get_apps();
foreach ($applications as $key => $value) { echo '<option value='.$key.'>'.$value.'</option>'; }
?>
</select>
</div>
<div class="col-xs-1">
<label> Version </label>
<select class="form-control" id="version">
<?php
$versions = get_versions();
foreach ($versions as $key => $value) { echo '<option value='.$key.'>'.$value.'</option>'; }
?>
</select>
</div>
<div class="col-xs-12">
<button type="submit" class="btn btn-default" onclick="fonction_submit_graph(this)"> <b> Submit </b> </button>
</div>
</div>
</body>
But I would like the second one (the version) to appear only when a value on the first controller (the application) is chosen. And my function get_versions() will then depend on the selected application : get_versions(application_number).
How can I do to show the second controller only when a value on the first one is selected ? And to get the selected value ?
Thank you!
If I understand you right, you want to show the second select dropdown after a value is chosen from the first.
This can easily be done with JavaScript and CSS:
In the second select tag, add these attributes: style="{display: none;}". This will hide the dropdown. Also give it id="select2" for easy identification.
In the first select tag, add this attribute: onchange=showHiddenSelect(); This causes a function to be called when the value changes ie an option is selected. Also give it an id="select1".
Somewhere in your document, define the showHiddenSelect function like this:
function showHiddenSelect() {
document.getElementById("select2").style.display="block";
}
If you also wish to get the selected value, you'd just add this in that function: var select1value = document.getElementById("select1").value; and do what you want with it.
If you need the selected option from select1 to be sent to a backend and then used to calculate the options for select2, then your best bet would be to use AJAX to send and receive data from a script and then populate the select with JavaScript.
I have a select option in foreach loop. In every loop I'm creating a div and inside div a select option. But problem is when I send select's value with post method I can't get option's correct posted value. For foreach first 2,3 item button is working and submitting form but when I try to submit other items' form button is not working I can't create unique name for select because I must assign post variable to a variable in action page.
I think problem is about unique select name="", I can't give an unique name to select because I need it's name in action page as you know. How can I solve this stupid thing.?
<?php
foreach($a as keywords_arr){
?>
<form name="<?= $keywords_arr["keyword_id"]?>" method="post" action="actions/myaction.php">
<div class="modal-body">
<label for="tagler">Bla bla</label>
<select name="tagler" style="width: 100%;" class="form-control">
<?php
$all_tags = $db->query("SELECT *****");
$all_tags->setFetchMode(PDO::FETCH_ASSOC);
$all_tags_arr = $all_tags->fetchAll();
foreach ($all_tags_arr as $tag) {?>
<option value="<?= $tag["tag_id"] ?>"><?= $tag["tag_name"] ?></option>
<?php } ?>
</select>
<input type="hidden" name="kw_id_label" value="<?= $keywords_arr["keyword_id"] ?>"/>
<br><br>
<span id="result_lbl_"></span>
</div>
<div class="modal-footer">
<button id ="btn_<?= $keywords_arr["keyword_id"] ?>" type="submit">Save</button>
</div>
</form>
<?php}?>
myaction.php page
<?php
$tag = $_POST["tagler"];
$keyword_id = $_POST["kw_id_label"];
?>
Fiddle example:See example
You should declare select as array like tagler[]
<select name="tagler[]" style="width: 100%;" class="form-control">
Now in your php file use print_r($_POST), you will get selected options in array.
You can create dynamic select names in loop but in addition to that create hidden field as array which will save names of all select value generated. so in POST you will read hidden value to identify select boxes.
<?php
foreach($a as keywords_arr){
?>
<form name="<?= $keywords_arr["keyword_id"]?>" method="post" action="actions/myaction.php">
<select name="tagler<?php echo $keywords_arr["keyword_id"]?>" style="width: 100%;" class="form-control">
---
---
<input type = "hidden" name="select_boxes_name[]" value="<?php echo tagler<?php echo $keywords_arr["keyword_id"]?>">
after form post read select_boxes_name[] array to identify select boxes posted.
The unique id for select name="" is not your problem. The only problem I found is that you have the last php tag too close to the closing bracket and the intepreter doesn't recognize it. Please, try to create space between the closing bracket.
Change this:
<?php}?>
to this:
<?php } ?>
(UPDATE)
Use the below code for myaction.php:
<?php
$post_name = "uniquename";
$post_len = strlen($post_name);
foreach ($_POST as $key => $value)
{
//if the name of the current post begins with "uniquename"
if (substr($key, 0, $post_len) == "uniquename")
{
$index = $post_len;
$post_id = (int)substr($key, $index);
echo "post_id = " . $post_id . "<br/>key = " . $key;
// do your stuff here...
break;
}
}
?>
Here, we filter the POST variables and when we find the right one, we extract the ID from it (the ID in our case is the numeric part after the "uniquename". e.g. From uniquename5) the ID (post_id) will be 5.
I would like to know how to retrieve a selected value from a dynamically selection box. If I get the selected value then I will store it into another variable that is located in another php file. This variable will help me in a sql query in postgresql.
//First php file
<form name="boton_alta_soniador" action="dar_alta_soniador.php" method="POST" autocomplete="off">
<div class="text-field">
Nombre de la asociacion
<? $strconn="dbname=postgres port=5432 host=127.0.0.1 user=xxx password=xxx";
$conn=pg_Connect($strconn);
$consulta="Select n_asociacion from asociacion";
$result=pg_query($conn,$consulta);
while($results [] =pg_fetch_object($result));
array_pop($results);?>
<select name="asociacion_seleccion" id="i_clave_asociacion">
<?php foreach ( $results as $option ) : ?>
<option value="<?php echo $option->i_clave_asociacion; ?>"><?php echo $option->n_asociacion; ?></option>
<?php endforeach; ?>
</select>
</div>
</form>
This is just the dynamically selection box. Then I want to store the selected value in this variable:
$ingresaAsociacion = pg_escape_string($_POST['asociacion_seleccion']);
So I can query the following statement:
$conocerIDasociacion = "SELECT N_ASOCIACION FROM ASOCIACION WHERE I_CLAVE_ASOCIACION='$ingresaAsociacion'";
I didn't want to use jQuery because the whole system is almost entirely made in PHP and HTML.
Please, any help is welcome and I'm all ears to everyone.
Cheers!
Looks like you're missing a Submit button.
You do not have to use AJAX at all, nor jQuery. You can submit your form and process the selection value as you see fit.
The selected value in the select Tag will be sent to dar_alta_soniador.php, and that file will process that data, using the exact code you wrote:
$_POST['asociacion_seleccion']
So, in dar_alta_soniador.php you will write that code:
$ingresaAsociacion = pg_escape_string($_POST['asociacion_seleccion']);
And then perform the query. You do not even have to worry about sending the data around in a session variable, POST does it for you already.
So everything should be OK in your code, or I may have misunderstood your question. Do you have an error message or get some inappropriate behavior?
Maybe the submit button is missing? I use a code like this:
For the select tag:
<div class="controls">
<select name="list_name">
<option>List</option>
<?php
foreach ($nucleos as $inner_array) {
$out = "<option>" . $inner_array['name'] . "</option>";
echo $out;
}
?>
</select>
</div>
And for the Submit button:
<div class="control-group">
<div class="controls">
<button type="submit" class="btn">Confirm</button>
<br/>
</div>
</div>
</form>
I am using bootstrap here for style, HTML and CSS. Nothing more.
Best wishes,
I found another solution:
<? $strconn="dbname=postgres port=5432 host=127.0.0.1 user=xxxx password=xxxx";
$conn=pg_Connect($strconn);
$consulta="Select n_asociacion from asociacion";
$result=pg_query($conn,$consulta);?>
<select name="asociacion_seleccion" id="i_clave_asociacion">
<?php
while($row=pg_fetch_assoc($result))
{echo "<option>{$row['n_asociacion']}</option>";}?>
</select>
The point is, whenever the user selects an option from the dyamically selection box, the value of the selection box is known if we call the value with pg_escape_string($_POST['NAME OF YOUR SELECTION BOX']);.
Thanks to all for your collaboration,my problem was resolved.
I need to post an additonal variable when using bootstrap. I have a dropdown menu that is working fine, but as a user selects from the dropdown menu I would also like to be able to choose a second input from a pagination list..
my code so far:
<form action="racing.php" method="POST">
<select name="meeting_codes" id="meeting_codes" class="span1" onchange="this.form.submit();">
<?php include('get_codes.php');
$selected = $_POST['meeting_codes'];
for($i=0; $i<count($data_array); $i++){
echo' <option value="'.$data_array[$i][0].( $data_array[$i][0] == $selected ? '" selected="selected">' : '">' ).$data_array[$i][0].'</option>';
}
?>
</select>
</form>
<div class="pagination">
<ul>
<?php
for($j=1; $j<=12; $j++){
echo '<li><a>'.$j.'</a></li>';
}
?>
</ul>
</div>
I include a PHP file that requires two $_POST variables, first from the drop down, and the second from the pagination list...
The first includes file (get_codes.php) creates an array for the dropdown menu.
How do I insert the second POST variable?
i've managed a solution... whether it's appropriate or not, or elegant, i am unsure
old code:
echo '<li><a>'.$j.'</a></li>';
changed to:
echo '<li>Race '.$j.'</li>';
where 'meeting_code' and 'race_num' are the variables need posted via $_GET
I've created a php form to insert values into a database.
One of my form options is a dynamic list populated with fields from another table.
I first created the form without the dynamic option, and all data inserted just fine (and still does).
Now I'm attempting to include the code below, and while it displays the option values properly, the value fails to insert. Any advice?
<?php
/*
* LIST ALL CATEGORIES
****************************************/
include('../dbconnection.php');
$query = 'SELECT category_id, category_name FROM ingredient_categories';
$result = mysql_query($query);
echo '<select>';
while ($ingredientCategoryOption = mysql_fetch_array($result)) {
echo '<option value="'.$ingredientCategoryOption[category_id].'">'.$ingredientCategoryOption[category_name].'</option>';
}
echo '</select>';
?>
I had created something similar yesterday. The $polls array is passed to the view in CodeIgniter in the $this->load->view('poll.php', $data['polls']), while you do it in the page itself. However, you can have the general idea.
<FORM id="formPoll" class="question" name="createpoll" action="<?php echo base_url()?>index.php/poll/selectOption/" method="POST">
<select name="poll_list">
<?php
foreach($polls as $poll){
echo "<option name='poll_table'>$poll->name</option>";
}
?>
</select>
<div id="input">
Poll name: <input type="text" name="name"></input>
Title: <input type="text" name="title"></input>
</div>
<div id="options">
Option: <input type="text" name="option"></input>
</div>
<input type="submit"></input>
</FORM>
Some ideas:
Check if $results is not empty before using it
Give your <select> form a name, as I showed above
Check if $ingredientCategoryOption is not null or is something returned.
Check your database connection