Values null when passed from another page using modal and jquery - php

I am having problem on passing values into another page using jQuery and modal. So here is my code.
Firing up the modal:
echo "<input type='hidden' data_project='$row[PROJECT_NAME]' data_headmark='$row[HEAD_MARK]' data_id='$row[ID]'></input>";
echo '<button type="button" name="qcreject" id="qcreject" class="btn btn-primary btn-danger btn-lg btn-block" data-toggle="modal" data-target="#qcRejectModal">
<span style="font-size:35px"> '.$row['PROJECT_NAME'].' / <b>'.$_POST["hm"].'</b>/'.$row['ID'].' ~ FAIL</span></button>';
<div class="modal-body">
<div class="row">
<div class="col-md-12">
<div class="form-group">
This is the confirmation window to PASS all the Quality Control for, <br/><br/>
PROJECT : <b>'.$row['PROJECT_NAME'].'</b>
HEADMARK : <b>'.$row['HEAD_MARK'].'</b>
ID : <b>'.$row['ID'].'
</div>
</div>
</div><br/>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<textarea name="reasonForRejection" id="reasonForRejection" class="form-control" placeholder="Reason for rejection" rows="2" required></textarea>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" name="rejectbutton" id="rejectbutton" class="btn btn-danger">Confirm REJECT!</button>
</div>
and when user click the reject button it should pass PROJECT_NAME, HEAD_MARK, ID and values in the textarea to the testClass.php.
jQuery is,
$('#rejectbutton').click(function() {
$.post("testClass.php",{
projectName : this.getAttribute("data_project"),
headMark : this.getAttribute("data_headmark"),
id : this.getAttribute("data_id"),
reasonReject : this.getAttribute("reasonForRejection")});
});
and inside the testClass.php
echo 'TEST CLASS WORKS';
$val1 = $_POST["projectName"];
$val2 = $_POST["headMark"];
$val3 = $_POST["id"];
$val4 = $_POST["reasonReject"];
var_dump($val1);
var_dump($val2);
var_dump($val3);
var_dump($val4);
unfortunately all the values I'm getting in the testClass are null
TEST CLASS WORKSstring(0) ""
string(0) ""
string(0) ""
string(0) ""

Add id attribute in your hidden field then use that in js like below code.
echo "<input type='hidden' data_project='$row[PROJECT_NAME]' data_headmark='$row[HEAD_MARK]' data_id='$row[ID]' id="data_field" />";
// your codes
$('#rejectbutton').click(function() {
var myData = $('#data_field');
$.post("testClass.php",{
projectName : myData.attr("data_project"),
headMark : myData.attr("data_headmark"),
id : myData.attr("data_id"),
reasonReject : $("#reasonForRejection").val()
});
});

Looking at this code:
$('#rejectbutton').click(function() {
$.post("testClass.php",{
projectName : this.getAttribute("data_project"),
headMark : this.getAttribute("data_headmark"),
id : this.getAttribute("data_id"),
reasonReject : this.getAttribute("reasonForRejection")});
});
it assumes, that when the element with id #rejectbutton is clicked, which is:
<button type="button" name="rejectbutton" id="rejectbutton" class="btn btn-danger">Confirm REJECT!</button>
It has the attributes you want on it, but it doesn't. Also, you are trying to get the attributes using getAttribute() - why don't you use the jquery method?
this is how you do it. Update your HTML to be like this:
<button
type="button"
name="rejectbutton"
id="rejectbutton"
class="btn btn-danger"
data_project="some project name",
headMark="some head mark",
data_id="1234",
reasonForRejection="some reason here"
>Confirm REJECT!</button>
Then update your JS to get those attributes in your post request like this:
$('#rejectbutton').click(function() {
$.post("testClass.php", {
projectName : $(this).attr('data_project'),
headMark : $(this).attr('headMark'),
id : $(this).attr('data_id'),
reasonReject : $(this).attr('reasonForRejection'),
});
});

Related

Bootstrap Modal won't POST all data with datepicker field using PHP

