How multiselect post in array? - php

This my code. How this multiselect values with array to post modelseardh.php
<p>
<label>Country:</label>
<select multiple="multiple" name="country[]" class="qs_carlocation1 carlocations" onChange="jsFunction()" id="selectOpt">
<?php
$sql_se = mysql_query('select * from `country`');
while($se = mysql_fetch_assoc($sql_se)) {
?>
<option value = "<?php echo $se['id'];?>"><?php echo $se['country'];</option>
<?php
}
?>
</select>
</p> `
<p id="change_form">
<label>Mark:</label>
<select multiple="multiple"name="search_mark[]"class="qs_carlocation1 carlocations" >
<?php
$sql_search_mark = mysql_query('select * from `mark`');
while($smark = mysql_fetch_assoc($sql_search_mark)) {
?>
<option value="<?php echo $smark['id'];?>"><?php echo $smark['name'];?></option>
<?php
}
?>
<script>
function jsFunction(){
var select = document.getElementById("selectOpt").value;
$.post(
"modelsearch.php",
{
id: "" + select + ""
},
function(data)
{
$('#change_form').html(data);
}
);
}
</script>
</select>
</p>
<?PHP
//this modelsearch.php
include('../db.php');
$id = $_POST['id'];
?>

There's wrong with enclosing & misplaced tag in the following line that possibly make the rendering ouput fails:
<option value = "<?php echo $se['id'];?>"><?php echo $se['country'];"></option>
which is missing ?>"> right before the </option>, and you need to remove "> between ?> <?php
it should be :
<option value = "<?php echo $se['id'];?><?php echo $se['country'];?>"></option>
and you also don't ouput any values between <option> xxx </ouput>, so the value attribute is assigned in the <option> but nothing to be ouput. I meant that xxx to be output
and FYI , you dont need to multiple echo the values, just concatenate the values :
<option value = "<?php echo $se['id'] +""+ $se['country'];?>" ></option>

First of all, you don't need to keep the country name in Array change country[] to country only
<script type="text/javascript" src="jquery-1.11.1.min.js"></script>
<script language='javascript'>
function showselection()
var frm = document.testingform //this is the name of the form can be as your form name
var opt = frm.country //this is the multiselect option name
var numofoptions = opt.length
var selValue = new Array
var j = 0
for (i=0; i<numofoptions; i++)
{
if (opt[i].selected === true)
{
selValue[j] = opt[i].value
j++
}
}
var myvalues=document.getElementById('yourhiddenvaluename').value=selValue;
$.post( 'modelsearch.php',{ newva: myvalues }, function( data ) {
$('#change_form').html(data)
});
}
</script>
<select name='country' multiple onchange='showselection()'>
//Your values
</select>
<input type="text" id="yourhiddenvaluename" name="seletedvalued" />
In modelsearch.php:
print_r($_POST['newva']);

Related

Dependent combo boxes in PHP and Ajax

I need help with my code. I want to select values from the database based on the first combo box selection.
The first combo box has subject ID, which I want to use in my SQL to find the value of the second combo box.
<form class ="form_group" id="form1" enctype="multipart/form-data" method="POST" action="uploadstudent.php">
StudentID:<br>
<input type="text" name="StudentID" value="<?php echo $_SESSION['login_user'];?>"readonly>
<br><br>
Subject ID:<br>
<select id="soflow" name="SubjectID" onChange="getState(this.value);">
<option>
<?php
mysql_connect("localhost", "id503120_course", "12345678");
mysql_select_db('id503120_course_db');
$StudentID=$_SESSION['login_user'];
$Course = mysql_query("SELECT course FROM student WHERE StudentID = '".$StudentID."'");
$result = mysql_fetch_assoc($Course);
$newcourse = implode($result);
$query=mysql_query("SELECT SubjectID FROM subjects WHERE Course= '".$newcourse."' AND SubStatus = 'Y'");
if(!$numrows=mysql_num_rows($query)==0)
{
while($row=mysql_fetch_assoc($query))
{ ?>
<option value="<?php echo $row['SubjectID']; ?>">
<?php echo $row['SubjectID']; ?>
</option>
<?php }
}
else{
echo "No submissions are currently open for you";
}
?>
</select>
<script>
function getState(val) {
$.ajax({
type: "POST",
url: "submission.php",
data:'SubjectID='+val,
success: function(data){
$('#soflow2').html(data);
}
});
}
</script>
Assign number: <br><select id="soflow2" name="AssessmentNum">
<option>
<?php
mysql_connect("localhost", "id503120_course", "12345678");
mysql_select_db('id503120_course_db');
if(!empty($_POST["SubjectID"])) {
$query ="SELECT AssignNum FROM subjects WHERE SubjectID = '".$_POST['SubjectID']."'";
while($row=mysql_fetch_assoc($query))
{ ?>
<option value="<?php echo $row['AssignNum']; ?>">
<?php echo $row['AssignNum']; ?>
</option>
<?php }
}
?>
Here in assign number, I am not able to view any information at all.
Please help me.
The problem is in the parameter SubjectID in the ajax request. try this:
function getState(val) {
$.ajax({
type: "POST",
url: "submission.php",
data:{'SubjectID' :val},
success: function(data){
$('#soflow2').html(data);
}
});
}

How to get the selected dropdown value on submit in Laravel?

In Laravel, I am trying to get the value of selected drop-down value and send it to another controller. And by using that drop-down value below values need to be changed by fetching the data from the database.
Below is the code I have tried.
<form action="{{{ url("/getDetailsBynumber/$_POST['number']") }}}" method="post">
<select name="number" id="number">
<option selected="selected">Select number id</option>
<?php
$numberArray = json_decode($number_id, true);
for ($i = 0 ; $i<=$number_count ; $i++) {
?>
<option value="<?php echo $numberArray[$i]["number_value"] ?>"><?php echo $numberArray[$i]["number_value"]; ?></option>
<?php
}
?>
</select>
<input type="submit" value="Submit">
You need to add name attribute to select tag. As a result when form submitted it will send selected value with it's name. For example:
<select name="product_id">
<option value="1">Cup</option> <!-- Suppose this option selected -->
<option value="2">Pen</option>
<option value="3">Book</option>
</select>
If you submit form, you can get selected value in your controller as follows:
public function methodName(Request $request)
{
// $request->product_id is name attribute of your select tag
print_r($request->product_id); // It will print out 1 which is value of Cup
}
<form action="{{ url("/getDetailsBynumber/") . $_POST['number'] }}" method="post">
<select name="number" id="number">
<option selected="selected">Select number id</option>
<?php
$numberArray = json_decode($number_id, true);
for ($i = 0 ; $i<=$number_count ; $i++) {
?>
<option value="<?php echo $numberArray[$i]["number_value"] ?>"><?php echo $numberArray[$i]["number_value"]; ?></option>
<?php
}
?>
</select>
<input type="submit" value="Submit">
This is my code to get state name after submit if validation fail incase then it works
on load of page you can get the country id like this
var countryid = $('#opt_country').val();
after that base on that you have to find state_id
like this
if (countryid != '')
{
$.ajax({
url: base_path + 'getstate',
type: "POST",
data: {in_country_id: countryid},
async: true,
cache: false,
success: function (statedata) {
var obj = jQuery.parseJSON(statedata);
console.log(obj);
if (obj.SUCC_FLAG == 1)
{
for (var i in obj)
{
console.log(obj);
var id = obj[i].in_state_id;
var name = obj[i].st_state_name;
if (id == stateid)
{
$("#opt_state").append("<option value='" + id + "' selected >" + name.toString() + "</option>");
} else {
$("#opt_state").append("<option value='" + id + "'>" + name.toString() + "</option>");
}
}
}
else
{
confirm('We are unable to load State');
}
},
error: function () {
alert("server error");
}
});
}
and after that you can get the state so here is my example to get state after submit the page

Select dropdown item and redirect to different page

My Code:
select name="accttype">
<?php foreach($query as $row)
{
$act=$row->accounttype;
?>
<option value="<?php echo$act;?>"><?php echo$act;?></option>
<?php
}
?>
</select>
When I select the dropdown list I want to to redirect different functions to load different pages, like:
<option value="<?php echo site_url('Controller_n/function'><?php echo$act;?></option>
Is this possible?
Check this sample:
<select id="accttype" name="accttype">
<?php foreach($query as $row)
{
$act=$row->accounttype;
?>
<option value="<?php echo $act ?>"><?php echo $act ?></option>
<?php } ?>
</select>
<script>
$(function(){
// bind change event to select
$('#accttype').on('change', function () {
var url = $(this).val(); // get selected value
if (url) { // require a URL
window.location = url; // redirect
}
return false;
});
});
</script>
Give a id to your select like below
<select id="accttype">
<?php foreach($query as $row)
{
$act=$row->accounttype; ?>
<option value="<?php echo$act;?>"><?php echo$act;?></option>
<?php } ?>
</select>
// add jquery library if you not added already
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#accttype').change(function(){
window.location = $(this).val();
});
});
</script>
Hope it will help you.

Datalist onkeyup not working via ajax value

I'm stuck with this problem with in a week. I tried to set a onkeyup on datalist, only allow to submit within datalist autocomplete option value. I tried 2 different script but it has the same problem. When my option value is came via ajax, my textbox not allowing to type even if the value is in the option list. Why it is? Help please. I'm stuck with this :/
When I tried to echo the option list just like this
<input list="languages" id="none"></input>
<datalist id="languages" name="options">
<option value=""></option>
<?php echo $option1; ?>
</datalist>
the onkeyup works well. But when the value is came from ajax, the problem comes in. Why is that? Please help me with this.
index.php
Drop1
<?php
$mysqli = new mysqli("localhost", "root", "", "2015");
$combo = $mysqli->query("SELECT * FROM category GROUP BY cat_code ORDER BY id");
$option = '';
while($row = $combo->fetch_assoc())
{
$option .= '<option value = "'.$row['category'].'">'.$row['category'].'</option>';
}
?>
<select id="main" name="main">
<option value="" disabled="disabled" selected="selected">Choose</option>
<?php echo $option; ?>
</select>
<span id="result"> <input list="languages" id="none"></input>
<datalist id="languages" name="options">
<option value=""></option>
</datalist></span>
<input type="submit" name="submit" value="Submit"/>
<script type="text/javascript">
$('#main').change(function () {
$.ajax({
url: 'getajax.php',
data: {
mainlist_id: $(this).val()
},
dataType: 'html',
type: 'POST',
success: function (data) {
$('#languages').html(data);
}
});
});
</script>
<script>
$(document).ready(function() {
var validOptions =[];
$("#languages option").each(function(){
validOptions.push($(this).val())
});
$("#none").autocomplete(validOptions, { mustMatch: true });
});
$('input#none').result(function(event, data, formatted) {
$("#result").html(!validOptions ? "No match found!" : "Selected: " + formatted);
}).keyup(function() {
$(this).search();
$(this).css("background-color", "#D6D6FF");
});
</script>
Ajax
<?php
if (isset($_POST["mainlist_id"])) {
$mysqli = new mysqli("localhost", "root", "", "2015");
$main = $mysqli->real_escape_string($_POST["mainlist_id"]);
$result1 = $mysqli->query("SELECT * FROM code WHERE cat_code='$main' GROUP BY item_code ORDER BY item");
$option1 = '';
while($row = $result1->fetch_assoc())
{
$option1 .= '<option value = "'.$row['item'].'">'.$row['item'].'</option>';
}
echo $option1;
}
?>
Try this:
success: function (data) {
$('#languages').html(data);
$("#languages option").each(function(){
validOptions.push($(this).val())
});
}

selected item in query fro dropdown list in php

Here is my code I made a script to get values in "list_cust_city" from selected item in "list_cust_name" through query in php. I didnt get any values in "list_cust_city" for city. I made city.php.
<script>
$('#list_cust_name').change(function(){
alert("heyyy");
$.ajax({
url:'city.php',
data:{cust_name:$( this ).val()},
success: function( data ){
$('#list_cust_city').html( data );
}
});
});
</script>
<label style="color:#000">Name </label>
<?php
$query_name = "SELECT DISTINCT cust_name FROM customer_db ORDER BY cust_name"; //Write a query
$data_name = mysql_query($query_name); //Execute the query
?>
<select id="list_cust_name" name="list_cust_name">
<?php
while($fetch_options_name = mysql_fetch_assoc($data_name)) { //Loop all the options retrieved from the query
$customer=$fetch_options_name['cust_name'];
?>
<option value="<?php echo $fetch_options_name['cust_name']; ?>"><?php echo $fetch_options_name['cust_name']; ?></option>
<?php
}
?>
</select>
city.php
<body>
<?php
include('dbconnect.php');
db_connect();
$cust_name1=$_GET['cust_name']; //passed value of cust_name
$query_city = "SELECT DISTINCT cust_city FROM customer_db WHERE cust_name='$cust_name1'ORDER BY cust_city"; //Write a query
$data_city = mysql_query($query_city); //Execute the query
while($fetch_options_city = mysql_fetch_assoc($data_city)) { //Loop all the options retrieved from the query
?>
<option value="<?php echo $fetch_options_city['cust_city']; ?>"><?php echo $fetch_options_city['cust_city']; ?></option>
<?php
}
?>
</body>
You must use document ready because the DOM isn't load.
$( document ).ready(function() {
$('#list_cust_name').change(function(){
alert("heyyy");
$.ajax({
url:'city.php',
data:{cust_name:$( this ).val()},
success: function( data ){
$('#list_cust_city').html( data );
}
});
});
});
PHP uses . to concat strings. Change your query to:
$query_city = 'SELECT DISTINCT cust_city FROM customer_db WHERE cust_name="'.$cust_name1.'"ORDER BY cust_city';
Also add this to your first php file:
<select id="list_cust_city" name="list_cust_city"></select>
Here's full code.
php 1:
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(function() {
$('#list_cust_name').change(function(){
$.ajax({
url:'city.php',
data:{cust_name:$( this ).val()},
success: function( data ){
$('#list_cust_city').html( data );
}
});
});
});
</script>
<label style="color:#000">Name </label>
<?php $data_name = mysql_query("SELECT DISTINCT cust_name FROM customer_db ORDER BY cust_name");?>
<select id="list_cust_name" name="list_cust_name">
<?php while($fetch_options_name = mysql_fetch_assoc($data_name)) { ?>
<option value="<?php=$fetch_options_name['cust_name']; ?>"><?php=$fetch_options_name['cust_name']; ?></option>
<?php } ?>
</select>
<select id="list_cust_city" name="list_cust_city"></select>
city.php:
<?php
include('dbconnect.php');
db_connect();
$cust_name1=$_GET['cust_name'];
$data_city = mysql_query('SELECT DISTINCT cust_city FROM customer_db WHERE cust_name="'.$cust_name1.'" ORDER BY cust_city');
while($fetch_options_city = mysql_fetch_assoc($data_city)) {
?>
<option value="<?php=$fetch_options_city['cust_city'];?>"><?php=$fetch_options_city['cust_city'];?></option>
<?php
}
?>

Categories