$_GET to accept values with space - javascript

I checked all the possible solutions here, unfortunately it won't work. What seems to be the problem with my code?
I tried the following.
urldecode (http://php.net/manual/en/function.urldecode.php)
str_replace http://php.net/manual/en/function.str-replace.php)
But no luck.
I tried the following in isset
$accounttitle = $_GET['accounttitle'];
urlencode($_GET["accounttitle"])
$accounttitle = str_replace(" ", "", $accounttitle );
Here's my isset
if(isset($_GET['accounttitle'])){
$accounttitle = $_GET['accounttitle'];
} ?>
Here's my form
<div class="box-header with-border">
<!--<i class="fa fa-plus"></i> New -->
<div class="form-group">
<?php
$sql = "SELECT accountcode, accounttitle, accounttype FROM earningsamendmentaccount";
$query = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
?>
<label for="select_account_title" class="col-sm-3 control-label">Select Account Title</label>
<div class="col-sm-9">
<select class="form-control" id="select_account_title" style="text-transform:uppercase" required>
<option value="">PLEASE SELECT OPTION</option>
<?php
while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC))
{
echo "<option value=".$row['accounttitle'].">".$row['accounttitle']."</option>";
}
?>
</select>
</div>
</div>
</form>
Here's my script
$(function(){
$('#select_account_title').change(function(){
window.location.href = 'earnings_amendment.php?accounttitle='+$(this).val();
});
});
</script>
I can echo() but it won't work on values with space.

Your problem is not receiving $_GET but rather with your outgoing link.
<select class="form-control" id="select_account_title" style="text-transform:uppercase" required>
<option value="">PLEASE SELECT OPTION</option>
<?php while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)): ?>
<option value="<?= urlencode($row['accounttitle'])?>"><?= $row['accounttitle'] ?></option>
<?php endwhile; ?>
</select>

I prefer trim 'trim()' php function not string replace.

Related

Insert data that doesnt exist from selectbox

I would want a select box that can insert data if the data typed doesn't exist in the DB, could anyone help me, please?
<div class="form-group">
<label for="">Catégorie</label>
<select class="form-control select2" name="category" required>
<?php
$select = $pdo->prepare("SELECT * FROM tbl_category");
$select->execute();
while($row = $select->fetch(PDO::FETCH_ASSOC)){
extract($row)
?>
<option><?php echo $row['cat_name']; ?></option>
<?php
}
?>
</select>
</div>
Use select2 plugging. It's facilitated to search and add new value which is not in dropdowns.
Click here Select2
<div class="form-group">
<label for="">Catégorie</label>
<select class="form-control select2" name="category" required id="category">
<?php
$select = $pdo->prepare("SELECT * FROM tbl_category");
$select->execute();
while($row = $select->fetch(PDO::FETCH_ASSOC)){
extract($row)
?>
<option><?php echo $row['cat_name']; ?></option>
<?php
}
?>
</select>
</div>
$("#category").select2({
tags: true
});

Combobox based on another combobox

I'm trying to do the following. I am selecting an item from a combobox then I want the values of the next combobox to be the result of a query based on the first selection. This is what I have so far.... HTML
<fieldset>
<div class="form-group">
<select class="custom-select" name="serviciuSelect" id="serviciuSelect" onchange="arataAngajat()">
<option selected="">Selecteaza serviciul dorit</option>
<?php foreach ($servicii as $item): ?>
<option value="<?php echo $item['denumire']; ?>"><?php echo $item['denumire']; ?></option>
<?php endforeach;?>
</select>
</div>
</fieldset>
<fieldset>
<div class="form-group">
<select class="custom-select" name="angajatSelect" id="angajatSelect" style="visibility: hidden">
<option selected="">Selecteaza angajatul dorit</option>
<!-- <?php foreach ($angajati as $item): ?>
<option value="<?php echo $item['nume']; ?>"><?php echo $item['nume']; ?></option>
<?php endforeach;?> -->
</select>
</div>
</fieldset>
JavaScript
<script>
function arataAngajat() {
document.getElementById("angajatSelect").style.visibility = "visible";
var e = document.getElementById("serviciuSelect").value;
document.cookie = "selectSer = " + e;
}
</script>
PHP
<?php
function getSelectServiciu(){
$selectServiciu= $_COOKIE['selectSer'];
$query = "SELECT COD_A FROM angajat WHERE COD_A IN (
SELECT COD_A FROM angajat_serviciu WHERE COD_S='".
$selectServiciu."')";
$result = $this->db->query($query)->result_array();
return $result;
}
?>
I got stuck when I had to call the PHP function getSelectServiciu(). Also, If there's a better way of doing this, please let me know.
Thanks,
Tibi
this script is get data from server under a service display as a combobox options
services.php
<?php
//i'm using array u can use mysql array
$services=array();
$services[]=array('name'=>'Flowers');
$services[]=array('name'=>'Fruits');
//data under services
$data=array();
$data['Flowers']=array('Rose','Belly');
$data['Fruits']=array('Banana','Mango');
//check the the client is requested for data under a service
if(isset($_GET['get_data'])){
$current_service=$_GET['get_data'];
$data_array=$data[$current_service];
echo '<option selected="">Select</option>';
foreach($data_array as $op){
echo '<option value="'.$op.'">'.$op.'</option>';
}
}else{
?>
<fieldset>
<div class="form-group">
<select class="custom-select" name="Services" id="Services" onchange="Getnext_option(this.value)">
<option selected="">Select</option>
<?php foreach ($services as $item): ?>
<option value="<?php echo $item['name']; ?>"><?php echo $item['name']; ?></option> <?php endforeach;?>
</select>
</div>
</fieldset>
<fieldset>
<div class="form-group">
<select class="custom-select" name="Loaded_data" id="Loaded_data" style="display: none"> </select>
</div>
</fieldset>
<script>function Getnext_option(id){
var load_to=document.getElementById('Loaded_data');
load_to.style.display="none";
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
load_to.innerHTML=
this.responseText;
load_to.style.display="block";
}
};
xhttp.open("GET","services.php?get_data="+id, true);//get data from server
xhttp.send();
}</script>
<?php } ?>