I have two elements of members data such as id and some other info in a modal which are hidden input elements inside my modal, I bind data from a anchor tag using javascript, example of data element in the anchor tag:
data-column="'.htmlspecialchars($column, ENT_QUOTES, 'utf-8').'"
Javascript example to bind into the modal:
$('#EnterExpiryModal').on('show.bs.modal', function (e) {
var memberID = $(e.relatedTarget).data('id');
$('#memID232').val(memberID);
var cola3 = $(e.relatedTarget).data('column');
$('#column3').val(cola3);
});
Having the relevant data for the members in question in my modal (modal code snippet below):
<div class="modal" id="EnterExpiryModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="false">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<div id="ExpiryError"></div>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Please enter document expiry date</h4>
<br>
<form class="form-horizontal" method="post" id="ExpiryDateForm">
<div class="form-group">
<input id ="memID232" name="memID232" class="form-control" type="hidden">
</div>
<div class="form-group">
<input id ="column3" name="column3" class="form-control" type="hidden">
</div>
<div class="form-group">
<label class="col-md-4 control-label">Date</label>
<div class="col-md-6 inputGroupContainer">
<div class="input-group">
<div class="clearfix">
<div id="date" data-placement="left" data-align="top" data-autoclose="true">
<input name="date" type="text" class="form-control hasDatepicker" placeholder="Choose Date">
</div>
</div>
</div>
</div>
</div>
<div class="middleme"><p><i class="fa fa-info-circle iwarner" aria-hidden="true"></i><small> These documents can be uploaded again at any time...</small></p></div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" id="expirySubmit" name="expirySubmit" class="btn clocumsbtn">Confirm</button>
</div>
</form>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div>
The above code produces the below modal:
As you can see user is prompted to enter a expiry date in the form field and click confirm.
However the issue I am having is when I hit submit the form does submit, my jQuery validation does all the necessary checks before submitting using ajax, but my hidden input elements don't submit with the values as they should, they appear to be empty, but the datepicker (date) field is populated - I know the values populate with the required data I need.
Here's what the modal looks like without the elements being hidden:
Here is the validation:
$("#ExpiryDateForm").validate({
rules:
{
memID232: {
required: true,
minlength: 0
},
column3: {
required: true,
minlength: 0
},
date: {
required: true,
minlength: 3
}
},
messages:
{
memID232: "There's an error",
column3: "There's an error",
date: "Please enter the expiry date",
},
submitHandler: submitExpiryDate
});
here's the submit handler:
function submitExpiryDate() {
var data = $("#ExpiryDateForm").serialize();
$.ajax({
type : 'POST',
url : 'enterdocexpiry.php',
data : data,
beforeSend: function() {
$("#expirySubmit").html('<span class="fa fa-spinner"></span> please wait...');
},
success : function(responses) {
if(responses=="1"){
$('#ExpiryError').removeClass('animated shake');
$("#ExpiryError").fadeIn(1000, function(){
$('#ExpiryError').addClass('animated shake');
$("#ExpiryError").html('<div class="alert alert-danger"> <span class="fa fa-exclamation"></span> Sorry there was an error!</div>');
});
}
else if(responses=="updated"){
$("#ExpiryError").fadeIn(2000, function(){
$("#expirySubmit").html('<span class="fa fa-spinner"></span> updating...');
$("#ExpiryError").html("<div class='alert alert-success'><span class='fa fa-check-square-o'></span> Added Expiry Date!</div>");
setTimeout(function(){
window.location.href="manage_docs.php";
},2000);
});
}
else {
$("#ExpiryError").fadeIn(1000, function(){
$("#ExpiryError").html('<div class="alert alert-danger"><span class="glyphicon glyphicon-info-sign"></span> '+data+' !</div>');
$("#expirySubmit").html('<span class="glyphicon glyphicon-log-in"></span> Some other error');
});
}
}
});
return false;
}
here the php:
require "database.php";
$memberID = $_POST['memID232'];
$column = $_POST['column3'];
$date = DateTime::createFromFormat('d/m/Y',$_POST['dates']);
$expiryDate = $date->format("Y-m-d");
$docploaded = "Yes";
if (isset($_POST['expirySubmit'])) {
if ($column == "passport") {
$statement = $conn->prepare('UPDATE memberdocs SET pexpiry=:expiryDate,puploaded=:docploaded WHERE m_id=:memberID');
$statement->bindParam(':expiryDate', $expiryDate);
$statement->bindParam(':docploaded', $docploaded);
$statement->bindParam(':memberID', $memberID);
$statement->execute();
if($statement->execute() ):
echo 'updated';
else:
echo "1";
endif;
}else if ($column == "crb") {
$statement = $conn->prepare('UPDATE memberdocs SET cvexpiry=:expiryDate WHERE m_id=:memberID');
$statement->bindParam(':expiryDate', $expiryDate);
$statement->bindParam(':memberID', $memberID);
$statement->execute();
if($statement->execute() ):
echo 'updated';
else:
echo "1";
endif;
}
}
Now I have done some troubleshooting and it seems the datepicker is the issue here. If I remove the datepicker (date) form field and replace it with a standard free text input field my form submits successfully with the memID232 input field and column3 input field populated, executing my php script, I've tried to be as clear as possible I hope the screenshots and snippets help, any advice?

