I have two fail for my script.
1.publicAdvart.php
<script type="text/javascript">
$(document).ready(function () {
$('#select_marka').change(function () {
$.post(
'modeli.php',
{id: $('#select_marka').val()},
function (res) {
$('#model').html(res);
}
);
});
});
</script>
<select class="form-control" name="marka" id="select_marka">
<option></option>
<?php
$sel = "SELECT * FROM marka";
$query = mysqli_query($conn, $sel);
while ($row = mysqli_fetch_array($query)) {
?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['marka']; ?></option>
<?php
}
?>
</select>
2.modeli.php
<?php
if (isset($_POST['id']) && $_POST['id'] != '') {
$marka_id = $_POST['id'];
?>
<select id="select_marka" class="form-control">
<?php
$selModel = "SELECT * FROM marka_model WHERE marka_id='$marka_id'";
$queryModel = mysqli_query($conn, $selModel);
while ($rows = mysqli_fetch_array($queryModel)) {
?>
<option value="<?php echo $rows['model']; ?>"><?php echo $rows['model']; ?></option>
<?php
}
?>
</select>
<?php
}
?>
Where is a problem? These two script half work.
This is OK click "Opel" -> "meriva". http://i.stack.imgur.com/ma5XZ.jpg
Next step 2 is a problem, i want "name" marka and "name" model.
Problem this is here.I don't get "marka - id", i want "name" http://i.stack.imgur.com/Nm1Yk.jpg
Update:
step2.php
<?php
if (isset($_POST['submit'])) {
$marka = $_POST['marka'];
$model = $_POST['model'];
$_SESSION['marka'] = $marka;
$_SESSION['model'] = $model;
}
?>
<div class="col-md-12 well advertTittle">
<h4 class="well">
<?php echo $_SESSION['marka'] . ' ' . $_SESSION['model'] . ' ' . stripslashes($_SESSION['modify']); ?>
</h4>
<span><?php echo $_SESSION['price'] . ' лв.'; ?></span>
</div>
You forget to give the "name" attribute for yours "marka" select field:
<?php
...
?>
<select id="select_marka" class="form-control"> // <- here
<?php
$selModel = "SELECT * FROM marka_model WHERE marka_id='$marka_id'";
....
Should be something like this:
<?php
...
?>
<select id="select_model" class="form-control" name="model"> // <- here!!!
// "id" attribute also should be changed
<?php
$selModel = "SELECT * FROM marka_model WHERE marka_id='$marka_id'";
....
Also, yours step2.php should be like this:
<?php
if (isset($_POST['submit'])) {
$_SESSION['marka'] = $_POST['marka'];
$_SESSION['model'] = $_POST['model'];
}
function validData($data) {
return
isset($data['marka'])
&& isset($data['model'])
&& isset($data['price'])
;
}
if (validData($_SESSION)) {
$marka_id = intval($_SESSION['marka']);
$model_id = intval($_SESSION['model']);
$marka = mysqli_fetch_array(mysqli_query($conn, "SELECT name FROM marka where id = $marka_id"));
$model = mysqli_fetch_array(mysqli_query($conn, "SELECT name FROM marka_model WHERE id = $model_id"));
?>
<div class="col-md-12 well advertTittle">
<h4 class="well">
<?php echo $marka['name'] . ' ' . $model['name'] . ' ' . (isset($_SESSION['modify']) ? stripslashes($_SESSION['modify']) : ''); ?>***
</h4>
<span>
<?php echo $_SESSION['price'] . ' лв.'; ?>
</span>
</div>
<?php } else { ?>
Not all fields filled.
<?php } ?>
Related
Actually, I have a field name (industry) the problem is that I want to get two value from the database change of industry by jquery but how do I do this can't understand. Please help-
Here is my code-
<label>Select Type of Industry</label>
<select class="form-control" name="industry" id="industry" onchange="industry_id(this.value);" required="">
<option value="">------------------Select Your Industry----------------</option>
<?php
foreach ($industry as $key => $value) { ?>
<option value="<?= $value['id']; ?> "> <?= $value['industory']; ?></option>
<?php } ?>
</select>
</div>
<script>
function industry_id(value) {
$.get("<?php echo base_url()?>dive/getebitvalue/"+value,function (data) {
$("#ebit_name").html(data);
// alert(data);
});
}
</script>
one value I get easily but how to get second from the same id?
Here is my model---
function getebit()
{
$industry=$this->uri->segment(3);
$sql = $this->db->query('SELECT * FROM industry WHERE id='.$industry);
$result = $sql->result();
$html='';
foreach ($result as $data) {
$html.= "<option value='" . $data->ebit . "' >" . $data->ebit . "</option>";
}
echo $html;
}
}
and here is my controller--
function getebitvalue(){
$data['ebit']= $this->site_model->getebit();
}
Method getebitvalue in controller should be like this :
function getebitvalue()
{
$industry_id = $this->uri->segment(3);
$result = $this->site_model->getebit($industry_id);
$html='';
if ( !empty($result)){
foreach ($result as $item)
{
$edita[$key] = $item->edita;
$html.= "<option value='" . $item->ebit . "' >" . $item->ebit . "</option>";
}
$data['edita'] = $edita;
$data['html'] = $html;
print_r($data);
exit;
}
}
Here is your model---
function getebit($industry_id)
{
$sql = $this->db->query('SELECT * FROM industry WHERE id='.$industry);
$result = $sql->result();
return $result;
}
You can also do this-
view page-
<div class="form-group">
<label>Select Type of Industry</label>
<select class="form-control industryData" name="industry" id="industry" required="">
<option value="">------------------Select Your Industry----------------</option>
<?php
foreach ($industry as $value) { ?>
<option data1="<?php echo $value['ebitda']; ?>" data2="<?php echo $value['ebit']; ?>" value="<?= $value['id']; ?>"> <?= $value['industory']; ?></option>
<?php } ?>
</select>
</div>
model page-
function select($tbl,$con=''){
$this->db->select("*");
$this->db->from($tbl);
if(!empty($con))
$this->db->where($con);
$query = $this->db->get();
return $query->result();
}
controller page-
public function dive(){
$this->load->view('common/header');
$data['industry'] = $this->site_model->select('industry');
$this->load->view('dive/dive',$data);
$this->load->view('common/footer');
}
Now use jquery on your view page--
<script>
$(document).on("change", ".industryData", function () {
var value = $(this).val();
var data1 = $('.industryData option:selected ').attr('data1');
var data2 = $('.industryData option:selected ').attr('data2');
alert(data1);
});
</script>
I have a table with id, header, etc. Would like to make a give a name from header column to dropdown list to each value. Now its only shows value, which is very uncomfortable :
<form method="POST" enctype="multipart/form-data" action ="uploadext.php">
<?php require_once('uploadext.php'); ?>
<div class="col-md-6"><input type="file" name="fileToUploadgp"></div>
<div class="col-md-6"><input type="file" name="fileToUploadpro"><br>
<?php
$dbc = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
$query = "SELECT id, header FROM done_add";
$query1="SELECT header FROM done_add";
$data = mysqli_query($dbc, $query);
$data1=mysqli_query($dbc, $query1);
$array=[];
while ($row = mysqli_fetch_array($data)) {
$arrayid[] = $row['id'];
$arrayhead[]=$row['header'];
}
?>
<select name="selectlink">
<?php foreach ($arrayid as $arr) {?>
<option value = "<?php print($arr)?>"
} ?><?php print($arr) ?></option>
<?php } ?>
</select>
<input type="submit" value="Отправить файлы" name="submita">
</div>
</form>
(sorry for bad english)
change the way you store your data in $array:
while ($row = mysqli_fetch_array($data)) {
$option = [];
$option['id'] = $row['id'];
$option['header'] = $row['header']
$array[] = $option;
}
every $option in $array will have and id and a header, then you can simply:
<select name="selectlink">
<?php foreach ($array as $option) {?>
<option value = "<?php echo $option['id'];?>"
} ?><?php echo $option['header'];?></option>
<?php } ?>
</select>
I'm trying to show an error message if a checkbox has not been selected. I've managed to get it done input field, but unsure of how to get it done with a checkbox. This is what I have so far:
<?php
# check if data has been posted
if (!empty($_POST)) {
# keep track validation errors
$author_error = null;
$categories_error = null;
# keep track of post values
$author = $_POST['author'];
$categories = $_POST['categories'];
# validate input
if (empty($author)) {
$author_error = 'Please select author';
$valid = false;
}
if (empty($categories)) {
$categories = 'Please select categories';
$valid = false;
}
# if data is valid, insert into the database
if ($valid) {
}
}
?>
<div class="control-group <?php if (!empty($author_error)){ echo 'error'; } ?>">
<label class="control-label">Author</label>
<div class="controls">
<select name="author" id="author">
<option value="">Select one</option>
<?php $sql2 = 'SELECT id, name FROM author';
foreach ($dbConnection->query($sql2) as $data2) { ?>
<option value="<?php echo $data2['id']; ?>"
<?php if(isset($author) && $author == $data2['id']) { echo 'selected'; } ?>>
<?php echo $data2['name']; ?>
</option>
<?php } ?>
</select>
<?php if (!empty($author_error)) { echo '<span class="help-inline">' . $author_error . '</span>'; } ?>
</div>
</div>
<div class="control-group <?php if (!empty($categories_error)){ echo 'error'; } ?>">
<fieldset>
<legend class="control-label">Categories:</legend>
<?php $sql3 = 'SELECT id, name FROM category';
foreach ($dbConnection->query($sql3) as $data3) { ?>
<div class="controls">
<label for="category<?php echo($data3['id']);?>">
<input type="checkbox" name="categories" id="categories" value="<?php echo($data3['id']); ?>"
<?php if(isset($_POST['categories']) && in_array($data3['id'], $_POST['categories'])) { echo 'checked'; } ?>>
<?php echo($data3['name']); ?>
</label>
</div>
<?php } ?>
</fieldset>
<?php if (!empty($categories_error)) { echo '<span class="help-inline controls">' . $categories_error . '</span>'; } ?>
</div>
Where am I going wrong with the categories field?
Check it like:
isset($_POST['categories'])
On line #62 you are checking if $categories_error is empty but you are using variable $categories for storing error message on line #19
i have a query in php :
<div class="col-md-7" id="col_bahan">
<select name="bahan[]" id="bahan" class="form-control bahan_asli">
<option value="0">--pilih--</option>
<?php
$qry = $db->query("SELECT * FROM bahan");
while ($bahan = $qry->fetch_array()) {
$qry2 = $db->query("SELECT * FROM warna WHERE id_warna='$bahan[id_warna]'");
$warna = $qry2->fetch_array();
?>
<option value="<?php echo $bahan['id_bahan']; ?>"><?php echo $bahan['id_bahan']; ?> - <?php echo $bahan['nm_bahan'] . ' Warna ' . $warna['nm_warna']; ?></option>
<?php
}
?>
</select>
</div>
I want to copy/clone this code in jquery, then I render element in HTML. I don't like using .clone() for it,
Try this,
$("#div").html($("#col_bahan").html());
i wanna get this, value of 3 selectbox are same:
http://i.stack.imgur.com/EqvpT.jpg
then, i write this code:
$jabatan = mysql_query("SELECT * FROM jabatan WHERE id_jabatan NOT IN (1,10,11,12)") or die(mysql_error());
if (isset($_GET['id'])) {
$query = mysql_query("SELECT *, month(tanggal_larut) as bulan_larut, day(tanggal_larut) as hari_larut, year(tanggal_larut) as tahun_larut FROM larut WHERE id_larut ='$_GET[id]'") or die(mysql_error());
$row = mysql_fetch_object($query);
}
<li>
<label>Pemateri 1</label>
<select name="pemateri1">
<?php
while ($p1 = mysql_fetch_object($jabatan)) {
?>
<option value="<?php echo isset($p1->id_jabatan) ? $p1->id_jabatan : '' ?>" <?php if (isset($_GET['id'])) { if($p1->id_jabatan == $row->pemateri1) {echo "selected";} else {echo "";} } ?> ><?php echo isset($p1->nama_jabatan) ? $p1->nama_jabatan : '' ?></option>
<?php
}
?>
</select>
</li>
<li>
<label>Pemateri 2</label>
<select name="pemateri2">
<?php
while ($p2 = mysql_fetch_object($jabatan)) {
?>
<option value="<?php echo isset($p2->id_jabatan) ? $p2->id_jabatan : '' ?>" <?php if (isset($_GET['id'])) { if($p2->id_jabatan == $row->pemateri2) {echo "selected";} else {echo "";} } ?> ><?php echo isset($p2->nama_jabatan) ? $p2->nama_jabatan : '' ?></option>
<?php
}
?>
</select>
</li>
<li>
<label>Pemateri 3</label>
<select name="pemateri3">
<?php
while ($p3 = mysql_fetch_object($jabatan)) {
?>
<option value="<?php echo isset($p3->id_jabatan) ? $p3->id_jabatan : '' ?>" <?php if (isset($_GET['id'])) { if($p3->id_jabatan == $row->pemateri3) {echo "selected";} else {echo "";} } ?> ><?php echo isset($p3->nama_jabatan) ? $p3->nama_jabatan : '' ?></option>
<?php
}
?>
</select>
</li>
i get this:
http://i.stack.imgur.com/vYnpd.jpg
Ok, please somebody help me, how to correct the code? thanks
Can you try this.... if you want the same select box option why using more time while loop, you can do in one loop. For sql injection read this REF
<?php
$jabatan = mysql_query("SELECT * FROM jabatan WHERE id_jabatan NOT
IN (1,10,11,12)") or die(mysql_error());
if (isset($_GET['id'])) {
$query = mysql_query("SELECT *, month(tanggal_larut) as bulan_larut,
day(tanggal_larut) as hari_larut, year(tanggal_larut) as tahun_larut
FROM larut WHERE id_larut ='".mysql_real_escape_string($_GET[id])."'") or
die(mysql_error());
$row = mysql_fetch_object($query);
}
$Selectbox_options = '';
while ($p1 = mysql_fetch_object($jabatan)) {
$value = isset($p1->id_jabatan) ? $p1->id_jabatan : '';
$selection_value = '';
if (isset($_GET['id'])) {
if ($p1->id_jabatan == $row->pemateri1) {
$selection_value = "selected";
} else {
$selection_value = "";
}
}
$option_title = isset($p1->nama_jabatan) ? $p1->nama_jabatan : '';
$Selectbox_options .= '<option value="' . $value . '" ' . $selection_value . ' >' . $option_title . '</option>';
}
?>
<li>
<label>Pemateri 1</label>
<select name="pemateri1">
<?php echo $Selectbox_options;?>
</select>
</li>
<li>
<label>Pemateri 2</label>
<select name="pemateri2">
<?php echo $Selectbox_options;?>
</select>
</li>
<li>
<label>Pemateri 3</label>
<select name="pemateri3">
<?php echo $Selectbox_options;?>
</select>
</li>