delete db entry using js - php

i have three file...
1st index.tpl.php
code of this is
<td>
<input type="button" name="checkbox" value="delete" onclick="selectAccess('<?php echo __('are you sure you want to delete data')?>','<?php echo $sGroups['id_user']; ?>')">
</td>
2nd is common.js
function selectAccess(message,AccessId){
var answer = confirm(message)
alert(AccessId);
if (answer)
{
$.ajax({
url: SITE_URL + '/users/delete',
type: 'POST',
data: {
'ID':AccessId
},
success: function(data){
alert(data);
}
});
}
}
here its not working..... plese advice me where is mistake
thnks...

do you see the confirm box ? (then click event was rightly handled and processed)
Is SITE_URL defined somewhere and do you see the XHR request fire ?
(then ajax call is good)

Related

Trying to hide specific div in jquery in php while loop

I have this form being outputted from a PHP while loop :
echo '<div id="postCont" class="postCont'.$pid.'" style="block;">
<div id="clLink">
<a id="clLink" href="'.$plink.'" target="_blank"" title="'.$ptitle.'">'.$ptitle.'</a>
</div>
<div id="clDate">Posted on '.$pdate.'</div>
<div id="clDesc">'.$pdesc.'</div>
<form method="post" class="ibadForm">
<input type="hidden" name="id" value="'.$pid.'">
<input type="hidden" name="hiddenBad" value="yes">
<input type="image" src="../img/bad.png" name="subBad" value="Bad" class="bad">
</form>
</div>';
I am trying to remove the individual .postCont when the ibadForm is clicked with jquery.
$(".ibadForm").submit(function(e) {
var url = "add_form.php";
var id = <?php echo $pid?>;
$.ajax({
type: "POST",
url: url,
data: $(this).serialize(),
success: function(data)
{
$('.postCont' + id).hide();
}
});
e.preventDefault();
});
It submits the form to add_form.php fine but doesn't hide the postCont. If I remove the id from the class then it hides all post Cont's. Can anyone tell me where I am going wrong?
You could use closest() to get the parent div then hide it using hide() method like :
$(".ibadForm").submit(function(e) {
var url = "add_form.php";
var id = <?php echo $pid?>;
var _this = $(this);
$.ajax({
type: "POST",
url: url,
data: $(this).serialize(),
success: function(data)
{
_this.closest('.postCont').hide();
}
});
e.preventDefault();
});
NOTE : You should store the $(this) object that refer to the clicked form in some variable (_this in my example) then use it inside the success callback since $(this) inside callback doesn't refer no more to the form, e.g :
_this.closest('.postCont').hide();
Hope this helps.

calling OOP PHP function via AJAX

I have OOP php file and I would like to call one of the function by AJAX, i have read a lot about this (mostly stackoverflow), but for some reason, it just doesnt work.
I know that ajax function is called (I tried to add some alert into success function and that alert appeared after clicking the button), but somehow it ignores everything from file ajax.php (i even tried to add some echo at the top of ajax.php file, but nothing happened)
also note that I need button, not input (although that would be much easier)
this is my button:
<button class='formular-button' type='button' onclick='prihlasPA()'> vypiš přihlaš </button>
this is my script for button:
<script>
function prihlasPA() {
$.ajax({
type: "POST",
url: "ajax.php",
data: {action: 'prihlasP'},
success: function(){},
error: function(){
alert("chyba");
}
});
}
</script>
and this is inside ajax.php, which is called by AJAX:
include( 'navstevnik.php' );
echo "aaa";
if(isset($_POST['action']) ) {
$navstevnik = new navstevnik; //navstevnik is name of my class btw
$navstevnik->vypisPrihlas();
}
I dont think it matters, but in my function vypisPrihlas() is this code:
public function vypisPrihlas(){
$this->index=1;
echo '
<fieldset class="formular">
<div class="pure-control-group stred" >
<input id="nick" type="text" name="nickP" placeholder="Nickname">
</div>
<div class="pure-control-group stred">
<input id="password" type="password" name="hesloP" placeholder="Heslo">
</div>
<input class="pure-button pure-button-primary stred" style=width:188px; type="submit" name="buttonP" value="přihlásit" />
</fieldset>';
}
I don't see any issues with the above code.
When the function vypisPrihlas echos the HTML, you won't see it on your main page. Instead, you need to load it in from your success function and append the HTML somewhere on your page.
$.ajax({
type: "POST",
url: "ajax.php",
data: {action: 'prihlasP'},
success: function(data){
$( ".your_favorite_div" ).append(data);
},
error: function(){
alert("chyba");
}
});
By what you said, the ajax function is fine, right?
I think your problem is the path for PHP, since it is not called. try change this line
url: "ajax.php",
for the complete path, like \var\www\path_to_code\ajax.php. Maybe it helps.

how to hide a button after clicking it and calling a php page