Passing a PHP variable to a modal and using it in MySQL query

I have a table with multiple edit buttons. Each edit button is supposed to open up a modal and I am trying to pass the delivery_id to it, so I can then use it in MySQL query
echo "<td><button type='button' class='btn dt_buttons' data-toggle='modal' data-id='$delivery_id' data-target='#editModal'>Edit</button></td>";
What's the best way of retrieving that value in the modal and using it as a variable? I thought that just using $delivery_id would work, but of course that would be too simple!
Code inside the modal:
<div id="editModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Edit Purchase</h4>
</div>
<div class="modal-body">
<?
$query = "SELECT id, supplier_id, date as del_date, delivery_number, po_number, cost_value FROM store_purchases WHERE id = $delivery_id";
echo $query;
$retval = f_select_query($query, $datarows);
$lint_product_id = f_htmlspecialchars_decode($datarows[0]->id , ENT_QUOTES);
$supplier_id = intval($datarows[0]->supplier_id);
$delivery_date = $datarows[0]->del_date;
$delivery_number = intval($datarows[0]->delivery_number);
$lint_unit_cost = f_htmlspecialchars_decode($datarows[0]->cost_value , ENT_QUOTES);
$lint_unit_cost = floatval($lint_unit_cost);
$lint_unit_cost = number_format($lint_unit_cost, 2);
$department_id_dropdown = f_get_dropdown("supplier_name", "supplier_name", "supplier_master", $supplier_id, "id", " store_id = $store_id", '', '', '', false, false, true);
?>
<div class="container-fluid" id="div_user_master" class="ae_form" >
<form id="myForm" action="/platformDev/create_subscription.php" method="POST">
<?
echo "Supplier Name: <td class='text-right' id='department_id' style='width:20%;'> $department_id_dropdown </td> <input id='purch_id' name='purch_id' class='form-control purch_id' value='$product_id' type='hidden'/>";
echo "Delivery Date: <span class='required_field'><i class='fa fa-star fa-sm'></i> </span> <input class='form-control' tabindex='3' id='date' name='date' value= '$delivery_date' type='text'/> <br/>";
echo "Delivery Number: <input type='text' id='unit_cost' name='unit_cost' class='form-control unit_cost' style='width:80%;' value='$delivery_number' />";
echo "Invoice Cost: <input type='text' id='unit_cost' name='unit_cost' class='form-control unit_cost' style='width:80%;' value='$lint_unit_cost' /></div>";
?>
</form>
</div>
</div>
<div class="modal-footer">
<button class="btn form-btns btn-primary" style="float: left;" data-dismiss="modal" id="customButton">Add Purchase</button>
<button type="button" class="btn dt_buttons close_this ajax_forms" data-dismiss="modal">Close</button>
</div>
</div>
</div>
The code below will get the data-id value from the button that was clicked.
You can use this template:
$('#editModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);
var delivery_id = button.data('id'); // delivery id here
var modal = $(this)
modal.find('.modal-title').text('Delivery #' + delivery_id);
modal.find('.modal-body').html('content here');
});
https://getbootstrap.com/docs/4.0/components/modal/
https://getbootstrap.com/docs/3.3/javascript/#modals
I guess that you are trying to use that id to fetch records and populate the modal with that data.
If that's the case :
You can retrieve the data-id attribute value with Javascript and send Ajax request to your php script and query your database.
$('.dt_buttons').on('click', function()
{
var id = $(this).attr('data-id');
$.ajax
({
url: "your/url",
data:
{
id : id
},
method: 'POST'
}).success(function(response)
{
var json = response,
obj = JSON && JSON.parse(json) || $.parseJSON(json);
// say you have following fields.
var fid = obj[0].id;
var title = obj[0].title;
//retrieve record fields here.
// or just pass the `id` skipping the Ajax stuff above.
$('#editModal')
.find('span.doc-title').text(title).end()
.find('[name="id"]').val(id).end();
/* show modal.. */
$('#editModal').modal('show');
});
});

