I want a simple that when I click, it loads the content from a file file.php.
I also need the Ajax to send a string to file.php
This is my file.php
session_start();
include("../database.php");
if (isset($_GET['id'])) {
$to = $_GET['to'];
mysql_query("UPDATE `flashchat` SET `open` = '1' WHERE `to` = '$to' AND `from` = '$_SESSION[id]'");
}
The PHP string
$chatinfo[id];
Thanks!
<a id="link" href="#">Click on me to get the contents of the file.php</a>
<div id="result">Result form file.php</div>
<script>
$("#link").click(function(){
$.ajax({
type: "GET",
url: "file.php",
data: { id: 123, to: 456 }
})
.done(function( html ) {
$("#result").html( html );
});
})
</script>
$(function(){
$('#link').click(function(){
var to = <?php echo $chatinfo['to']; ?>;
$.ajax({
type: 'GET',
url: 'lukk.php',
data: 'id='+to+,
sucess: function() {
$('#sucess').html(data);
}
});
return false;
});
});
Related
My PHP is returning this data to Ajax...
echo $data6['favorite_properties_id'];
I am updating it in one function and trying to send it to another using following html and jquery .
<img class="<?php if($favorite == 1){ echo 'alreadyfavorite';} else { echo 'addtofavorite';} ?>" pid="<?php echo $propertyid; ?>" fpid="<?php while($data5=$select5->fetch()){echo $data5['favorite_properties_id'];} ?>" src="../images/system/addtofavorite.png">
This is my jquery...
$('.alreadyfavorite1').click(function() {
event.preventDefault();
var del_id = $(this).attr('fpid');
var $ele = $(this).parent().parent().parent();
var reference = this;
$.ajax(
{
type: 'POST',
url: '../controllers/favoritesaddremove.php',
data:
{
del_id: del_id
},
success: function(data)
{
$ele.fadeOut(1000).delay(1000).remove(1000);
}
});
});
// On Search Property Results Page - Add to Favorite Button (Heart)
$('.addtofavorite').click(function() {
event.preventDefault();
var ins_id = $(this).attr('pid');
var del_id = $(this).attr('fpid');
var reference = this;
/* alert(del_id);
alert(ins_id); */
$.ajax(
{
type: 'POST',
url: '../controllers/favoritesaddremove.php',
data:
{
ins_id: ins_id
},
success: function(data)
{
$(reference).toggleClass("addtofavorite alreadyfavorite");
$('.alreadyfavorite').attr('fpid', data);
}
});
});
The second function is not working, but if i refresh the page then the second function is working...
First assign some id to the image and change fpid to data-fpid(data-attribute):
<img class="asdasd" id="aid" data-fpid="something">
In your success try:
success: function(data)
{
$('#aid').data('fpid', data); //this should update the value in data-fpid
}
I have a page with list of buttons, when each button is clicked, it's value is captured and ajax call in made. PHP does DB updates on ajax call. I want to return data to ajax call. The data is obtained from DB. But I'm unable to point out what's the error in below code.
Here is PHP code:
if (isset($_GET['val']))
{
$chapter_id=$_GET['val'];
$sql= "SELECT file_name,edit_link,name,email FROM `chapter_list` where chapter_id='$chapter_id'";
$result = mysql_query($sql,$rst);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$vol_name = $row["name"];
$vol_email= $row["email"];
$vol_link= $row["edit_link"];
}
$update=mysql_query("UPDATE `chapter_list` SET `status` = '$update_status' WHERE `chapter_list`.`chapter_id` = '$chapter_id';");
header('Content-Type: application/json');
echo json_encode(array("name"=>$vol_name,"email"=>$vol_email,"link"=>$vol_link));
}
Here is the AJAX request
$(document).ready(function(){
$('.btn').click(function(){
var clickBtnValue = $(this).val();
$.ajax ({
url: '',
data: { val : clickBtnValue},
dataType:'JSON',
success: function(res) {
alert(res.name);
}
});
});
});
I'm not getting the alert!
Try like this.
Maybe response data is null.check your php code(query lines).
Here My php code is :
if (isset($_GET['val'])) {
$vol_name = 'dummy_name';
$vol_email = 'dummy_email';
$vol_link = 'dummy link';
header('Content-Type: application/json');
echo json_encode(array("name"=>$vol_name,"email"=>$vol_email,"link"=>$vol_link));
exit;
}
My javascriptcode is :
<input type="text" class="btn" value="test" />
<script type="text/javascript">
if('addEventListener' in document){
document.addEventListener("DOMContentLoaded", function(e){
//dom loaded
$(document).on("click",".btn",function(e){
e.preventDefault()
var e_val = $(this).val();
console.log('my value is :' + e_val);
if(e_val){
$.ajax({
type: "get",
dataType: 'json',
url: 'here your url or slash',
data: { // request e_val
val : e_val,
}
}).done(function(xhr) {
// console.log(xhr);
if(xhr.name){
alert('response data is '+ xhr.name);
}
})
}
})
},false)
}
</script>
try this..
while($row = mysql_fetch_assoc($result))
{
$vol_name = $row["name"];
$vol_email= $row["email"];
$vol_link= $row["edit_link"];
$ret[$vol_name]= array(
'email'=>$vol_email,
'link'=>$vol_link
);
}
then use in the return statement..
echo json_encode($ret);
You can send parameters in HTML
<button class="btn" atribute_id="21543">Button</button>
$(document).ready(function() {
$('.btn').click(function() {
var Value_of_Btn= $(this).attr("atribute_id"); <-------
$.ajax({
url: '',
data: {
val: clickBtnValue
},
dataType: 'JSON',
success: function(res) {
alert(res.name);
}
});
});
});
So I have 2 files file1.php with all the php and ajax, and file2.php with only php.
$_POST["there_id"] will be sent from file1.php page to file2.php through ajax.
file1.php code:
<div class="list_items">
<li><p>content here ...</p>
</div>
<div class="content_area">
//load ajax content here
</div>
$(".box").on("click", function(e) {
e.preventDefault();
var there_id = $(this).attr("id");
$.ajax({
url: "indexnew.php",
type: "POST",
data: { there_id : there_id}
});
});
file2.php code:
<div id="file2content>
<?php
$there_id = $_POST['there_id'];
$user_id = 5;
$fetch_data = regular_query("SELECT * FROM contents WHERE
(user_by = :me or user_by + :them) AND (user_to = :me or user_to = :them)",
["me" => $user_id, "them" = $there_id], $conn);
foreach ($fetch_data as $data) : ?>
<li><p>database data here</p>
<?php
endforeach;
?>
</div>
now what i want to do is when file2.php is done with php and list items are available i want to load file2contents to content_area on file1.php.
$(".box").on("click", function(e) {
e.preventDefault();
var there_id = $(this).attr("id");
$.ajax({
url: "indexnew.php",
type: "POST",
data: { there_id : there_id},
success: function(data) {
$('.content_area').html(data);
}
});
});
However, content_area should really be an id rather than a class so that it's unique.
Since you're just returning html, you can simplify it using the .load() function.
$('.box').on('click', function(e) {
e.preventDefault();
var there_id = $(this).attr('id');
$('.content_area').load('indexnew.php', {there_id: there_id});
});
success: function(data) { $(".content_area").html(data); }
adding the above to your AJAX call should do what you want if I understand you correctly.
I've a link in my php page. Which I want to send a id request to server. But it doesn't response correctly. Actually, I've got two pages.
a. users.php
b. user_update.php
So I'm trying a send a request to the server using jquery/ajax by user id from users.php page to user_update.php for update the details and get back the successful result to user.php page.
The code is look like this:
<div id="edit_user_details">Edit Details</div>
<script>
$('#edit_user_details').click(function() {
id = <?php echo $uid; ?>
$.ajax({
url: edit_user_details.php,
type: post,
data: id;
success: function(response) {
$('#edit_user_result').html(response);
}
});
});
</script>
But it's not working properly. is there anything I'm missing because of I'm new learning of Jquery/Ajax. :)
Update:
I'm requesting this id in php while loop. It's look like this..
while($search_result = mysql_fetch_array($getUser)){
$uid = (int) $search_result['user_id'];
<div id="edit_user_details">Edit Details</div>
}
<script>
$('#edit_user_details').click(function() {
id = <?php echo $uid; ?>;
$.ajax({
url: 'edit_user_details.php',
type: 'post',
data: {'id' : id},
success: function(response) {
$('#edit_user_result').html(response);
}
});
});
</script>
Is it wrong ? What I'm trying is... I want to update user details base on their id. So what need to do for this ?
you need to create a unique id in loop or create a function and calll it onclick like
<?php while($search_result = mysql_fetch_array($getUser)){
$uid = (int) $search_result['user_id']; ?>
<div id="edit_user_details">Edit Details</div>
<?php }?>
<script>
function myfunc(id) {
id = id;
$.ajax({
url: 'db_search.php',
type: 'post',
data: {'id' : id},
success: function(response) {
$('#edit_user_result').html(response);
}
});
}
</script>
you should end your id initialization with a semi-colon;
id = <?php echo $uid; ?>;
Check the correct way of using ajax in this example: Demo ajax using JQuery
Make changes as shown below and try again..
1: Add 'post' in type.
2: Add ''edit_user_details.php' in url
3: Add semicolon at the end here
<?php echo $uid; ?>;
4: take your id as shown in below code
Updated code :-
<script>
$('#edit_user_details').click(function() {
id = <?php echo $uid; ?>;
$.ajax({
url: 'edit_user_details.php',
type: 'post',
data: {'id' : id},
success: function(response) {
$('#edit_user_result').html(response);
}
});
});
</script>
For more information :-
https://api.jquery.com/jQuery.ajax/#entry-examples
i think you are not declaring var id before
try
<div id="edit_user_details">Edit Details</div>
<script>
$('#edit_user_details').click(function() {
var id = <?php echo $uid; ?>;
$.ajax({
url: edit_user_details.php,
type: post,
data: [id:id],
success: function(response) {
$('#edit_user_result').html(response);
}
});
});
</script>
Check your url inside $.ajax. If you are sending your data to user_update.php then your code should be as follows :
<script>
$('#edit_user_details').click(function() {
id = <?php echo $uid; ?>
$.ajax({
url: user_update.php,
type: post,
data: id,
success: function(response) {
$('#edit_user_result').html(response);
}
});
});
</script>
Just change your url to user_update.php and put a comma(,) after data:id in the place of semicolon(;).
This code will works fine for what you need and with less lines, assuming that the PHP var $uid will be processed on loading the page:
<div id="edit_user_details">Edit Details</div>
<script>
$('#edit_user_details').click(function() {
$.get('edit_user_details.php', 'id=<?php echo $uid; ?>', function(response){
$('#edit_user_result').html(response);
});
});
</script>
This the HTML code that displays the first two comments in my site. I want if a user clicks on the Load more comments button, it should load the fetched comments that is returned via the jQuery AJAX and append it to the two divs in .comment_data div but it is not working even though the AJAX seems to be returning the expected result.
This is the HTML code:
<div class = 'feeds'>
<div class = 'comments'>
<div class = 'comment_data>
<div class = 'per_comment'>
<p> slideToggle!</p>
</div>
<div class = 'per_comment'>
<p> classToggle!</p>
</div>
<button class='morecomments' value='7' name = 'more' type='submit'>
Load more comments</button>
</div>
</div>
</div>
And this is the AJAX code:
$(".morecomments").click(function () {
var post_id = $(this).val();
var request = $.ajax({
url: "comments.php",
type: "POST",
data: { post : post_id },
dataType: "html"
});
request.done(function( msg ) {
$(this).prev('.per_comment').html( msg );
});
});
And the comments.php code:
if(isset($_POST['post']) )
{
$post_id = $_POST['post'];
$qry = "SELECT user_id, comment FROM comments WHERE post_id = ? ORDER BY time DESC LIMIT 1, 1000";
$q = $conn->prepare($qry) or die("ERROR: " . implode(":", $conn->errorInfo()));
$q->bindParam(1, $post_id);
$q->execute();
if($commentz = $q->fetchAll()){
foreach ($commentz as $comment){
echo "<div class = 'per_comment'>";
echo "<p>". $comment[0] ." ". $comment[1] . "</p>";
echo "</div>";
}
}
}
Try the following:
$(".morecomments").click(function () {
var $this = $(this);
var post_id = $(this).val();
var request = $.ajax({
url: "comments.php",
type: "POST",
data: { post : post_id },
dataType: "html"
});
request.done(function( msg ) {
$this.prev('.per_comment').html( msg );
});
});
this isn't what you think it is. Try setting the context property of the ajax options:
$(".morecomments").click(function () {
var post_id = $(this).val();
var request = $.ajax({
url: "comments.php",
type: "POST",
data: {
post: post_id
},
dataType: "html",
context: this
});
request.done(function (msg) {
$(this).prev('.per_comment').html(msg);
});
});