How to call HTML required attribute when the value is 0?

I have a drop down list where I'm populating data from the database.
<div class="form-group">
<label>Title:</label>
<select class="form-control" name="Title" id ="titleselect" required>
<!-- <option value="" selected="selected">?</option> -->
<?php foreach ($titles as $row) {
if($this->session->userdata('status')=='active' && $this->session->userdata('Title') == $row->id) { ?>
<option value="<?php echo $row->id; ?>" selected="selected"><?php echo $row->value; ?></option>
<?php }else{?><option value="<?php echo $row->id; ?>"><?php echo $row->value; ?></option><?php } }?>
</select>
</div>
My form,
How values are populated from the database,
My database table,
I want to add a validation when the title form is populated with the value "0", which is "?", it should call the HTML required attribute.
Or I would like to disable the option with the '?' mark.
How can I do achieve this?
I have managed to find a solution to my issue. I managed to do it without any JS or Jquery. Just added an if statement.
<div class="form-group">
<label>Title:</label>
<select class="form-control" name="Title" id ="titleselect" required>
<?php foreach ($titles as $row) {
if($this->session->userdata('status')=='active' && $this->session->userdata('Title') == $row->id) { ?>
<?php if(($this->session->userdata('Title') == 0)) { ?>
<option value="" selected disabled><?php echo $row->value; ?></option>
<?php } else{?>
<option value="<?php echo $row->id; ?>" selected="selected"><?php echo $row->value; ?></option>
<?php } ?>
<?php }else{?><option value="<?php echo $row->id; ?>"><?php echo $row->value; ?></option><?php } }?>
</select>
you can use jquery like this: you can use an id for ? like id="yourZeroOption" and then
$('option#yourZeroOption').each(function() {
if ($(this).isChecked())
$("select").attr("required", true);
else
alert('this filled cannot be ?');
});
since what you asked was'nt clear enough this code might helf and if you manipulate this code you can get what you want from your code. but this for sure will give you a gist of what you should do. by the way this code does not need to submit it will show the alert before that
If this is in Laravel there is a way to catch certain number to be pass. In this case, try with greater_than rule with a custom message.
Example
$this->form_validation->set_rules('title', 'Title', 'greater_than[0]');
$this->form_validation->set_message('greater_than', 'Please select one of these value');
Note: JS validation works, But keep mind it's not always.
You should mark your <select> field as required, then the first option as disabled (and selected):
<select required>
<option value="" selected disabled>?</option>
<option value="1">Mr.</option>
<option value="2">Miss</option>
<option value="3">Mrs.</option>
</select>
This will make the first entry work as a placeholder basically.
we cant make it require with 0 value it can be done only if value=""
here is my idea,
<form action="/action_page.php">
<select required>
<option value="0">?</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<input type="submit">
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("select option:selected").val("");
});
</script>
<style>

Ajax Php dependent dropdown select - How to post name of city instead id into database (submit from data)?

