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
Related
Once I add session in edit form, I could not get selected values in dropdown menu. I am using bootstrap framework. When I remove session from the same beginning it works.
edit.php file
<?php
session_start();
$car=$_POST['car']
?>
<form method="post" action="">
<div class=from-group>
<label for="exampleInputEmail1>Car</label>
<select class="form-group" name="car" id="car" value="<?php echo $car; selected?>">
<option value=<?php echo $car?> selected>
<?php cars()?>
</option>
</div>
</form>
I have created "cars" function in which I get values of all cars from database.
function cars(){
$link = new mysqli("", "", "", "");
$link = set_charset("utf8);
$sql = mysqli_query($link, "SELECT * FROM db_cars ORDER BY CarId")
echo '<option value=""> Choose car </option>'
while ($record = mysqli_fetch_array($sql)) {
echo '<option value = "'.$record['CarId']'"> . $record['CarName'].' </option>
}
}
Any help or advice is appreciated.
The select element has no name attribute, so it cannot be a successful control (i.e. submit any data).
The form has no submit button, so there is no obvious way for you to trigger the form submission. (You have to submit the form in a new HTTP request to the server to run the PHP and get the data from the user).
First, add the semicolons after every line when you use PHP. That can make a lot of troubles.
Second, some attributes have just one ("), like the label in the HTML.
Third, you are adding "something" called selected after echoing $car.
In the function cars() you are echoing without a semicolon (;) and just after doing a while() loop.
Check out all, you are missing very important and essential things
Also, I gonna guess this is not a "copy-paste" from your code, but if it's, you also are missing </select>
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()
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.
CanĀ“t find the mistake. I have three dropdown menus which are connected to a SQL server. When I load the page the first dropdown is already filled. After selecting a value from the first dropdown the page calls the function "reload(form)" from the script and in shows the new index in the second value. Till there it works fine, but when I select a value from the second dropdown in order to call the function "reload2(form)" it refreshs the whole page and the value selected from the first and second dropdown disappears and the dird dropdown doesn't show anything.
Here is the script code:
<script language=JavaScript>
function reload(form)
{
var val=document.form.cliente.options[form.cliente.options.selectedIndex].value;
self.location='indexmm.php?cliente=' + val ;
}
function reload2(form)
{
var val=document.form.cliente.options[form.cliente.options.selectedIndex].value;
var val2=document.form.equipos.options[form.equipos.options.selectedIndex].value;
self.location='indexmm.php?cliente=' + val + '&equipos=' + val2 ;
}
</script>
And here is my php code:
<form method="post" name="form" id="form" action="post.php">
<h1>Cliente:
<?php
echo "<select name='cliente' onchange=\"reload(this.form)\"><option value=''>Seleccione...</option>";
while($busq1 = mysql_fetch_array($consultacliente)) {
if($busq1['IdCln']==#$cliente){echo utf8_encode("<option selected value='$busq[IdCln]'>$busq1[Cliente]</option>"."<BR>");}
else{echo utf8_encode("<option value='$busq1[IdCln]'>$busq1[Cliente]</option>");}
}
echo "</select>"
?>
</h1>
<h1>Equipos:</br></h1>
<?php
echo "<select name='equipos' size='5' style='width:400px' onchange=\"reload2(this.form)\">";
while($busq3 = mysql_fetch_array($consultaequipos)) {
if($busq3['IdGn']==#$refaccioness){
echo utf8_encode("<option selected value='$busq3[IdGn]'>$busq3[Generador]</option>"."<BR>");}
else{echo utf8_encode("<option value='$busq3[IdGn]'>$busq3[Generador]</option>");}
}
echo "</select>"
?>
</h1>
<h1>Refacciones:</br></h1>
<?php
echo "<select name='refacciones' size='20' style='width:400px'>";
while($busq4 = mysql_fetch_array($consultarefac)) {
echo utf8_encode("<option value='$busq4[Descripcion]'>$busq2[Descripcion]</option>");
}
echo "</select>"
?>
The variables $consultarefac, $consultacliente, $consultaequipos are the SQL query.
Any suggestion?
I would recommend NOT reloading the page every time someone changes a dropdown. I would recommend only updating the dropdowns below the current one using AJAX.
Simple AJAX call would be to replace a div surrounding the following dropdown(s) with a snippet of html. The first select will replace the contents of div 'second' (which will include clearing the third dropdown. The second select will replace the contents of div 'third'.
The pages you grab with ajax will return the html snippet for only the appropriate section. The replace functions will use ajax against the appropriate pages to replace the contents of the appropriate div.
<div id="first">
<select name=first onchange="replace1()"></select>
<div id="second">
<select name=second onchange="replace2()"></select>
<div id="third">
<select name=third></select>
</div>
</div>
</div>
Let's say I have a HTML form:
USA
CDN
And I select option 1 (USA)
Then a php page
Ok, duh, works fine and echo's "1"
How can I display "USA" as well? So in essence, I want to pass along the option's TEXT also. How could I do this?
You would either need to change the option value on the HTML page to be the text you want displayed (poses a XSS security hazard) or on the page that's displaying the text, you'd need an array where all the values match with the forum input.
Ex:
$names[1] = "USA"; $names[2] = "CDN";
Then when you want to display it, you'd call $names[$selection] to output the text.
Another option you can consider is using javascript to update a hidden HTML element on the submitting page when the user makes their selection (using onUpdate). This information would be passed along with the submitted form data. Not the most secure or reliable of options, but an option nonetheless.
The client's browser will not post back anything beyond the form element's name and value - you would have to incorporate additional form fields and Javascript (or change the element's value) to post "USA".
I suggest having a copy of the same data structure you used to print the form.
For example, if you were to do it all in one PHP page...
<?php
$countries=array('USA','CDN');
?>
<form action='?' method='post'>
<select name='country'><?php
foreach($countries as $key=>$country){?>
<option value='<?php echo $key;?>'><?php echo $country;?></option>
<?php
} ?>
</select>
<input type='submit'>
</form>
<?php
if(isset($_POST['country'])){
$chosen=(int)$_POST['country'];
if(!isset($countries[$chosen])){?>Unknown country selection, wtf<?php exit; }?>
<div style='margin-top:20px;'>You selected <?php echo $countries[$chosen];?></div>
<?php }
Another option is to ditch the numeric value altogether, and just use the country name as the value of the option. However, for verification, you'll still want the list of values. Check the differences in this example...
<?php
$countries=array('USA','CDN');
?>
<form action='?' method='post'>
<select name='country'><?php
foreach($countries as $country){?>
<option value='<?php echo $country;?>'><?php echo $country;?></option>
<?php
} ?>
</select>
<input type='submit'>
</form>
<?php
if(isset($_POST['country'])){
$chosen=preg_replace('/[^\w]/','',$_POST['country']);//this isn't strictly necessary, since the next line checks if the value is in your original array - but filtering input is a good habit!
if(!in_array($_POST['country'],$countries)){?>Unknown country selection, wtf<?php exit; }?>
<div style='margin-top:20px;'>You selected <?php echo $chosen;?></div>
<?php }