show hide button jquery with dynamic values

i have this code in index.php:
<tbody>
<?php foreach($data as $fetch): ?>
<tr>
<input type="hidden" name="user_id" value="<?php echo $_SESSION['user_id']?>">
<td class="segment" contenteditable="true"><?= $fetch['segment'] ?></td>
<td class="text-center">
<button class = "btn btn-default action-btn" data-id="<?= $fetch['id'] ?>" data-action="update">
<span class = "glyphicon glyphicon-edit"></span> Update
</button>
|
<button class = "btn btn-success activate-btn action-btn" data-id="<?= $fetch['id'] ?>" type="submit" data-action="activate">
<span class = "glyphicon glyphicon-check"></span> Activate
</button>
<button style="display:none" class = "btn btn-danger deactivate-btn action-btn " data-id="<?= $fetch['id'] ?>" data-action="deactivate">
<span class = "glyphicon glyphicon-folder-close"></span> deactivate
</button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<form action="create.php" method="post" class="form-inline">
<div class = "form-group">
<label>Segment:</label>
<input type = "text" name = "segment" class = "form-control" required = "required"/>
</div>
<div class = "form-group">
<button type="submit" name = "save" class = "btn btn-primary"><span class = "glyphicon glyphicon-plus"></span> Add</button>
</div>
</form>
<script type="text/javascript">
$(".action-btn").on("click", function(e) {
var id = $(this).attr("data-id");
var segment = $(this).parents("tr").find("td.segment").html();
var action = $(this).attr("data-action");
$.ajax({
"url": "action.php",
"method": "post",
"data": {
"id": id,
"segment": segment,
"action": action
},
success: function(data) {
alert(data);
}
});
}); </script>
<script>$(".activate-btn").click(function(){$(this).hide();$(".deactivate-btn").show();});</script>
</body>
the problem is when i click on any activate button all deactivate button show up and the activate doesnt disappear,
i mean in any when i click activate the deactivate sho up in every other td and when i click deactivate again the activate button doesnt show up, any help?
You may want to change
$(".activate-btn").click(function(){$(this).hide();$(".deactivate-btn").show();});
into:
$(".action-btn").click(function(){$(this).hide();$(".deactivate-btn").show();});
$(".deactivate-btn").click(function(){$(this).show();$(".action-btn").show();});
Having 2 buttons is abit confusing to the end user since they wont know which segment is active and which segment is deactivated. From your code,all the buttons are shown when the page loads initially. An alternative would be:
if($fetch['status']=="active"){
<button class="btn-danger statusbtn" data-action="deactivate"
id="<?=$fetch['id']?>">
<span class="glyphicon glyphicon-folder-close"></span>
Deactivate
</button>
}else{
<button class="btn-success statusbtn" data-action="activate"
id="<?=$fetch['id']?>">
<span class="glyphicon glyphicon-ok"></span>
Activate
</button>
}
Then edit your javascript to read:
<script type="text/javascript">
$(".action-btn").on("click", function(e) {
var id = $(this).attr("id");
var segment = $(this).parents("tr").find("td.segment").html();
var action = $(this).attr("data-action");
$.ajax({
"url": "action.php",
"method": "post",
"data": {
"id": id,
"segment": segment,
"action": action
},
success: function(data) {
if(action=='activate'){
$(this).html("
<span class='glyphicon glyphicon-folder-close'>
</span>
Deactivate");
$(this).attr('data-action',"deactivate");
$(this).removeClass('btn-success')
$(this).addClass('btn-danger');
}else{
$(this).html("
<span class='glyphicon glyphicon-ok'>
</span>
activate");
$(this).attr('data-action',"activate");
$(this).removeClass('btn-danger')
$(this).addClass('btn-success');
}
alert(data);
}
});
});
</script>
I haven't run the code to test it for errors but i think this will solve your problem.

Submit when enter is pressed

is it possible to have this form submit when someone presses enter
<div class="form-group">
<?= lang("reference_note", "reference_note");?>
<?php echo form_password('reference_note', $reference_note, 'class="form-control kb-pad" id="reference_note" autofocus="autofocus"');?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default pull-left" data-dismiss="modal"> <?=lang('close')?> </button>
<button type="button" id="suspend_sale" class="btn btn-primary"><?= lang('submit') ?></button>
</div>
the field requires input of a pincode and then when enter is pressed it should submit the form.
now the code is entered and it only submits when the button is pressed
Are you using JQuery? If so, this will do the trick:
$("input").keypress(function(event) {
if (event.which == 13) {
event.preventDefault();
$("form").submit();
}
});
Otherwise, you can use pure javascript to accomplish it by doing this:
<script type="text/javascript">
function submitOnEnter(inputElement, event) {
if (event.keyCode == 13)
{
inputElement.form.submit();
}
}
</script>
Then on your input element, add this:
onkeypress="submitOnEnter(this, event);"

Action button with modals and jQuery

I have a problem in my codes. So the idea is I have two modals that triggered when user Click OK or Fail
So when the user click ok the modal is
<div class="modal-body">
This is the confirmation window to PASS all the Quality Control for, <br/><br/>
Project : <b>'.$row['PROJECT_NAME'].'</b><br/>
HeadMark : <b>'.$row['HEAD_MARK'].'</b><br/>
ID : <b>'.$row['ID'].'</b>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" name="acceptButton" id="acceptButton" class="btn btn-success">Confirm PASS!</button>
and when user click FAIL its still the same body but with different button action,
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" name="rejectButton" id="rejectButton" class="btn btn-danger">Confirm REJECT!</button>
</div>
and my jQuery corresponded with those buttons are,
$('#acceptButton').click(function() {
var myData = $('#data_field');
$.post("acceptClass.php",{
acceptProjectName : myData.attr("data_project"),
acceptHeadMark : myData.attr("data_headmark"),
acceptId : myData.attr("data_id")
},function callback(result){
window.location = "update_fabrication_qc_pass.php";
});
});
$('#rejectButton').click(function() {
var myData = $('#data_field');
$.post("rejectClass.php",{
projectName : myData.attr("data_project"),
headMark : myData.attr("data_headmark"),
id : myData.attr("data_id"),
reasonReject : $("#reasonForRejection").val()
},function callback(result){
window.location = "update_fabrication_qc_pass.php";
});
});
Variables that are to be passed are stored in the hidden input,
echo "<input type='hidden' data_project='$row[PROJECT_NAME]' data_headmark='$row[HEAD_MARK]' data_id='$row[ID]' id='data_field'></input>";
So the problem is, all the buttons only work with the (#rejectButton). So even if I click accept button, only the action in the rejectClass.php are applied to the database. It supposed to be when user click acceptButton, actions in the acceptClass.php that are applied.
Please help me, im so confused.

Categories