Few days I try to solve problem but no luck.
Problem is next:
Web form with dependent dropdown select box where user can select his
city, zip and street and that work but.
When I submit form in my database no city_name (text) - only city_id (number/value).
So, question is - How to post city_name in database? User is from Rome and I Rome (text) is submited not city_id (number).
Select input:
<div class="form-group">
<label class="col-md-4 control-label" for="grad">City:*</label>
<div class="col-md-4">
<select type="text" name="city_name" id="city_name" class="form-control input-sm">
<option value=""></option>
<option value="999">---Enter city yourself---</option>
<?php $sql_query="SELECT * FROM city_table order by city_id asc";
$result=mysqli_query($dbconfig,$sql_query);
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
?>
<option value="<?php echo $row['city_id'].'_'.$row['city_name'];?>"><?php echo $row['city_name'];?></option>
<?php }?>
</select>
</div>
</div>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="post_no">ZIP code:*</label>
<div class="col-md-4">
<select name="zip_name" id="zip_name" class="form-control input-sm" onchange="recalculate_price();">
</select>
</div>
</div>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="address">Street:*</label>
<div class="col-md-4">
<select name="street_name" id="street_name" class="form-control input-sm" onchange="recalculate_price();">
</select>
</div>
</div>
get_zip_php
<?php
include('db_config.php');
if($_POST['id'])
{
$id=$_POST['id'];
$sql_query="SELECT * FROM zip_table where city_id='$id' order by zip_name asc";
$result=mysqli_query($dbconfig,$sql_query);
?>
<option selected="selected">Select ZIP code :</option><?php
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC))
{
?>
<option value="<?php echo $row['zip_id']; ?>-<?php echo $row['limousine']; ?>-<?php echo $row['kombi']; ?>-<?php echo $row['mini_van']; ?>"><?php echo $row['zip_name']; ?></option>
<?php
}
}
?>
get_street_php
<?php
include('db_config.php');
if($_POST['id'])
{
$id=$_POST['id'];
$sql_query="SELECT * FROM street_table where zip_id='$id' order by street_name asc";
$result=mysqli_query($dbconfig,$sql_query);
?>
<option selected="selected">Select street please :</option><?php
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC))
{
?>
<option value="<?php echo $row['street_id']; ?>"><?php echo $row['street_name']; ?></option>
<?php
}
}
?>
Ajax:
<script>
$(document).ready(function()
{
$("#city_name").change(function()
{
var id=$(this).val();
var data = 'id='+ id;
$.ajax
({
type: "POST",
url: "get_zip.php",
data: data,
cache: false,
success: function(html)
{
$("#zip_name").html(html);
recalculate_price();
}
});
});
$("#zip_name").change(function()
{
var id=$(this).val();
var data = 'id='+ id;
$.ajax
({
type: "POST",
url: "get_street.php",
data: data,
cache: false,
success: function(html)
{
$("#street_name").html(html);
recalculate_price();
}
});
});
});
</script>
If I make some changes on "select city part" (Im beginner and really Im lost in all this) I can`t pass zip.
So, please tell me where to make all changes to pass all that things and post city_name, zip (name...I make it as "xxxx - city") and street name. Please help me, I googled and read here about problems few days and can`t continue work on my project until I fix this. Thanks in advance.
Use bellow select tag. It may useful for you
<select type="text" name="city_name" id="city_name" class="form-control input-sm">
<option value=""></option>
<option value="999">---Enter city yourself---</option>
<?php
$sql_query="SELECT * FROM city_table order by city_id asc";
$result=mysqli_query($dbconfig,$sql_query);
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo "<option value='".$row['city_id']."'>".$row['city_name']."</option>";
}
?>
</select>
Concatenate the id with value like this using delimiter like this
<option value="<?php echo $row['city_id'].'_'.$row['city_name'];?>"><?php echo $row['city_name'];?></option>
In php use explode()
The explode() function breaks a string into an array.
$ss = explode('_','6_Rome');
print_r($ss);
echo $ss[0]; //6 pass the value into query
In jquery use split()
Split a string into an array of substrings:
use split()
$vv = variable_name.split("_");
fetch city name from database on the basis of city id in your POST array before INSERT Query.
SELECT `city_name` FROM `city_table` WHERE `city_id`='".$_POST['city_name']."'
then use that city name in your insert query.

Retrieving data from databse using the dropdown selected list in php and mysql

I have texbox and dropdown list which is populated from mysql database. I want to retrieve data from database and wants to display in textbox using dropdown selected list, without refreshing the page. Here is my code and Thanks in Advance.
<select name="select1" class="form-control" id="dropdownlist1">
<option id="0">-- Select the Company --</option>
<?php
require("dbcon.php");
$getallcompanies = mysql_query("SELECT * FROM ifcandetails6");
while($viewallcompanies = mysql_fetch_array($getallcompanies)){
?>
<option id="<?php echo $viewallcompanies['tcuid']; ?>"><?php echo $viewallcompanies['tcname'] ?></option>
<?php
}
?>
</select>
Input Textbox:
<input type="text" id="field1" value="<?php echo $viewallcompanies['tccontact']?>" disabled/>
Separate things out like this:
<?php
require("dbcon.php");
$query = mysql_query("SELECT tcuid, tcname, tccontact FROM ifcandetails6");
while($row = mysql_fetch_array($query)){
$contacts[] = $row['tccontact'];
$companies[] = $row;
}
?>
<select name="select1" class="form-control" id="dropdownlist1">
<option id="0">-- Select the Company --</option>
<?php foreach($companies as $company) { ?>
<option id="<?= $company['tcuid']; ?>"><?= $company['tcname'] ?></option>
<?php } ?>
</select>
<?php foreach($contacts as $contact) { ?>
<input type="text" id="field1" value="<?= $contact['tccontact']?>"/>
<?php } ?>

Categories

Resources