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.
Related
I want to be able to send content editable text to a php file to make some changes using ajax. This is how it is set up:
index.php
<div id="textArea">
<div contenteditable id="textField"></div>
</div>
</body>
</html>
<script>
var storyArea = $("#storyArea");
var textField = $("#textField");
var textArea = $("#textArea");
textField.on("keydown", function(e)
{
if (e.which == 13)
{
e.preventDefault();
var newVal = textField.text();
var exp = /\W/g;
if(!(exp.exec(newVal)))
{
$.ajax(
{
type: 'post',
url: 'story.php',
datatype: "html",
data: newVal,
success: function (data)
{
alert(data);
}
});
}
else
{
textArea.css("border", "2px solid #d45454");
textField.empty();
newVal = '';
}
}
});
</script>
story.php
<?php
$input = $_POST['newVal'];
echo $input;
?>
The problem I'm having is that my alert returns "Undefined index: newVal"
Thanks in advance.
You should use data: {newVal:newVal} to give the variable a name for the posted data. The function expects key:value pairs.
I have two pages, index.php and block1.php
I wanted to load a data from block1.php to a div #details in index.php
here's my code
index.php
<div id="pop_box">
<div class="close"><span id="close">×</span></div>
<div id="block"></div>
<div id="details"></div>
</div>
block1.php
<script>
$(document).ready(function() {
var lnk;
$('[href]').click(function() {
lnk = '';
lnk = $(this).attr('href');
if (lnk.charAt(1) == "b") {
var lot = lnk;
$.ajax({
url: "2dmap_func.php",
method: "POST",
dataType: "text",
data: {
lot: lot
},
success: function(data) {
//alert(data);
$('index.php #pop_box #details').load(data);
}
});
}
});
});
</script>
as you can see I want to load the data from block1.php to index.php's div#details and this code doesn't work.
$('index.php #pop_box #details').load(data);
In you index.php add following line:
<?php include_once 'block1.php' ?>
In your block1.php change following line:
$('index.php #pop_box #details').load(data);
to
$('#details').html(data); // if data is valid html
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;
});
});
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);
});
});