Drop down menu in PhP javascript html - javascript

Need your help in my application i have a php page (profile.php) that give me this result:
Test1/Test
Test2/Test0
...
I want to put each line in this result in a selection bar like this one:
but in fact the result was like this one :
this is my code for m drop down menu:
<option value="0">please select an existing profile</option>
<?php
require('profile.php');
?>
code profile.php:
<?php
$output = shell_exec('/etc/init.d/dima --get-profilelist');
echo "<option value=\"" . $output ."\">".$output."</option>";
?>

Your result should looks like :
<option value="0">please select an existing profile</option>
<option value="1">Test1/Test</option>
<option value="2">Test2/Test0</option>
Yours is like that :
<option value="0">please select an existing profile</option>
<option value="0">Test1/Test Test2/Test0</option>
You have to enclose each result line in the tag so that it can work as expected

$output is taking all the content in one line...you can use var_dump() to see how data is returned in $output and edit that... You need to use for each value in the dropdown

You should loop your $output and echo every option, Try This :
<?php
$output = shell_exec('/etc/init.d/dima --get-profilelist');
$optionsArray = explode(' ',$output);
foreach(optionsArray as $option)
echo "<option value='". $option."'>".$option."</option>";
?>
Hope this will help.

Related

jQuery removing characters after apostrophe when sending string from HTML to jQuery

I am trying to pass a variable, a first name with an apostrophe, from HTML to jQuery. By the time the value gets to jQuery, the string is missing all the characters after the apostrophe.
Name Example: Test'Ing
This is part of the form. I fill the list dynamically and the name appears correct when pulled from the database:
<?php echo "<select id='hiringManager' class='dropdown_form' name='hiringManager'>";
echo "<option disabled selected>Hiring Manager</option>";
while ($result_row = mysqli_fetch_row($hiring_manager_query)) {
echo "<option value='" . $result_row[0] . "'>" . $result_row[0] . "</option>";
}
echo "</select>" ?>
And then I get the value from the form and display it in a jQuery modal for confirmation:
$('#hiringManagerModal').text($('#hiringManager').val());
The resulting text shown is:
"Test" instead of "Test'Ing"
I have poked around but have not been able to find a post that addresses this, or I could not be phrasing the issue correctly in the search.
Any help is appreciated. Thanks!
The problem is with the PHP code. I have replaced the 's with "s and "s with 's. The outputted HTML earlier was,
<select id='hiringManager' class='dropdown_form' name='hiringManager'>
<option disabled selected>Hiring Manager</option>
<option value='test'ing'>test'ing</option> <!-- In this option, the value is test instead of test'ing -->
</select>
and now after all the replacements, it is,
<select id="hiringManager" class="dropdown_form" name="hiringManager">
<option disabled selected>Hiring Manager</option>
<option value="test'ing">test'ing</option><!-- Now, here the value is test'ing, as required -->
</select>
What really happens is that when ' is encountered, the string gets terminated and as a result only "test" gets outputted instead of "test'ing".
The corrected PHP code after all the replacements:
<?php
echo '<select id="hiringManager" class="dropdown_form" onchange="displayInModal();" name="hiringManager">';
echo '<option disabled selected>Hiring Manager</option>';
while ($result_row = mysqli_fetch_row($hiring_manager_query)) {
echo '<option value="' . $result_row[0] . '">' . $result_row[0] . '</option>';
}
echo '</select>'
?>
with,
$('#hiringManagerModal').text($('#hiringManager').val());
Now, the same problem would arise with
<option value="test"ing"></option>
Other way:
echo '<option value='test\'ing'></option>';

repopulating multiple select form with a drop down form through ajax

