ajax on click update form content - php

This is my form.
If i click the status content like progress or dsgs a textfield should appear. If i type text in it and click outside or press enter the old content should updated with the new one. I need it to be done with ajax and php. I am a beginner in php and ajax. Any reference or how can i do this?
This is my code for add status
$insert_task = "INSERT INTO `tbl_task` (`intProjectid`,`intUserid`,`dtDate`,`dtFinishdate`,`varIssue`,`varStatus`,`varNeedhelp` )VALUES ('".$id."','".$userid."','".$dtdate."','".$dtfinish."','".$issue."','".$status."','".$help."');";
$insert_query=mysql_query($insert_task);

You didn't give me anything but I've tried to implement something by guessing and hope if it doesn't solve your problem but at least it will help you. Following code is for your ajax functionality, you can put it inside the head tag of your page between script tags-
$(document).ready(function(){
var eventFlag=false;
var originalText='';
$('#mytable tr td span').click(function(e){
e.stopImmediatePropagation();
$(this).siblings().show().focus();
$(this).hide();
eventFlag=false;
originalText=$(this).siblings().val();
});
$('#mytable tr td input').blur(function(e){
if(!eventFlag && validate($(this))) doAjax($(this));
else
{
$(this).siblings().show();
$(this).hide();
}
});
$('#mytable tr td input').keypress(function(e){
e.stopImmediatePropagation();
var code = (e.keyCode ? e.keyCode : e.which);
if(code==13)
{
if(validate($(this)))
{
doAjax($(this));
eventFlag=true;
}
else
{
$(this).siblings().show();
$(this).hide();
}
}
});
function validate(input)
{
console.log(input.val()+" "+originalText);
if(input.val()!='' && input.val()!=originalText)
return true
else return false;
}
function doAjax(input)
{
var formData="proId="+input.attr('id')+"&text="+input.val();
$.ajax({
type: "POST",
url: "update.php",
data: formData,
success: function(data){
if(data==1)
{
input.siblings().text(input.val()).show();
input.hide();
}
else
{
input.siblings().show();
input.hide();
alert("something Wrong !");
}
},
error:function (xhr, ajaxOptions, thrownError){
alert("Error:"+xhr.status+" "+thrownError);
}
});
}
});
And I guessed your form could be something like this
<form action="#" method="post">
<table id="mytable">
<thead>
<tr>
<th>Issue</th><th>Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>Login</td><td id="1"><span>Progress</span><input id="1" type="text" value="Progress" /></td>
</tr>
<tr>
<td>Something Else</td><td id="2"><span>Anything</span><input id="2" type="text" value="Anything"/></td>
</tr>
</tbody>
</table>
</form>
Put this inside your head or stylesheet (without style tag)
<style>
#mytable tr td input{display:none;}
</style>
And your update.php file should be something like
<?php
$proId = $_POST['proId'];
$text = $_POST['text'];
$update_task="update tbl_task set varStatus='".$text."' where intProjectid=".$proId;
if(mysql_query($update_task))
{
echo "1";
}
else
{
echo "0";
}
?>
I've tested it and working. The id I've used in the form is by assuming that you have id's for each of your status and you should update instead of inserting to change the status. let me know if it helps or if you need more help, I'll be on touch. Thanks!

Related

How to update data directly in row?

