How to make it select <script> function work? - php

I have a problem about select function. I have two file, one is frontend, one is backend. How I can make the select function to link with backend file?
Below is my coding:
Frontend file (user_list.php) ?f is represent backend file -
<script>
$("#merchant_type").click(function(){
$.ajax({
type: 'POST',
url: "?f=user_list",
if($("#merchant_type option:selected").text() == "Please Select"){
$("#merchant_id").val('Please Select');
}
});
});
</script>
Backend file (user_list.php)
<label for="cp8" class="control-label col-lg-4">Merchant</label>
<div class="col-lg-5">
<select class="form-control required" id="merchant_id" name="merchant_id">
<option value="" selected>Please Select</option>
<!--<option value="0" selected>All</option>-->
<?php
$sql_branch = 'SELECT * FROM merchant_list;';
$arr_branch = db_conn_select($sql_branch);
foreach ($arr_branch as $rs_branch) {
echo '<option value="' . $rs_branch['id'] . '">' . $rs_branch['shop_name'] . '</option>';
}
?>
</select>
</div><br><br><br>
<label for="cp8" class="control-label col-lg-4">Merchant Types</label>
<div class="col-lg-5">
<select class="form-control required" id="merchant_type" name="merchant_type" disabled>
<option value="0" selected>Please Select</option>
<option value="1">Event Sponsored</option>
<option value="2">Normal Merchant</option>
</select>
</div><br><br><br>
My output select option is below:
For example:
First I choose the Merchant option is "PALVO", then I choose the Merchant Types is "Event Sponsored". After that, if I want go back the Merchant to change other option, the Merchant Types will automatically turn to "Please Select" option. Now the select function can't work because can't link to the backend file.

On change merchant,merchant type will change to default
$(document).on("change","#merchant_id",function(){
$("#merchant_type").val('0');
})
Here Default value for merchant type is 0

Related

How to display an alert message if the value is blank in the form

I have a form (bootstrap) in the script and need it if the user does not select the language and the value "" is blank to display the alert message and that he had to choose a language to proceed on the next step.
(as you can see the empty value in the form: value" " represent "Select Language")
I am using that form for google widget tool for translation, and when the visitor dont click anything and proceed to save file into the database , in the database I get value: "Select language" , I need that they pick one of the languages from the dropdown menu.
This is the form code:
<div class="form-group">
<label for="recipient-name" class="col-form-label">Language:</label>
<input type="text" class="form-control langinput" id="country" value="" readonly>
<select class="form-control langselect" aria-label="Language Translate Widget" id="country" style="display: none">
<option value="">Select Language</option>
<option value="English">English</option>
<option value="Afrikaans">Afrikaans</option>
<option value="Albanian">Albanian</option>
<option value="Amharic">Amharic</option>
<option value="Arabic">Arabic</option>
here is the button code for saving files into the database:
<button style="margin-top: -5%" background-color="#357ae8" type="button" class="btn btn-success" data-toggle="modal" data-target="#exampleModal" data-whatever="#getbootstrap" onclick="lang1()">Save to Database</button>
lang1 Function:
var langPath = $("select.goog-te-combo option:selected").text();
/*document.getElementById("country").options[document.getElementById("country").selectedIndex].text = langPath;
document.getElementById("country").options[document.getElementById("country").selectedIndex].value = langPath;*/
document.getElementById("country").value = langPath;
//$(".langinput").value = langPath;
//$('input[type="text"][class="langinput"]').prop("value", "langPath");
//$('.langinput').text(langPath);
$('.langinput').attr('value', langPath);
//$(".langinput").display = "inline";
}
/*function stoppedTyping(){
if($('#subtitle').val().length > 1 && $('#country').val().length >0 ) {
document.getElementById('submit_button').disabled = false;
} else {
document.getElementById('submit_button').disabled = true;
}
}
function stoppedSelect(){
if($('#subtitle').val().length > 1 && $('#country').val().length >0 ) {
document.getElementById('submit_button').disabled = false;
} else {
document.getElementById('submit_button').disabled = true;
}
}*/
With bootstrap just add required to the select:
<select class="form-control langselect" aria-label="Language Translate Widget" id="country" style="display: none" required>
<option value="">Select Language</option>
<option value="English">English</option>
<option value="Afrikaans">Afrikaans</option>
<option value="Albanian">Albanian</option>
<option value="Amharic">Amharic</option>
<option value="Arabic">Arabic</option>
</select>
Edit
Change the buttons onclick to onsubmit:
<button style="margin-top: -5%" background-color="#357ae8" type="button" class="btn btn-success" data-toggle="modal" data-target="#exampleModal" data-whatever="#getbootstrap" onsubmit="lang1()">Save to Database</button>
Just recommending to rewrite this: <option value="">Select Language</option>
to this: <option selected disabled>Select Language</option>
When user clicks on the next button check if the select's value length is 0 and return it.