i want to repopulate a multiple select form from the database with ajax by just selecting a drop down value.
here is the code for the drop down menu:
<?php
$sql2 = "select _id, title from sub_category order by title;";
$sel2 = mysqli_query($connect,$sql2);
$array2 = array();
while($row2 = mysqli_fetch_assoc($sel2)){
$array2[] = $row2;
}
?>
<div class="span2">
<select name="des_pos" id="des_pos">
<?php
foreach($array2 as $value2){ ?>
<option value ="<?php echo $value2['_id']; ?>" <?php if($value2["title"] == $desired_position){ echo 'selected="selected"';} ?>><?php echo $value2['title']; ?> </option>
<?php
}
?>
</select>
</div>
and here is the code for the multiple select form:
$sql4 = "SELECT _id, score_type from test_category where sub_code='$des_pos_id'";
$sel4 = mysqli_query($connect,$sql4);
$array4 = array();
while($row4 = mysqli_fetch_assoc($sel4)){
$array4[] = $row4;
}
<select name = 'test_tags[]' multiple>
<?php
foreach($array4 as $value4){ ?>
<option value ="<?php echo $value4['_id']; ?>" <?php echo in_array($value4['_id'], $test_tag) ? 'selected="true"' : null; ?>><?php echo $value4['score_type']; ?></option>
<?php
}
?>
</select>
so the the output that i want is, the values of the multiple select form should change depending on the choice on the dropdown menu...
i need a jquery.ajax code for this but i don't know where to begin... i am getting the value through a $_POST.. but i want to do it without going through another page and redirecting.
some helpful stuff:
AJAX Tutorial: W3schools ajax tutorial
then you need to learn about HTML DOM
and then you need to learn about Select DOM Object
mixing those will teach you how to figure out your question.
In the first dropdown #des_pos select event send the ajax post to the php page as follow.
$("#des_pos").select(function(e){
$.ajax({
url:"getdropdata.php",
dataType:"json",
success: function(data){
$.each(data,function(index,value){
$("#multiselect").append("<option value="+value+">"+value+"</option>"));
});
}
})
});

create dynamic dropdown list using append in javascript

Hi when I'm using static code into jquery for create dynamic dropdown list using append() then its working, like
$('#optionNameCity')
.empty()
.append('<option selected="selected" value="Wakad">Wakad</option><option value="Hinjewadi"> Hinjewadi </option>');
}
but when I'm using php script for create dynamic list then its failure and not showing any output, like
$('#optionNameCity')
.empty()
.append($(<?php include('db.php');$loc= mysql_query("select city from location");while($row=mysql_fetch_array($loc)){echo "<option selected='selected' value='".$row['city']."'>" . $row['city'] . "</option>";}?>));
}
Please guide me where I'm going wrong.
Thanks
<?php include('db.php');
$loc= mysql_query("select city from location");
$cityList="";
while($row=mysql_fetch_array($loc))
{
$cityList="<option selected='selected' value='".$row['city']."'>" . $row['city']."</option>";
}
?>
<?php if($cityList!=""){ ?>
$('#optionNameCity option').remove();
$('#optionNameCity).append("<?php echo $citylist; ?>");
<?php } ?>
Replace your code with above code

redirect to different URL with same form

i am new to PHP and trying to arrive with similar functionality given in URL.
i have a form with four select options, result will be retrived when last option selected.
my current code is :
{
$result = mysql_query($query,$con);
if(!$result)
echo mysql_error();
$option = "";
while($row = mysql_fetch_assoc($result)) {
$option .= '<option value = "'.str_replace(' ', '_', $row['bankname']).'">'.$row['bankname'].'</option>';
}
str_replace(' ', '_', $row['bankname'])
?>
<form method = "POST" action = "">
<select name = "bank" onChange="document.location.href=bank[selectedIndex].value">
<?php echo $option; ?>
</select>
</form>
}
Probably i am asking very high level question, but please help.
Regards,
Anitha
You can send the first time using ajax, and whem the ajax is sucess you can change the submit the form again returning true.
If I got the question right, you are not looking for loading options into select box, but trying to look for a solution where the last select box have values:
<select id = 'finalSelect'>
<option value='http://aaaa.com'>A<option>
<option value='http://bbbb.com'>B<option>
<option value='http://cccc.com'>C<option>
</select>
and based on the user selection your page should redirect to aaaa.com, bbbb.com or cccc.com.
If I am right about your requirement, all you have to do is:
<select id = 'finalSelect' ONCHANGE="location = this.options[this.selectedIndex].value;>
<option value='http://aaaa.com'>A<option>
<option value='http://bbbb.com'>B<option>
<option value='http://cccc.com'>C<option>
</select>

How can i select <select> <option> when pass an variable in the query string

suppose i have a select HTML element on the page. how can i select the option after passing a query string. such as computer.php?type=super2 it should select the super2 option in the select
<select>
<option value="Super">Super</option>
<option value="Super2">Super2</option>
<option value="Super3">Super3</option>
<option value="Super4">Super4</option>
</select>
can any one help. note: i need a static solution. i am not using any database. althou i am using PHP. any solution based on jquery or PHP ?
Here's how you can do it in PHP:
$type = $_GET['type'];
$options = array('Super', 'Super2', 'Super3', 'Super4');
echo '<select>';
foreach ($options as $option) {
echo "<option value='$option'" . ($option == $type ? ' selected' : '') . ">$option</option>";
}
echo '</select>';

Categories