Get id of Buttons using php once clicked - php

I have 4 buttons,and also an exportexcel button.I want to set the excel filename.If i clicked getToday button, i want to set the today date to the excel.
<div class="span3 btns_state">
<div data-toggle="buttons-radio" id="toggleButton" class="btn-group clearfix sepH_a">
<button type="button" name="getTday" id="getToday" class="btn btn-info rep_data tip-bottom active" data-original-title="<?php echo $today;?>"onclick = "show()">Today</button>
<button type="button" name="getWeek" id="getWeek" class="btn btn-info rep_data tip-bottom" data-original-title="<?php echo $this_week;?>"onclick = "show()">This Week</button>
<button type="button" name="getMonth" id="getMonth" class="btn btn-info rep_data tip-bottom" data-original-title="<?php echo $this_month;?>"onclick = "show()">This Month</button>
<button type="button" name="getPreMonth" id="getpremon"class="btn btn-info rep_data tip-bottom" data-original-title="<?php echo $previous_month;?>"onclick = "show()">Last Month</button>
</div>
</div>
My php code to get the dates.
$str = $_GET['getTday'];
if($str == 'getToday')
{
$var=$today;
}
else
{
$var=$this_week;
}
I want to retrieve the dates corresponding to the button i clicked,but only else part is working.
Here is the excel export.
saveAs(new Blob([s2ab(wbout)],{type:"application/octet-stream"}), Report-<?php echo $var?>.xlsx");

You can use query string to pass the value of clicked button
refer the "Today" button below.
You can set the value of variable "$var" to your execel or anything.
[Note:I done in the same page,create "test.php" and paste the following code and try,it will works well]
Thank You.
<?php
//Assume Variables $today and $this_week like below
$today = date('Y-m-d');
$this_week = "This week";
$var = "";
if(isset($_GET['getTday'])):
$str = $_GET['getTday'];
if($str == 'getToday')
{
$var=$today;
}
else
{
$var=$this_week;
}
endif;
echo $var;
?>
<div class="span3 btns_state">
<div data-toggle="buttons-radio" id="toggleButton" class="btn-group clearfix sepH_a" >
<button type="button" name="getTday" id="getToday" class="btn btn-info rep_data tip-bottom active" data-original-title="<?php echo $today;?>"onclick = "show('getToday')">Today</button>
<button type="button" name="getWeek" id="getWeek" class="btn btn-info rep_data tip-bottom" data-original-title="<?php echo $this_week;?>"onclick = "show()">This Week</button>
<button type="button" name="getMonth" id="getMonth" class="btn btn-info rep_data tip-bottom" data-original-title="<?php echo $this_month;?>"onclick = "show()">This Month</button>
<button type="button" name="getPreMonth" id="getpremon"class="btn btn-info rep_data tip-bottom" data-original-title="<?php echo $previous_month;?>"onclick = "show()">Last Month</button>
</div>
</div>
<script>
function show(val)
{
if(val == 'getToday')
{
location.href = "test.php?getTday=getToday";
}
}
</script>

Make button type submit with form tag
<form>
<button type="submit" name="getTday">
and so on...
</form>
Then you will get in php like $_GET['getTday'] this will get name attr of field so keep id and name same
else you need to use jquery/js to get id of button

add value attribute in your button like this value="getToday".
You can get this in php file.
Example
<button type="button" name="getTday" id="getToday" value="getToday" class="btn btn-info rep_data tip-bottom active" data-original-title="<?php echo $today;?>"onclick = "show()">Today</button>

Using jquery click event to perform your action. You have to remove all click event inside html
$(".btn.btn-info.rep_data.tip-bottom").click(function(){
var name=$(this).attr("name");
// you can get what ever it is
// this.id;
// this.value
});
DEMO
NOTE: Dont write event in html it is not good practis

Related

The disabled attribute of a dynamically created button in php not changing using it's name or id in jquery but it's css property is changing

None of the commented statement are working but the uncommented one is working perfectly fine
echo "<div class='col-xs-12 col-sm-3 emphasis'>
<button class='btn btn-primary btn-block disabled' name = 'proceed' id= 'proceed'><span class='fa fa-user'></span> Proceed </button>
</div> " //This is dynamically generated button using php
$('[name="proceed"').css('background-color','black');
// $('[name="proceed"]').removeAttr('disabled');
// $('[name="proceed"]').attr('disabled',false);
// $('[name="proceed"]').prop("disabled",false);
//$('#proceed').prop("disabled",false);
//$('#proceed').attr('disabled',false);
// $('#proceed').removeAttr('disabled');
You're trying to set attribute disabled while it's class name
You need either set disabled as attribute or use removeClass function
$('[name="proceed"').css('background-color','black');
$('[name="proceed"]').removeClass('disabled');
// $('[name="proceed"]').attr('disabled',false);
// $('[name="proceed"]').prop("disabled",false);
//$('#proceed').prop("disabled",false);
//$('#proceed').attr('disabled',false);
// $('#proceed').removeAttr('disabled');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='col-xs-12 col-sm-3 emphasis'>
<button class='btn btn-primary btn-block disabled' name = 'proceed' id= 'proceed'><span class='fa fa-user'></span> Proceed </button>
</div>
Try the following:
$('button[name="proceed"]').css('background-color','black');
EDIT:
Disabling input:
$("#proceed").prop('disabled', true);
Enabling Input:
$("#proceed").prop('disabled', false);

Form inputs with Datatables

I have a Datatable with form inputs.
This is how I create it
public function getpurchasedata(Request $request)
{
if($request->get('orderID')){
$id = $request->orderID;
$orderNo = PurchaseOrder::find($id);
$order_items = PurchaseOrderItem::where('order_id',$orderNo->order_id)
->with('material')->get();
return DataTables::of($order_items)
->addColumn('name',function($order_items){
$name = $order_items->material;
return $name['name'];
})->addColumn ('expdate',function($order_items){
return '<input type ="date" id="expdate"
name="expdate" class="form-control">';
})->addColumn('action', function ($order_items) {
$buttons ='<button id="edit"
class="btn btn-info btn-sm">
<i class="far fa-edit"></i>
</button>
<button id="remove"
class="btn btn-danger btn-sm">
<i class="far fa-trash-alt"></i>
</button>';
return $buttons;
})->rawColumns(['expdate','action'])->make(true);
}
}
I use the following method to pass Data table values to the controller
grn_tableData:grn_table.data().toArray(),
but when i submit data using an ajax function i get html markups instead on input values for the expdate column. how can i get the real value ? explain me
You can add a hidden column next to your expdate column. Doing this, you can get the real value after form submission.

jquery-the output is always in the first item only

I am trying to create a web store and I use Jquery in radio button for selecting a category but the output is always on the first item only not in each items.
here is the jquery code
function populateswatches() {
var swatchesName = $('input[name=optradio]:checked').val();
$.getJSON('getdata.php', {swatchesName: swatchesName}, function(swatch) {
var html = '';
$.each(swatch, function(index, array) {
html = html + '<div class="col-md-3 w3-margin-top"><div class="w3-display-container w3-hover-opacity"><div class="w3-center"><input type="radio" name="swatches_code" value="' + array['swatches_code'] + '" required></div><div class="w3-display-middle w3-display-hover"><button type="button" class="btn btn-primary"><i class="fa fa-search fa-lg" aria-hidden="true"></i></button></div><img src="../backend/functions/images/'+array['images']+'" style="width:100%"><div class="w3-center">' + array['swatches_code']+'</div></div></div>';
});
$('#swatches').html(html);
});
}
$(function() {
$(this).change(function() {
populateswatches();
});
});
here is the code for php. it is not complete though.
$priceSQL="SELECT * FROM price WHERE id_item='$IDitem'";
$priceRES=mysqli_query($conn,$priceSQL);
while($priceROW=mysqli_fetch_assoc($priceRES)){
$id_categ=$priceROW["id_category"];
$catSQL="SELECT * FROM category WHERE id_category='$id_categ'";
$catRES=mysqli_query($conn,$catSQL);
while($catROW=mysqli_fetch_assoc($catRES)){
echo '
<div class="radio">
<label><input type="radio" name="optradio" value="'.$priceROW["price"].'-'.$id_categ.'" required>'.$catROW["swatches_category"].'- ₱'.formatMoney($priceROW["price"]).'</label>
</div>
';
}
}
</li>
</ul>
<h4>SWATCHES</h4>
<div class="form-group">
<span id="swatches"></span>
</div>
<input type="hidden" value="'.$IDitem.'" name="id_item">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-success">Add to Cart</button>
here is the output
here is the image output for it
Reason:-
id is treated as unique identifier in jQuery.Since all spansin your code have same id, that's why code only working for first-one.
Solution:-
Convert id to class in your HTML code like below:-
<span class="swatches"></span>
And change # to . in jQuery like below:-
$('.swatches').html(html);
Note:- class is treated as a group identifier in jQuery and work on each element which share same class.

Codeigniter, Submit "button" missing instructions from controller or model?

the submit "button" isn´t working properly, I know there must be something missing, the problem is Idk what is it...
The main idea is to insert information, then do a refresh displaying it in a table below, all in the same "page", the "submit" doesn´t save :(.
Thanks in advance.
Part of the code in Model:
function create_gei()
{
$data['theyear'] = $this->input->post('theyear');
$data['qty_alumni'] = $this->input->post('qty_alumni');
$data['emanations'] = $this->input->post('emanations');
$data['temperature'] = $this->input->post('temperature');
$this->db->insert("pdc_factor_gei", $data);
}
Part of the code in Controller:
function btn_create_gei()
{
$this->model_gas_consum->create_gei();
$submit = $this->input->post('send');
if($submit=='repeatgei')
{
redirect(current_url("gas_consum/factor_gei/"), 'refresh');
}
else
{
redirect("gas_consum/home_gas/");
}
}
Part of the code in View "button":
<?php echo form_open("gas_consum/factor_gei"); ?>
<div class="row">
<div class="col-md-6 text-right">
<button
name="send" value="repeatgei" class="btn" type="submit">
<span class="glyphicon glyphicon-refresh"></span> Save</button>
</div>
<?php echo form_close(); ?>
Try this
<input type="submit" name="send" value="repeatgei" class="btn">
Try this, change your
<button name="send" value="repeatgei" class="btn" type="submit">
<span class="glyphicon glyphicon-refresh"></span> Save</button>
To this
<?php echo form_submit('send', 'repeatgei', 'class="btn"'); ?>

Cannot pass hidden form value to php

When i pass the hidden value to the php file it gives (true) 1 as a answer.
I am passing the value from the modal to the php file.
The span value was retrieved using jquery.
PHP CODE:
<?php
include "dbcon.php";
if(isset($_POST['pd_del']))
{
echo mysql_error();
$delid=isset($_POST['delidd']);
echo $delid;
}else
{
echo mysql_error();
}
?>
HTML CODE:
Form thats send the product id to the php file
<form name="prd_del" action="del_prod.php" method="post">
<div class="modal fade" id="delModal" tabindex="-1" role="dialog" aria-labelledby="delModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">DELETE PRODUCT</h4>
</div>
<div class="modal-body">
<h5>Do you want to Delete this Product ??? <span id="delid" name="delid"></span></h5>
<input type="hidden" name="delidd" id="delid">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" name="pd_del" >Delete It!</button>
</div>
</div>
</div>
</div>
</form>
Your HTML :
<h5>Do you want to Delete this Product ??? <span id="delid" name="delid"></span></h5>
<input type="hidden" name="delidd" id="delid">
Your JS :
$(".modal-body #delid").text( pd_del_id);
First problem is that you have 2 elements with the same id value (the sppan and the input field). You shouldn't.
Change one of your ids, something like that :
<h5>Do you want to Delete this Product ??? <span id="delid" name="delid"></span></h5>
<input type="hidden" name="delidd" id="delid_value">
And in your JS, if I understood what you want to do :
$(".modal-body #delid").text(pd_del_id); // Here you put the product's ID in your span to show it to the user.
$(".modal-body #delid_value").val(pd_del_id); // Here you put the product's ID in your hidden field to send it with your form.
Now, your PHP :
$delid=isset($_POST['delidd']);
echo $delid;
isset() function returns either true or false if the variable is set or not.
The variable $_POST['delidd'] is set (the hidden field is always sent to your PHP).
If you want to get the value (your product's ID) :
if (!empty($_POST['delidd'])) {
// The value is not empty
$delid = $_POST['delidd'];
} else {
// The value is empty : there is a problem (For example echo an error message or do whatever you have to do in that case.)
}

Categories