How the select function automatically turn into "Please Select" word?

I have a question about the "select" function. I have two input "select" functions. When I select the first input option, then I select the second input option. If I go back to the first input option to change other options, how can the second one automatically turn into "Please Select"word?
Below is my coding:
<label for="cp8" class="control-label col-lg-4">Merchant</label>
<div class="col-lg-5">
<select class="form-control required" id="branch_id3" name="branch_id3">
<option value="" selected>Please Select</option>
<!--<option value="0" selected>All</option>-->
<?php
$sql_branch = 'SELECT * FROM merchant_list;';
$arr_branch = db_conn_select($sql_branch);
foreach ($arr_branch as $rs_branch) {
echo '<option value="' . $rs_branch['id'] . '">' . $rs_branch['shop_name'] . '</option>';
}
?>
</select>
</div><br><br><br>
<label for="cp8" class="control-label col-lg-4">Merchant Types</label>
<div class="col-lg-5">
<select class="form-control required" id="merchant_type" name="merchant_type">
<option value="" selected>Please Select</option>
<option value="1">Event Sponsored</option>
<option value="2">Normal Merchant</option>
</select>
</div><br><br><br>
Below is my output picture:
Output
For example:
First I choose the Merchant option is "PALVO", then I choose the Merchant Types is "Event Sponsored". After that, if I want go back the Merchant to change other option, the Merchant Types will automatically turn to "Please Select" option. So that, my question is how to automatically make Merchant Types turn to "Please Select" option? Thanks for helping.
i hope this is what you looking for
$('#option').change(function(){
$('#type').val(0);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="option">
<option>Please Select</option>
<option>Option</option>
<option>Option1</option>
<option>Option2</option>
</select>
<select id="type">
<option value="0">Please Select</option>
<option>Type</option>
<option>Type1</option>
<option>Type2</option>
</select>
You can use jquery change for manipulating DOM element.
If you do not want to disable second select you can remove diabled attribute from select tag.
And also remove this from jquery code $("#select2").removeAttr('disabled');
$(document).ready(function(){
$("#select1").on('change',function(){
$("#select2").val('Please Select');
$("#select2").removeAttr('disabled');
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Select Value:
<select id="select1">
<option>Please Select</option>
<option>value1</option>
<option>value2</option>
<option>value3</option>
</select>
<select id="select2" disabled>
<option>Please Select</option>
<option>value1</option>
<option>value2</option>
<option>value3</option>
</select>
Use .val() on select DOM , and give option's value it will be selected.
With this check what is selected in first select then use .val()
$("#events").click(function(){
if($("#events option:selected").text() == "Reset First Select"){
$("#merchants").val('Please Select');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Merchants:
<select id="merchants">
<option>Please Select</option>
<option>Saab</option>
<option>Mercedes</option>
<option>Audi</option>
</select><br>
Event:
<select id="events">
<option>Reset First Select</option>
<option selected>Saab</option>
<option>Mercedes</option>
<option>Audi</option>
</select><br>
EDIT:
I have just added that when in second option reset first option will be selected it will select first

Best way to display image after user select one

Im wondering what is the best way to display an image after a user select one of the categories on the selection:
<form method="post" action="../user/posting.php">
<input type="text" name="title" required placeholder="Title"/>
<textarea name="body" rows="6" cols="50" placeholder="Details....."></textarea>
//Here is where the selection of the icon is
<select name="stat_icon" required>
<option name="fire" value="fire">Fire</option>
<option name="traffic" value="traffic">Traffic</option>
<option name="construction" value="construction">Construction</option>
<option name="crash" value="crash">Crash</option>
<option name="weather" value="weather">Weather</option>
<option name="robbery" value="robbery">Robbery</option>
<option name="deviation" value="deviation">Deviation</option>
<option name="police" value="police">Police</option>
</select>
<input type="text" name="location" required placeholder="Location">
<input type="file" name="img_stat">
<input type="submit" value="Stat!" />
</form>
I don't know what is the best way to display the image, if I have to upload the image first or I have to only set some if statements to select the image is in one of my folders. Im using php, html and for database mysql. Any suggestion
you can display an image using JQuery, ofcourse you have to upload all images to your images directory first.
$(document).ready(function() {
$("#imageSelector").change(function() {
var src = $(this).val();
$("#imagePreview").html(src ? "<img src='path/to/your/image/" + src + ".jpg'>" : "");
});
});
<select id="imageSelector" name="stat_icon" required>
<option name="fire" value="fire">Fire</option>
<option name="traffic" value="traffic">Traffic</option>
<option name="construction" value="construction">Construction</option>
<option name="crash" value="crash">Crash</option>
<option name="weather" value="weather">Weather</option>
<option name="robbery" value="robbery">Robbery</option>
<option name="deviation" value="deviation">Deviation</option>
<option name="police" value="police">Police</option>
</select>
<div id="imagePreview"></div>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
Ok so basically you should do this ( This suggestion is just an opinion form me , its a way that i use it ) :
You should have your imgs already saved in the database ( a name for the img so you can grape it from the folder ).
When the user pick a catalog from the forms you should have a php code that take the picked data and bring the data that compares with the user choice . i mean by the data is the Imgs .
This is an example from a code that i already have on my website , its easy , i used PDO instead of MYSQLI :
<form action="" method="post">
<select name="catalog" class="form-control input-lg" tabindex="1" required>
<option selected>Pick Your Favorite Catalog</option>
<?php // Fetching the catalogs names from the database
while($FCN = $FetchCatalogsName->FETCH(PDO::FETCH_ASSOC))
{
?><option value="<?php echo $FCN['catalog'];?>"><?php echo $FCN['catalog'];?></option><?php
}
?>
</select>
<br>
<input type="submit" name="filter" class="col-sm-4 form-control input-lg" value="Show Catalog">
</form>
Now we have to create the php code that will recive the img catalog from
<?php
include_once('lib/dbconnect.php');
if(isset($_POST['filter']))
{
$catalog = $_POST['catalog'];
$FetchImgs = $conn->prepare("SELECT * FROM picture WHERE catalog = ? "); // Fetching all Imgs that related to that catalog that the user has picked .
$FetchImgs->execute(array($catalog));
if($FetchImgs)
{
?>
<?php while($FI = $FetchImgs->fetch(PDO::FETCH_ASSOC))
{
?>
<div class="col-md-3">
<img src="uploading/<?php echo $FI['Filename']; ?>"> // $FI['Filename'] this will return the name of the img from the database .
</div>
<?php }
}
else
{
echo "something went wrong ";
}
}
?>
Note : in my case the php code is in the same page as the html form and its directly under it .
I don't know what is the best way to display the image, if I have to upload the image first or I have to only set some if statements to select the image is in one of my folders.
If your sole purpose is to display the image based on the selection, then there's no need to upload any images. The only catch here is, your image names should correspond to value attributes of select input. Also, image extension should be consistent.
So first of all, remove this line <input type="file" name="img_stat"> from your form and after form submission, display the appropriate image like this:
$image_path = '/yourImagePath/' . $_POST['stat_icon'] . '.jpg';
echo '<img src="' . $image_path . '" alt="stat_icon" />';
Note(s):
Change .jpg as per your image extensions
Change /yourImagePath/ and give the appropriate directory path

Empty query result

in a web form there are two drop-down lists. The second list items should change dynamically depending on the value selected on the first drop-down list.
This is how am I trying to do it:
index.php:
...
<script>
function getClient(val) {
$.ajax({
type: "POST",
url: "get_contacts.php",
data:'client_id='+val,
success: function(data){
$("#contacts-list").html(data);
}
});
}
</script>
...
<div class="form-group">
<label for="mto_client" class="col-sm-2 control-label">MTO Client</label>
<div class="col-sm-10">
<select name="mto_client" id="clients_list" onChange="getClient(this.value)">
<option value="">Select a Client</option>
<?php
do {
?>
<option value="<?php echo $row_RSClients['id_client']?>" ><?php echo $row_RSClients['client_name']?></option>
<?php
} while ($row_RSClients = mysql_fetch_assoc($RSClients));
?>
</select>
</div>
</div>
<div class="form-group">
<label for="mto_client_contact" class="col-sm-2 control-label">MTO Client Contact</label>
<div class="col-sm-10">
<select name="state" id="contacts-list">
<option value="">Select Client Contact</option>
</select>
</div>
</div>
get_contacts.php
<?php
require_once("dbcontroller.php");
$db_handle = new DBController();
if(!empty($_POST["client_id"])) {
$query ="SELECT * FROM tb_client_contacts WHERE contact_client_id = '" . $_POST["client_id"] . "'";
$results = $db_handle->runQuery($query);
?>
<option value="">Select Client Contact</option>
<?php
foreach($results as $state) {
?>
<option value="<?php echo $state["id_client_contact"]; ?>"><?php echo $state["contact_name"]; ?></option>
<?php
}
}
?>
There are objects on the table tb_clients_contact that meet the condition, but the second drop-down list doesn't show any objects.
Any help is welcome.
Instead of
$("#contacts-list").html(data);
It should be
$('#contacts-list').empty().append(data);
empty() will clear first the options inside the contacts-list select field, then append() will insert the options from the result of your AJAX.
You can also look at the console log for errors. If you are using Google Chrome, hit F12 to display the console log.

Show/hide select values based on previous select choice

I have 2 select's inside a form. The second select has about 2000 lines in total coming out of my mysql table. One of the column into that mysql has 1 of the values used into the first select. I want to be able to filter on that value when it is selected into the first select, so that it only shows these articles.
code now:
<div class="rmaform">
<select name="discipline" class="discipline">
<option value=" " selected></option>
<option value="access">ACCESS</option>
<option value="inbraak">INBRAAK</option>
<option value="brand">BRAND</option>
<option value="cctv">CCTV</option>
<option value="airphone">AIRPHONE</option>
<option value="perimeter">PERIMETER</option>
</select>
</div>
<div class="rmaform">
<select name="article" class="input-article">
<?php
$articleselect = $dbh->prepare('SELECT * FROM articles');
$articleselect->execute();
while($articlerow = $articleselect->fetch(PDO::FETCH_ASSOC)){
?>
<option value="<?php echo $articlerow['a_code'];?>"><?php echo $articlerow['a_code'];?> <?php echo $articlerow['a_omschr_nl'];?></option>
<?php
}
?>
</select>
I think i have to use Javascript for it but how do you combine PHP and Javascript? And what would be the best way to make the filter work?
jQuery for the change event and AJAX
$(document).ready(function(e) {
$('select.discipline').change(function(e) { // When the select is changed
var sel_value=$(this).val(); // Get the chosen value
$.ajax(
{
type: "POST",
url: "ajax.php", // The new PHP page which will get the option value, process it and return the possible options for second select
data: {selected_option: sel_value}, // Send the slected option to the PHP page
dataType:"HTML",
success: function(data)
{
$('select.input-article').append(data); // Append the possible values to the second select
}
});
});
});
In your AJAX.php
<?php
if(isset($_POST['selected_option']))
$selected_option=filter_input(INPUT_POST, "selected_option", FILTER_SANITIZE_STRING);
else exit(); // No value is sent
$query="SELECT * FROM articles WHERE discipline='$selected_option'"; // Just an example. Build the query as per your logic
// Process your query
$options="";
while($query->fetch()) // For simplicity. Proceed with your PDO
{
$options.="<option value='option_value'>Text for the Option</option>"; // Where option_value will be the value for you option and Text for the Option is the text displayed for the particular option
}
echo $options;
?>
Note: You can also use JSON instead of HTML for much simplicity. Read here, how to.
First give both of the selects an unique ids...
<div class="rmaform">
<select name="discipline" class="discipline" id="discipline">
<option value="" selected></option>
<option value="access">ACCESS</option>
<option value="inbraak">INBRAAK</option>
<option value="brand">BRAND</option>
<option value="cctv">CCTV</option>
<option value="airphone">AIRPHONE</option>
<option value="perimeter">PERIMETER</option>
</select>
</div>
<div class="rmaform">
<select name="article" class="input-article" id="article">
<option value="" selected></option>
</select>
Now you can use jQuery Ajax call to another file and get the HTML Response from that file and Populate in the select field with id="article" like this...
<script type="text/javascript">
$(document).ready(function(){
$("#discipline").change(function(){
var discipline = $(this).val();
$.post(
"ajax_load_articles.php",
{discipline : discipline},
function(data){
$("#article").html(data);
}
)
});
});
</script>
Now create a new file like ajax_load_articles.php... in the same directory where the html file exists... You can place it anywhere but then you have to change the $.post("url", the url to the ajax submission.
Contents of ajax_load_articles.php :
<?php
$discipline = $_POST["discipline"];
$articleselect = $dbh->prepare("SELECT * FROM articles WHERE colname = '{$discipline}'");
$articleselect->execute();
echo '<option value="" selected></option>';
while($articlerow = $articleselect->fetch(PDO::FETCH_ASSOC)){
?>
<option value="<?php echo $articlerow['a_code'];?>"><?php echo $articlerow['a_code'];?> <?php echo $articlerow['a_omschr_nl'];?></option>
<?php
}
?>
Now when ever you will change the select of the first select field an Ajax call will take place and the data of the second select field will automatically adjust to related data selected in the first select field.
This is an example where you can select the college specific to the state ..
Form.php
// your code for form ...
// select box for state starts
<div class="form-group">
<label for="select" class="col-lg-2 col-md-2 control-label">State</label>
<div class="col-lg-10 col-md-10">
<select class="form-control input-sm" name="statename" id="stateid">
<option value="">---- Select ----</option>
<?php
$state=sql::readResultArray("Your Query To select State`");
foreach($state as $s){
?>
<option value="<?php echo $s?>"> <?php echo $s ?> </option>
<?php
}
?>
</select>
</div>
</div>
<div class="form-group">
<label for="select" class="col-lg-2 col-md-2 control-label">Colleges</label>
<div class="col-lg-10 col-md-10">
<select class="form-control input-sm" name="mycollegename">
<option value="">--- Select --- </option>
</select>
</div>
</div>
// further code for from in form.php..
Script to find the state and then pass the value to find the respective colleges in that state
<script>
$(document).ready(function(e) {
$('#stateid').change(function()
{ ids=$('#stateid').val();
$.get('mycollege.php', {type:ids} ,function(msg){
$('select[name="mycollegename"]').html(msg);
});
});
});
</script>
Call this file through ajax .. Code on this file
mycollege.php
<?php
require('db.php'); // call all function required, db files or any crud files
$type=$_GET['type'];
if(!empty($type)){
$r=sql::read("Your Query To call all teh college list");
?>
<select name="mycollegename">
<option value="">Select One</option>
<?php
foreach($r as $r){
echo "<option value=\"$r->collegename\">$r->collegename </option>";
}
?>
</select>
<?php }else{ ?>
<select name="mycollegename">
<option value="">Select State</option>
</select>
<?php } ?>

Categories