Jquery ajax implementation issue - php

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");
}
});
});

Related

how to get the data from ajax request then use it in php conditional statemtn

I want to create an ajax post request that gets the value of the radio button then use it in a PHP conditional statement.
So far i have tried this code (all code is from one php file):
$(document).ready(function () {
$('.radio-buttons input[type="radio"]').click(function(){
var subject= $(this).val();
$.ajax({
url: 'home.php',
type: 'POST',
data: {
'subject': subject
},
success: function(response) {
alert(response);
}
});
});
});
<form id="form1" method="POST" class="radio-buttons ">
<input class="radio-filter" type="radio" name="subject" value="A">A</input>
<input class="radio-filter" type="radio" name="subject" value="B">B</input>
</form>
if (isset($_POST['subject'])) {
echo "Showing!";
}
the alert message shows the value of the radio button when I clicked them but the echo in PHP condition is not showing.
You are using alert(subject); which will just alert the value of the radio button as you've mentioned.
Change that line to alert(response); to alert the response on success.
The alert(subject) needs to be alert(response).
Change alert(subject) to alert(response)
because 'subject' is what you have been sent! so after it 'subject' will receipt by your PHP process and returned as response(echo "Showing") on value of the function an object success: function(response)
and alert the response to see data/text "Showing"
$(document).ready(function () {
$('.radio-buttons input[type="radio"]').click(function(){
var subject= $(this).val();
$.ajax({
url: 'home.php',
type: 'POST',
data: {
'subject': subject
},
success: function(response) {
alert(response);
}
});
});
});

How to display the AJAX response in input field and pass that value to PHP?

I am getting Id value from my AJAX and I am also able to display it. I set the Id value input text in the AJAX and displaying it on HTML like this <input value="4864" id="compare_id" type="hidden"> Now I have to pass that value to PHP to get the result from database.
Is there any idea how to do it?
AJAX
$(document).ready(function() {
arr = [];
$("[type=checkbox]").click(function() {
$.ajax({
type: "POST",
url: "includes/compare_process.php", //
data: 'users=' + arr,
dataType: 'json',
success: function(msg) {
$("#pics_name,#pics_Id").empty();
$.each(msg, function() {
$("#pics_Id").append("<input type=hidden value=" + this.Id + " id=compare_id name=compare_id>");
//alert(msg.id);
});
},
error: function() {
alert("failure");
}
});
});
});
//tried AJAX
$(document).ready(function(){
$('#search-button').click(function(){
$.ajax( {
type: "GET",
url: 'includes/compare_process.php?key=compare_details',
data: $('#search-form').serialize(),
success: function(response) {
//$('#response').html(response);
alert(response);
}
} );
});
});
PHP
<div class="bg-popup" style="display: none;" id="open_compare_popup">
//popup code here
<?php
$val2=htmlentities($_GET['compare_id']);
echo $val2;
$sql = "SELECT * FROM `request` WHERE Id IN($val2)";
?>
//end popup code here
This output I am getting from AJAX
<form action="#" method="get" id="search-form">
<span id="pics_Id">
<input value="4869" id="compare_id" name="compare_id" type="hidden">//output
<input value="4884" id="compare_id" name="compare_id" type="hidden">//output
<input value="5010" id="compare_id" name="compare_id" type="hidden">//output
</span>
<button class="action action--button action--compare" type="button" id="search-button" name="search-button"><span class="action__text">Compare</span></button>
</form>
I have to pass the input value to PHP after clicking on a button.

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

delete db entry using js

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)

Return data from PHP back to a javascript file?

I have a comments section on my websites that uses jQuery to animate comments in without reloading the page, as well as deleting comments.
Right now, I have it so when you write a comment, it sends it to a external JS, then to a php file where it processes. If it was successful, it will append the comment into the comment section. My delete action: jQuery deletes my the comment ID in the mysql database.
So my issue is I want to add a delete button via jQuery and somehow call back to the javascript file and tell it the id so the delete button knows the ID to put in the form so the delete file knows what to delete?
Here's my add_comment script:
$(function() {
$(".commentbutton").click(function() {
$.ajax({
type: "POST",
url: "http://myflashpics.com/process_addcomment.php",
data: $("#commentform").serialize(),
success: function() {
var theComment = $("#commentsss").val();
var theUserId = $("#user_id_two").val();
var thePhotoId = $("#photo_id").val();
var theProfilePicture = $("#user_profile_picture").val();
var theUsername = $("#user_username").val();
// Get new HTML data
var html = "<div class='comment_odd'><img src='" + theProfilePicture + "' class='comment_thumbnail'/><div class='comment_username'>" + theUsername + "</div><div class='comment_text'>" + theComment + "</div></div>";
// Append, then fade in
$(html).hide().appendTo(thecommentsdisplay).fadeIn(500);
}
});
return false;
});
});
Thanks in advance!Coulton
EDIT 1:
Here's my comments form (just to clarify):
user_id_two (the user's ID)
commentsss (comments field)
photo_id (the id of the photo being commented on)
user_profile_picture (profile to display on the user's profile picture in the banner)
user_username (username of the user commenting)
Here's also my delete button form:
<form method='post' action='' name='deleteform' id='deleteform'>
<input type='hidden' name='userid' value='<?php echo "$userid_session"; ?>' />
<input type='hidden' name='userpass' value='<?php echo "$password_session"; ?>' />
<input type='hidden' name='pictureid' id='pictureid' value='<?php echo "$picture_id"; ?>' />
<input type='hidden' name='profilepictureid' id='profilepictureid' value='<?php echo "$user_profile_picture_id"; ?>' />
</form>
And lastly my DELETE COMMENT jQuery:
$(function() {
$(".commentdeletebutton").click(function() {
var className = $(this).attr('class');
var theID = className.replace(/delete_button commentdeletebutton delete_(\d+)/, "$1");
commentDone = "#comment_" + theID;
formDone = "#commentdeleteform_" + theID;
$.ajax({
type: "POST",
url: "http://myflashpics.com/process_deletecomment.php",
data: $(formDone).serialize(),
success: function() {
$(commentDone).hide();
}
});
return false;
});
});
If you add an link for deleting, you can insert the complete url for deleting in the href, i.e. http://myflashpics.com/process_removecomment.php?id=xx.
So, you can bind a click event to that link and get the right url using $(this).attr('href') and doing the delete using ajax.
Edited for a more complete sample:
<? [[Loop your recordset]] { ?>
<div class="comment">
Delete
<div class="comment">
[[Comment content...]]
</div>
</div>
<? } ?>
<script>
$(function() {
$(".commentdeletebutton").click(function() {
$this = $(this);
$.ajax({
url: $this.attr('href'),
success: function(data) {
$this.parent().slideUp('fast', function() {
$this.parent().remove();
});
}
});
return false;
});
});
</script>

Categories