How to use multiple select with jQuery? - php

I need to submit a form with multiple values
<form onsubmit="return false;">
<?php
echo "<select id=\"uname\" name=\"uname[]\" multiple>\n";
$sql = "SELECT * FROM users ORDER BY surname ASC";
$res = $db->query($sql);
while($row = $res->fetch()){
$strA = $row["uname"];
$strB = $row["givenname"];
$strC = $row["surname"];
echo "<option value=\"$strA\">$strC, $strB</option>\n";
}
echo "</select>\n";
echo "<input type='button' name='endreBruker' value='OK' onclick='javascript:".$_POST['edit']."(this.form)'/>";
?>
and pass the POST values through this to newNote.php
function newNote(form) {
$('#main').load ('newNote.php', {'uname[]' : form.uname.value} );
}
Problem is I only receive an array with a single value.

Check out jQuery's serialize(). You could then write something like this:
function newNote(form) {
$('#main').load ('newNote.php', $(form).serialize() );
}

You may try something like that:
<select name="theselect" onchange="this.selectedIndex = 1;">
<option value="Red">Red</option>
<option value="Green" selected="selected">Green</option>
<option value="Blue">Blue</option>
</select>
Green color always been selected, but user may see other color, but don`t pick them

Related

How can i fix this nested isSet() php code with a switch statement inside?

Below is the code i am referring to. The problem here is that after I select a dropdown option (such as delete account/delete character), my switch statement displays a dynamically populated dropdown list(based on mysql data). However, when i try to select an option from that list and delete it, the if block (in the second code fence) never executes. I've tried a variety of workarounds and i am just at a loss of what to do. Any help would be appreciated. Thank you in advance.
<form class = "myform" action = "adminportal.php" method = "post">
<select name = "option" required>
<option value ="none" selected disabled hidden>Choose an Option</option>
<option value="banacc">Delete an account</option>
<option value="deletechar">Delete a character</option>
<option value="update">Add gold/levels/changename</option>
</select>
<input type ="submit" name = "submit_search" value="submit"/>
</form>
<?php
if(isset($_POST['submit_search']))
{
$mychoice =$_POST['option'];
switch($mychoice)
{
this does not execute
// list all the users, choose an account to ban
case "banacc":
$query = "select username from userinfo";
echo "<form action = '' method = 'POST'>";
echo "<select name='account' value=''>Account</option>";
echo"<option selected ='default' value = 'default'>Select Account</option>";
foreach ($con->query($query) as $row) {
echo "<option>$row[username]</option>";
}
echo "</select>";
echo"<input type ='submit' name = 'delete' value='delete'/></form>";
if(isset($_POST['delete']))
{
$account = $_POST['account'];
$query = "delete from userinfo where username = '$account'";
$query_run = mysqli_query($con,$query);
if($query_run){
echo '<script type = "text/javascript"> alert("Purchase Successful")</script>';
}
else{
echo '<script type = "text/javascript"> alert("Purchase Successful")</script>';
}
}
break;
case "deletechar":
$query = "select name from characterinfo";
echo "<select name='character' value=''>Character</option>";
echo"<option selected ='default' value = 'default'>Select Character</option>";
foreach ($con->query($query) as $row) {
echo "<option>$row[name]</option>";
}
echo '</select>';
break;
case "update":
$query = "select name from characterinfo";
echo "<select name='character' value=''>Character</option>";
echo"<option selected ='default' value = 'default'>Select Character</option>";
foreach ($con->query($query) as $row) {
echo "<option>$row[name]</option>";
}
echo '</select>';
break;
}
}
?>

PHP dropdown search result selecting wrong input

I have made a dropdown search form that is auto populated by my database content. The voices in the table would be for example types of woods with varing dimensions.
So there are repeatable wood names with diverse data.
To avoid repetition the dropdown is populated with wood types combined to be selected then displayed with all their variants.
The problem is, upon selecting an input, the results are of the item listed above and not the one selected.
<form action="search2.php" method="POST">
<select name="finit" onchange='this.form.submit()'>
<?php
include("connect.php");
$query = "SELECT finit FROM prime";
$info = mysqli_query($conn, $query);
$finit = '';
echo "<option value=\"\">Selezione Materiale</option>";
while($row = $info->fetch_assoc()){
if($row['finit'] != $finit) {
echo "<option value=\"$finit\">" . $row['finit'] . "</option>";
$finit = $row['finit'];
}
}
?>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
Since there are many variants(dimensions) associated with a single wood type, you have to first take the wood type as input from user(via dropdown list), and probably then you may want to display all possible variants(dimensions) of that particular wood type.
So, change the SQL query in the following way,
$query = "SELECT DISTINCT finit FROM prime";
and the while loop in the following way,
while($row = $info->fetch_assoc()){
$output = "<option value='" . $row['finit'] . "'";
if($row['finit'] == $_POST['finit']){
$output .= " selected='selected'";
}
$output .= ">" . $row['finit'] . "</option>";
echo $output;
}
Try this but change if condition according your default value and sql value should be match.
<form action="search2.php" method="POST">
<select name="finit" onchange='this.form.submit()'>
<?php
include("connect.php");
$query = "SELECT finit FROM prime";
$info = mysqli_query($conn, $query);
$finit = '';
?>
<option value="">Selezione Materiale</option>;
<?php
while($row = $info->fetch_assoc()){
if($row['finit'] == $finit) {
$selected = 'selected';
}else{
$selected = '';
$finit = $row['finit'];
}
?>
<option value="<?php echo $finit ?>" <?php echo $selected ?>><?php echo $row['finit']?></option>
<?php } ?>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>

how to create select from php

I am trying to create some code to do a list of components in mysql.
Something like this
decod - 1,2,3,4,5
and to make it in drop-down listlike this
decod (dorplist) 1
2
3
4
5
I tried this one:
<?php
$mysqli = mysqli_connect("host", "username", "password", "name");
$result = mysqli_query($mysqli, 'SELECT * FROM componnets') or die(mysqli_error(mysqli));
while ($row = mysqli_fetch_array($result)) {
$org = str_replace("," , "'>", $row['fill']."");
$replace = str_replace("," , "
".$org."</option><option value='", $row['fill']."'>");
echo "<input type='hidden' name='compo' value='".$row['compon']."'>".$row['compon']."";
echo "<select name='fill'>";
echo "<option value='".$replace."'>".$replace."</option>";
echo "</select>";
}
// Free result set
mysqli_free_result($result);
/* close connection */
$mysqli->close();
?>
and do make it output to an XML
but I don't know how! I output like this
<input type='hidden' name='compo' value='DECODERS'>DECODERS
<select name='fill'>
<option value='1010'>20'>30'>40</option>
<option value='2010'>20'>30'>40</option>
<option value='3010'>20'>30'>40</option>
<option value='40'>'>1010'>20'>30'>40</option>
<option value='2010'>20'>30'>40</option>
<option value='3010'>20'>30'>40</option><option value='40'></option>
</select>
<input type='hidden' name='compo' value='DECODERS'>DECODERS
<select name='fill'>
<option value='10'>10</option>
</select>
First create the <select> outside the while loop. Then iterate over the rows to add the elements.
I'm not sure what you're trying to accomplish with your str_replace calls, so I removed them.
Something like this might get you started in the right direction:
<?php
$result = mysqli_query($mysqli, 'SELECT * FROM componnets') or die(mysqli_error(mysqli));
echo "<select name='fill'>";
echo "<input type='hidden' name='compo' value='".$row['compon']."'>".$row['compon']."";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='".$row['fill']."'>".$row['fill']."</option>";
}
echo "</select>";
mysqli_free_result($result);
$mysqli->close();
?>

show selected value from dropdown

Hi i have been working on a form wherein there's a dropdown menu and it's values are from the database. My problem is it doesnt show the value selected after submitting the form. what maybe the problem?
<select name="professional" />
<option value="">Choose one</option>
<?php
$result2 = mysql_query("SELECT * FROM professional");
while($row2 = mysql_fetch_array($result2))
{
$prc = $row2['name'];
$prof = $row2['prcno'] ."\t"."|\t". $row2['name'] ."\t"."|\t".$row2['profession'];
echo "<option value ='$prc'>$prof</option>";
}
?>
</select>
<select name="professional" disabled/>
<option value="">Choose one</option>
<?php
$result2 = mysql_query("SELECT * FROM professional");
$i=0;
while($row2 = mysql_fetch_array($result2))
{
$prc = $row2['name'];
$p1[$i] = $prc;
$prof = $row2['prcno'] ."\t"."|\t". $row2['name'] ."\t"."|\t".$row2['profession'];
$p2[$i] = $prof;
if($_POST['professional'] == $p1[$i])
{
echo "<option selected value ='$p1[$i]'>$p2[$i]</option>";
}
else
{
echo "<option value ='$p1[$i]'>$p2[$i]</option>";
}
}
?>
</select>
It seems to me, you're not incrementing $i, so you keep overwriting $p1[0] and $p2[0] in each iteration of the while-loop.
So add $i++ at the beginning or the end of your loop - or drop the whole use of these to arrays ($p1 and $p2) and use $prc and $prof just as you do in the first code-block - or do you need them for something?
Another thing, try removing the space between value and ='$p1[$i]' - but I'm not sure if that's a problem.
Try
selected="selected"
in stead of
selected
change these lines to
echo "<option selected value ='<?php echo $p1[$i]; ?>'><?php echo $p2[$i]; ?></option>";
and do not forget to increment your $i too
Hope it will help :)

echo the selected value from the dropdown menu?

I have a form and here is code for the dropdown menu. Can you help me make a code to show the selected value after submitting the form? im using php
<select name="professional" />
<option value="">Choose one</option>
<?php
$result2 = mysql_query("SELECT * FROM professional");
while($row2 = mysql_fetch_array($result2))
{
$prc = $row2['name'];
$prof = $row2['prcno'] ."\t"."|\t". $row2['name'] ."\t"."|\t".$row2['profession'];
echo "<option value ='$prc'>$prof</option>";
}
?>
</select>
<?php
echo $_REQUEST['professional'];
?>
you might wish to use $_GET or $_POST instead of $_REQUEST, please check: http://php.net/manual/en/reserved.variables.request.php

Categories