Why its not working ?
I want to hide a submit button after it is clicked and also run a php page in the background at the same time to insert the entry into the table. How both these functions can be accomplished at the same time ?
function click(){
$(".hide").hide();
$.ajax({
type: "POST",
url: "onProfile.php",
dataType: 'html',
data: {data you want to send},
success: function(data){
}
});
return false;
}
<input type="submit" onClick="click()" class="hide" value="Submit">
Add an id to your button called: submit. Then no need to add the onclick. (Or you can use the $('.hide') selector.)
<input type="submit" class="hide" value="Submit" id="submit">
After this, you can check the click:
$('#submit').click(function(e) {
e.preventDefault();
$(this).hide();
$.ajax({
type: "POST",
url: "onProfile.php",
dataType: 'html',
data: {data you want to send},
success: function(data) {
}
});
});
The input element has a function click and is called instead of your click function, easiest solution is to change the name of your function.
A better solution would be to not use inline event handling
$(".hide").on('click', click);
success: function(){
$(button).hide();
}

Jquery ajax implementation issue

I'm working on the index.php. In the index page, my php code is generating Columns and Row(Table) in html. I am new to Jquery.
PHP generated table example
ID | Username | Status | Action
1 Demo No Install button
2 Smith No Install button
3 Edward No Install button
4 Admin No Install button
In the Action column , I've added Install button. So , my plan is to call update.php using Jquery AJAX POST when Install button is clicked.
Update Page Having POST variable :
1) id
2) action
3) flag
Now, what I've done so far.
Following code is generated by my PHP code (RAW code, for understanding)
<input type='hidden' name='id' id='1' value='1'>
<a href class='blue' href='' id='1'>Install</a>
<input type='hidden' name='id' id='2' value='2'>
<a href class='blue' href='' id='2'>Install</a>
<input type='hidden' name='id' id='3' value='3'>
<a href class='blue' href='' id='3'>Install</a>
<input type='hidden' name='id' id='4' value='4'>
<a href class='blue' href='' id='4'>Install</a>
Issue is how I can generate Jquery AJAX POST url, which will send data like
update.php?id=1&action=install&flag=1
Please suggest me.
Say this is your button
<input type="button" id="my_button" value="1">
$(document).ready(function () {
$("#my_button").on('click',function () {//Your install button click
var btInd = $(this).attr('value');
$.ajax({
type: "POST",
url: "update.php",
data: {"id":btInd,"action":"install","flag":1}, //You need to make changes if your values will change since i have hard coded the values
success: function(response) {
alert('Success');
}
});
});
Catch the request in update.php and do the rest.
Try like this
Script
$(document).ready(function () {
$("button").click(function () { // install button
var id = $(this).attr("id");
var action = "install";
var flag = "1";
$.ajax({
type: "POST",
url: "update.php",
data: {"id":id,"action":action,"flag":flag},
success: function(response) {
// do something
}
});
});
});
Sample PHP
<?php
include('config.php');
if($_POST["action"]=="install"))
{
$id = $_POST['id']; //Here posted id
$flag = $_POST['flag'];
}
?>
try this,
$.ajax({
type: 'POST',
url: 'update.php',
data: {
id:1,
action:xx,
flag:ff
},
success: function(response){
console.log(response);
}
});
for more information see this link
$.post( "update.php", { id: "John", action: "delete",flag :"abc" })
.done(function( data ) {
alert( "Data Loaded: " + data );
});
you can use jquery GET method to post data using get ..
$.get("update.php?id=1&action=install&flag=1",function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
or you can send data by
$.get("update.php",{id=1,action=install,flag=1},function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
$("#buttonID").live('click',function(){
$.ajax({
url: "../update.php",
type: 'POST',
data: { id: myId, action: myAction ,flag: myflag},
dataType: 'xml',
success: function (data) {
alert("Great success");
}
});
});

Getting 'commentcontent' via ajax

I'm building up an ajax function that grabs the info needed to send into the database than print it out on the page. Only it grabs the streamid but not the commentcontent. I can see this in firebug under the post parameters. The strange thing is, I've used the same method for my main status updates and just changed the id's so they don't conflict.
Most would say there is no value in the commentcontent..but that would be the users inserted comment, and their is no value on my main status updates..So I'm rubbing my head thinking, where am I going wrong?
FORM
<form id='mycommentform' method='POST' class='form_statusinput'>
<input type='hidden' name='streamid' id='streamid' value='".$streamitem_data['streamitem_id']."'>
<input class='text' name='commentcontent' id='commentcontent' placeholder='Say something' autocomplete='off'>
<input type='submit' id='button' value='Feed'>
</form>
</div>
AJAX
<script>
$(document).ready(function(){
$("form#mycommentform").submit(function(event) {
event.preventDefault();
var streamid = $("#streamid").val();
var commentcontent = $("#commentcontent").val();
$.ajax({
type: "POST",
url: "comment_add.php",
cache: false,
dataType: "json",
data: { streamid: streamid, commentcontent: commentcontent},
success: function(response){
$("#commentcontent").val("");
$("#commentaddid").html("<div class='stream_comment_holder' style='display:none;' id='comment_holder_"+response['streamitem_id']+"'><div id='comment_list_"+response['streamitem_id']+"'></div></div>");
}
});
return false
});
});
</script>
You always need to quote your data, so changing it to: { 'streamid': streamid, 'commentcontent': commentcontent} should probably fix your issue
After some discussion, we found out var commentcontent = $(this).children('#commentcontent ').val(); fixed the issue

Categories