I have a problem with my code. I have a php script where i, in a while, i write a table in html with html name and html value attr with the data of database, like the printscreen above
My table was assembled with input inside the TDs, to be able to "edit" directly in the table, without MODAL or redirection
</thead>
<tbody id="corpoTabela">
<?php
$i = 1;
while ($row = mysqli_fetch_array($result)) :;
?>
<tr>
<form name="presencaForm" id="presencaForm" method="POST">
<input type="hidden" id="idcrmmedico" name="idcrmmedico" value="<?php echo $row['id']; ?>"></input>
See that the opening of the form tag is inside the while, that is, for each row of the table, for each TR, I have a form (all with the same name)
The end of the form tag still inside php while. The Button, who receives the ID referring to the record in the DB, has the same name of the form
<td><button id="d" name="presencaForm" value="<?php echo $row['id'] ?>" class="btn btn-sm btn-info">Atualizar</a></td>
<td>
<button type="submit" id="" name="deletamedico" class="btn btn-sm btn-danger">Excluir</button>
</td>
</form>
</tr>
<?php
endwhile;
}
?>
</tbody>
This is my ajax script, which updates the data in the database without refreshing the page and returning me a success message.
<script>
$(document).ready(function() {
$('#presencaForm').on('submit', function(event) {
event.preventDefault();
$("#alert").css('display', 'none');
console.log("Botão Clicado!");
$.ajax({
url: "DAO/medico/update.php",
method: "POST",
data: $(this).serialize(),
dataType: "json",
success: function(data) {
if (data[0] == true) {
$("#success").html('Médico ' + data[1] + ' atualizado com sucesso');
$("#success").show();
setTimeout(function() {
$("#success").hide();
}, 5000);
}
}
})
});
});
</script>
The problem is that if i try to refresh the second line, the page just refreshes.
I used the code below to check, and I saw that all buttons return only the first record in the LOG.
Is there any way to update the data as I want, right in the table?
$(document).ready(function() {
$("button[name='presencaForm']").on("click", function(event) {
event.preventDefault();
for (var i = 1; i < document.getElementById("dataTable").rows.length; i++) {
console.log($('#idcrmmedico').val());
}
var val = document.getElementById('d').value;
var x = document.getElementById("dataTable").rows.length;
});
id is unique,if you want to control a group of form,you can use class.
For example:
<form class="presencaForm" method="POST">
Then in your javascript:
$('.presencaForm').on('submit', function(event) {

How can I toggle among flags when clicking on a <td> element or a <div> within it using AJAX?

The image below depicts how the table looks like right now, with some test data. Under "IMAGE" will be (thumbnails of) images of invoices and bills. The page is going to be used for accounting.
In <td> I put:
echo '<td><div id="currency" onclick="refreshClick()">'. $currency . '</div></td>';
This is the script itself:
<script>
function refreshClick() {
$.ajax({
url: "assets/scripts/php/currency.php",
success: function(data){
$('#currency').html(data);
}
});
}
</script>
so the data is loaded from a php script. But this solution doesn't work. Any suggestions?
Try somthing like this:
Your table:
the id should be unique and it wont be on multiple rows, so use a class and target that td div with $(this) in jQuery.
<td><div class="currency">'. $currency . '</div></td>
jQuery:
<script>
$(document).ready(function(){
$(document).on('click', '.currency', function(e){
$.ajax({
url: "assets/scripts/php/currency.php",
success: function(data){
$(this).html(data);
}
});
}
}
</script>

How to display value of jquery in php

I want to get the value of jquery in php form in popup.
my jquery is
$(document).ready(function(){
$("#submit").click(function() {
var mobileNumber = $("#mobileNumber").val();
if(mobileNumber=="")
{
alert("Please Enter Mobile Number");
}
else
{
$.ajax({
type: "POST",
url: "<?php echo base_url('test'); ?>",
data: {mobileNumber: mobileNumber},
success: function(result){
if(result){
$("#enter-otp").modal('show');
}
}
});
}
return false;
});
});
I want to print var mobileNumber value in enter-otp id popup in same page
so i write
<?php echo $mobileNumber; ?>
but it is not showing
If you want to enter value in enter-otp id popup in same page. You dont want any PHP script you can do it by jquery only. (Although You can write in success of ajax). Suppose you have div tag with id of otp-div inside enter-otp. You can write following code
success: function(result){
if(result){
$("#enter-otp").modal('show');
$("#otp-div").html(mobileNumber);
//OR ifotp-div inout attribute then use `val()`
$("#otp-div").val(mobileNumber);
}
}
depending on what gives you back your "result"-variable, you can output data.
so if your "result"-variable gives you back something useful, you can take this data and put it in the html like this.
success: function(result){
if(result){
$("#enter-otp").html(result).modal('show');
}
}
regarding to your edit, this would be a simple solution:
success: function(result){
if(result){
$("#enter-otp").html(mobileNumber).modal('show');
}
}
i picked up your code and run it on localhost. and after few changes, i got it working. to try following:
test.php
<form action="" method="post">
<input id="mobileNumber" type="text" name="mobileNumber" value="" placeholder="">
<input id="submit" type="submit" name="" value="submit">
</form>
<div id="enter-otp" style="display: none; border: 1px solid red;"></div>
<script>
$(document).ready(function() {
$("#submit").click(function() {
var mobileNumber = $("#mobileNumber").val();
if (mobileNumber == "") {
alert("Please Enter Mobile Number");
} else {
$.ajax({
type: "POST",
url: "result.php",
data: { 'mobileNumber': mobileNumber },
success: function(result) {
if(result){
$("#enter-otp").show();
$("#enter-otp").html(result);
} else {
alert("no data received");
}
}
});
}
return false;
});
});
</script>
result.php
<?php
if( isset($_REQUEST['mobileNumber'] )){
echo $mobileNumber = $_REQUEST['mobileNumber'];
} else {
echo "no data";
}
?>

how to checked the checkbox after fetching the data from database using jquery and php

when user click on a button it displays the dynamic data fetched from the database along with the appropriate check box ..so user has select or check the checkbox of some data and submit the result then those data are stored in database.
again that user clicked the same button then again those data are displayed but this time the data which are selected buy the user previously should be checked. I am trying to do that but enable to write code for it in jquery.
below is my code...
javascript
$(document).ready( function() {
$('#location_choose').click(function(e){
branch();
document.getElementById('brn_light').style.display='block';
document.getElementById('fade').style.display='block';
});
function branch()
{
//alert(user_id);
$.ajax({
url: "<?php echo base_url(); ?>/login/branch",
data: {},
type: "post",
cache: false,
success: function (data)
{
//alert(data);
var obj = $.parseJSON(data);
var result = "";
<?php foreach($resultsb as $rows){?>
for ( var i = 0; i < obj.length; i++ )
{
result+="<table id='branchtable'><tr height='25px' ><td>&nbsp&nbsp</td><td ><input class='test' type='checkbox' name='branch_name' value="+obj[i].id+"<?php echo ($rows['branch_id']=="+obj[i].id+"? 'checked' : '');?>></td><td width='15px'></td><td width='630px'><b>"+obj[i].branch_name+
"</b></td></tr><tr><table id='branch_address' style='background-color:#EBF5FB'><tr><td>&nbsp&nbsp</td></tr><tr><td width='60px'></td><td width='440px'>"+obj[i].address+"</td><td width='40px'></td></tr><td>&nbsp</td></table></tr><tr></tr></table>";
}
<?php }?>
//alert(result);
document.getElementById("branch_table").innerHTML=result;
},
error: function (xhr, ajaxOptions, thrownError)
{
//alert(thrownError);
}
});
}
});
i tried using
<?php echo ($rows['branch_id']=="+obj[i].id+"? 'checked' : '');?>
in above code but still it doesnt checked the previous selected one.
Give The Same Class of Checkbox and Write Jquery Code
<html>
<head>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>
<script>
$( document ).ready(function() {
$( '.select_all_text').click(function() {
var is_check = $(".select_type").prop('checked');
if(is_check == false)
{
$(".select_type").prop('checked', true);
$(''.select_all_text").text('De-select All');
}
else
{
$(".select_type").prop('checked', false);
$('.select_all_text").text('Select All');
}
});
});
</script>
</head>
<body>
<p style="position: sticky; z-index: 100; width: 50%;"><a id="select_all" onclick="check_uncheck" style="margin-right: 2%;" href="javascript:void(0)">Select all</a>
<?php
foreach($data_list as $data)
{
?>
<input type="checkbox" name="select_type[]" value="<?php echo $item_id; ?>" class="select_type" id="select_type"/>
<?php
}
?>
</body>
</html>

how to reset text area after pressing button using angular js?

I am working on a project where i have to perform actions like social media, i stuck on a stage where i have to comment and then i have to reset that editable div.
Editable Div code:
<div id="txtComment" contenteditable="true" class="form-control txtComment" style="height: 156px; margin-bottom: 10px;">
My button for calling comment function Code:
<input type="button" value="POST" ng-click="comment(<?php echo $itemId ?>,<?php echo $itemDesc ?> ,user_comment,0,0,mood)">
my function:
$scope.comment=function (itemid, itemdescid,user_comment,replyId,parentcomId,mood)
{
var feeling="";
//console.log(mood);
if (typeof mood !== "undefined")
{
feeling=mood;
//console.log(mood+"test");
}
var comm="";
if(replyId==0)
{
comm=$("#txtComment").html();
}else if(replyId == parentcomId)
{
comm=$("#"+parentcomId+"comment").html();
//console.log(comm);
} else
{
comm=$("#"+replyId+"comment").html();
}
console.log(comm);
//console.log(feeling);
var req = {
method:"POST",
data:{item_Id:itemid, item_desId:itemdescid, comment:comm,commentReplyId:replyId,parent_commentId:parentcomId,User_feeling:feeling,userId:$scope.id},
url:"<?php echo base_url()?>index.php/ItemImportance/Comment",
headers: {
'Content-Type': 'application/json'
},
};
$http(req).then(function (response) {
if (response.data !=null)
{
if(response.data.msg !=null)
{
alert(response.data.msg);
}
else
{
//var scopeNmae="item"+"_"+itemid+"_"+itemdescid;
// console.log(scopeNmae);
//$scope[scopeNmae]=response.data;
//console.log(response.data);
// window.location.reload();
$scope.itemcomments=response.data;
//eraseText();
}
} else
{
}
}, function (response) {
console.log(response)
});
}
what i have to do is just empty my editable div after i click on comment button!
please help me as fast as you can.
I wrote a really simple example plnkr, please have a look at it.
<body ng-controller="MainCtrl">
<textarea ng-model="comment" placeholder="comment"></textarea>
<button ng-click="comment = ''">Clear</button>
</body>
My recommendations are:
Keep it simple
Don't try to mix Angular with jQuery, unless you really have to
Use appropriate html elements
Edit:
Example plnkr with content editable div. It's a fork from the AngularJS website, I just added the clear button.
this has worked for me, here the
html code
`<textarea [(ngModel)] ="myInput"></textarea>
<button (click) = "this.deleteText();">clear</button>`
angular code
myInput: string;
deleteText(){
this.myInput = '';
}

Categories