I am a student and also working in my university. I am not studying computer science.
I mainly do support stuff, but now I should take care of our internship database.
Task: Create a drop down menu from a town which a student choses, then show all internships in this town.
I have an existing SQL database, can create a drop down menu from it, but have NO idea how I can use the selected value. A submit button would be fine, any solution which works is fine.
How can I put the chosen value from drop down menu to a PHP variable?
<?php
$dbanfrage = "SELECT DISTINCT Stadt FROM $tabelle WHERE Land = 'China' OR Land = 'Taiwan' OR Land = 'VR China' ORDER BY STADT ";
$result = mysql_db_query ($dbname, $dbanfrage, $dbverbindung);
if (!$result)
{
$message = 'Ungültige Abfrage: ' . mysql_error() . "\n";
$message .= 'Gesamte Abfrage: ' . $dbanfrage;
die($message);
}
echo "<select>";
echo "<option>Stadt auswählen</option>"; // printing the list box select command
while($nt = mysql_fetch_array($result)) //Array or records stored in $nt
{
echo "<option value = $nt[id]>$nt[Stadt]</option>"; /* Option values are added by looping through the array */
}
echo "</select>"; // Closing of list box
mysql_close ($dbverbindung);
?>
Just suggesting how you can do it --
<select name="country" id="country" onChange="showState();" >
<option value="">Select Country</option>
<option value="1">India</option>
<option value="2">Nepal</option>
</select>
Your Script--
<script type="text/javascript">
function showState( )
{
var value = document.getElementById('country').value;
var url = '<?php echo base_url ?>showstate.php';
$.ajax({
type: "GET",
url: url,
data:{'country':value},
success:function(results)
{
$('#div_state').html(results);
}
});
}
</script>
Your showstate.php php page --
//INCLUDE CONNECTION file to for db query
$type = $_GET['country'];
//Your DB QUERIES
This will load the content in your #div_state div. You can also use Jquery For this..i think this will help you :) an d let me know if you have any query.
<select name="name">
<option value="select">select</option>
<?php
$dbanfragee=mysql_query("SELECT DISTINCT Stadt FROM $tabelle WHERE Land = 'China' OR Land = 'Taiwan' OR Land = 'VR China' ORDER BY STADT ");
while($row=mysql_fetch_array($$dbanfrage)){?>
<option value="<?=$row['id']?>"><?=$row['stadt']?></option>
<?php }?>
</select>
Related
I make a dropdown for a form, I will show the code below. However, when I click the submit button, there is an error saying,
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'brand' cannot be null (SQL: insert into supplier_details.
The data that I chose from the dropdown is actually null. Actually, I'm new to Laravel.
I don't want to make a dropdown list from a database, I just want to display the option and the option will be inserted into the database when the user clicks the submit button after filling in the form.
<div class="form-group row">
<label style="font-size: 16px;" for="id" class = "col-sm-2">Item Brand </label>
<label for="supp_name" class = "col-sm-1">:</label>
<div class="col-sm-7">
<select name="brand" class="form-control js-example-basic-single" required>
<option >Please select item brand</option>
<option value="machine1"> Item Brand 1 </option>
<option value="machine1"> Item Brand 2 </option>
<option value="machine1"> Tem Brand 3 </option>
</select>
</div>
</div>
Controller
public function createsupplierdetails()
{
return view ('frontend.praiBarcode.getweight');
}
public function supplierdetails(Request $r)
{
$details = new SupplierDetail;
$getuserPO = Supplier::where('PO',$r->PO)->first();
$details->brand = $getuserPO->brand;
$details->container_no = $getuserPO->container_no;
$details->date_received = $getuserPO->date_received;
$details->gross_weight = $getuserPO->gross_weight;
$details->tare_weight = $getuserPO->tare_weight;
$details->net_weight = $getuserPO->net_weight;
$details->save();
return view ('frontend.praiBarcode.viewsupplierdetails')
->with('details',$details);
}
This to check to verify if it is working:
Make sure you are submitting the correct form.
Try doing dd on your controller dd($request->all())
If data is reaching the controller and not inserted into the database, check on your model, if it is added to fillable or if there is only id in the guarded array. You can know about more here in https://laravel.com/docs/9.x/eloquent#mass-assignment
Error should be fixed, as soon as you fix it.
Controller
use Validator;
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'brand' => 'required',
]);
if ($validator->fails()) {
return redirect()->back()->with('error', $validator->errors()->first());
}
$details = new SupplierDetail();
$details->brand = $request->brand;
$details->container_no = $request->container_no;
$details->date_received = $request->date_received;
$details->gross_weight = $request->gross_weight;
$details->tare_weight = $request->tare_weight;
$details->net_weight = $request->net_weight;
$details->save();
if ($trending) {
return redirect(route('details.index'))->with('success', 'Field added successfully');
} else {
return redirect()->back()->with('error', 'Field has been not added successfully');
}
}
Im querying data which is pulled in via an Axios call. The drop-down 'subjects' pulls back results and queries the data but I would like it to only pull back exacts. For example, if I select 'English', I just want it to return subjects which have the subjects 'English' and not the subjects which are 'English and maths'.
Would I use a regEx. If so how would I go about this? Any help appreciated.
<select v-model="subject"
class="form-control"
#change="subjectonchange()"
:disabled="selectDisabledSubject"
>
<option disabled value="">Select subject</option>
<option
v-for="subject in uniquesubjects"
:key="subject"
:value="subject"
>
{{ subject }}
</option>
</select>
method: { subjectonchange: function () {
let query = "";
if (this.subject !== "") {
query = this.subject;
console.log(this.subject);
} else {
query = "!showall";
}
this.query(query);
},}
Hello guys I have a query that need a variable to be executed but that variable is returned by a select on a form
$obtermaxcampo = "SELECT P$pagina as maxcampo FROM slider_settings where ID = $ID";
if I define $pagina = 1 it only query me the P1 and I have 12 P's.
My question is if theres any way to start the variable in 1 but after submit delete that definition and give him a $pagina = $_POST['selectedPagina'];
here is the selected form
<form action="" method="POST" name="carregarinfo">
<label>Pagina</label>
<select name ="selectedPagina" id="selected" >
<?php for ($k = 1; $k <= $result['max']; $k++){?>
<option value ="<?php echo $k;?>" <?php if($_POST['selectedPagina'] == $k){echo "selected='$k'"; }?>><?php echo $k;?></option>
<?php
}
?>
</select>
<label>Campo</label>
<select name ="selectedCampo" id="selected" >
<?php for ($z = 1; $z <= $resultmaxcampo['maxcampo']; $z++){?>
<option value ="<?php echo $z;?>"><?php echo $z;?></option>
<?php
}
?>
</select>
<input type="submit" name="carregar" id="carregar" value="Carregar">
</form>
if(isset($_POST['carregar'])){
$sql = "SELECT tituloP$pagina as titulopag, Butao, Titulo, Texto FROM slider_settings, slider_config where slider_settings.ID = $ID and slider_config.P_NUM = $pagina and slider_config.Campo = $campo and slider_config.ID = $ID";
$sqlconnect = $connect->query($sql);
$sqlresult = $sqlconnect->fetch_assoc();
$pagina = $_POST['selectedPagina'];
$obtermaxcampo = "SELECT P$pagina as maxcampo FROM slider_settings where ID = $ID";
$connectionmaxcampo = $connect->query($obtermaxcampo);
$resultmaxcampo = $connectionmaxcampo ->fetch_assoc();
}
I came across with this issue during my work process and couldn't find any logical solution.
Firstly, I have two dropdown menus. One for countries, other for cities. As Materialize converts select element into ul, thus I have two ul-s instead.
<select name="res_country" id="res_country">
<option selected="" disabled="">Ölkə</option>
<?php
foreach ($countries as $country) {
print "<option value='".$country['id']."'> ".$country["text"]."</option>";
}
?>
</select>
<select name="res_city" id="res_city">
<option selected="" disabled="">Şəhər</option>
<?php
foreach ($cities as $city) {
print "<option parent=".$city['country_id']." value=".$city['id'].">".$city['text']."</option>" ;
}
?>
</select>
I wanted to dynamically set options of second dropdown according to which country I selected in the first one.
Yes, I have already read about that I should firstly destroy and then create dropdowns after modifiying them. But this didn't work.
I fixed this problem by writing below code in JS.
$('#res_country') stands for first dropdown with countries, $('#res_city') for the second with cities.
$('#res_country').on('change',function () {
var country_id = $('#res_country option:selected').val();
var indexes = [];
//
$('#res_city option').each(function()
{
if($(this).attr('parent') == country_id){
var val = $('#res_city option').index($(this));
indexes.push(val);
}
});
console.log(indexes);
$('#res_city').prev().children().hide();
for(var i=0; i<indexes.length; i++){
$('#res_city').prev().children().eq(indexes[i]).show();
}
});
But I am not satisfied with such solution. I wonder if any of you had similar situation?
I am using DataTables and I would like my length(select option) to outside of the table
(ex. on my div).
create new select form
<select name='length_change' id='length_change'>
<option value='50'>50</option>
<option value='100'>100</option>
<option value='150'>150</option>
<option value='200'>200</option>
</select>
init dataTables
var oTable = $('#example').DataTable({});
set initial value
$('#length_change').val(oTable.page.len());
add function .change
$('#length_change').change( function() {
oTable.page.len( $(this).val() ).draw();
});
reference : https://datatables.net/reference/api/page.len()
It cannot be directly moved by just copying the whole change length drop down outside the table.
Instead create a new drop-down, where ever you want but set the following in the datatable call -
<select name='length_change' id='length_change'>
<option value='50'>50</option>
<option value='100'>100</option>
<option value='150'>150</option>
<option value=''>All</option>
</select>
`var oTable = $('#sample_1').dataTable( {
.....
"bLengthChange": false, //This will disable the native datatable length change
.....
...
"fnServerParams": function ( aoData ) {
aoData.push( { "name": "length_change", "value": $('#length_change').val() } );
},
.....
....
});
`
The `aoData.push` will send the selected value of the customer length change to the server.
In the Model Class from where the array will be returned for the datatable, include the pushed value to the limit.
i.e. if `$postData` is the array of posted values to the server then -
`if($postData['length_change'])
$limit = (int) $postData['length_change'];
else
$limit = _DEFALUT_VALUE;
`
I hope it helps.